Git Product home page Git Product logo

klaviyo-elixir's Introduction

Klaviyo

Installation

klaviyo is published on Hex. Add it to your list of dependencies in mix.exs:

def deps do
  [
    {:klaviyo, "~> 3.0"}
  ]
end

Usage

Requests can be made using the send/2 function. This function accepts a Klaviyo.RequestOperation struct as the first argument and a keyword list of configuration options as the second argument.

Resource modules (e.g.Klaviyo.Event) return Klaviyo.RequestOperation structs.

Example

iex> %Klaviyo.Event.all()
      ...> |> Klaviyo.send(
      ...>      api_key: "pk_xxx",
      ...>      revision: "2023-01-24"
      ...>    )
      {:ok, %Klaviyo.Response{}}

Configuration

The send/2 function takes a keyword list of configuration options as the second argument. These provide the client with additional details needed to make a request and various other options for how the client should process the request and how it should behave.

Options

  • :access_token - OAuth2 access token
  • :api_key - Public or private Klaviyo API key
  • :client - HTTP client adapter used to make the request. Defaults to Klaviyo.HTTP.Hackney.
  • :client_opts - Configuration options passed to the client adapter
  • :headers - HTTP headers used when making a request
  • :host - Hostname used when making a request. Defaults to a.klaviyo.com.
  • :json_codec - Module used to encode and decode JSON. Defaults to Jason.
  • :path - Base path used when building the URL to send a request to
  • :port - HTTP port used when making a request
  • :protocol - HTTP protocol used when making a request
  • :retry - Module implementing a request retry strategy. Disabled when set to false. Defaults to false.
  • :retry_opts - Options used to control the behavior of the retry module
  • :revision - Revision of the API endpoint

Retries

klaviyo has a built-in mechanism for retrying requests that either return an HTTP status code of 500 or a client error. You can enable retries by providing a module that implements the Klaviyo.Retry behaviour to the :retry option when calling Klaviyo.send/2.

Currently, klaviyo provides a Klaviyo.Retry.Linear strategy for retrying requests. This strategy will automatically retry a request on a set interval. You can configure the interval by adding :retry_in with the number of milliseconds to wait before sending another request to the :retry_opts option.

Example

iex> %Klaviyo.Event.all()
      ...> |> Klaviyo.send(
      ...>      api_key: "pk_xxx",
      ...>      retry: Klaviyo.Retry.Linear,
      ...>      retry_opts: [retry_in: 250],
      ...>      revision: "2023-01-24"
      ...>    )
      {:ok, %Klaviyo.Response{}}

The example above would retry a failed request after 250 milliseconds. By default Klaviyo.Retry.Linear will retry a request immediately if :retry_in has no value

klaviyo-elixir's People

Contributors

anthonator avatar dependabot-preview[bot] avatar dependabot[bot] avatar msaurabhee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

klaviyo-elixir's Issues

`retry` option error: function true.wait_for/2 is undefined (module true is not available)

hello!

There is a bug in the current code for the retry options.
Currently, the retry option is typed in the %Opts{} struct as a boolean | module, but this fails when the value is a boolean (see the bug in this line).

Can you please fix it?
Otherwise, I can create a PR to get it fixed but before spending time on it I will need to know your idea for this retry option... is the boolean type deprecated? which default value should be preferred?

My initial approach would be something like (disclaimer, this code won't work it's just to get the idea)

  defp do_retry(result, request, opts) do
    attempt = Map.get(request.private, :attempt, 1)

    max_attempts = Keyword.get(opts.retry_opts, :max_attempts, 3)

    if max_attempts >= attempt do
      seconds_to_wait = seconds_to_wait(opts)

      :timer.sleep(seconds_to_wait)

      request
      |> Map.put(:private, Map.put(request.private, :attempt, attempt + 1))
      |> do_send(opts)
    else
      {:error, result}
    end
  end

  defp seconds_to_wait(retry: retry) when is_boolean(retry) do
    @whatever_default_wait
  end

  defp seconds_to_wait(retry: retry) when is_module(retry) do
    opts.wait_for(request, opts)
  end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.