ApifyClient.Client (apify_client v1.0.0)

Copy Markdown View Source

Runtime client behind the generated ApifyClient.API.* operations.

Every generated function ends in ApifyClient.Client.request/1, which sends the request with Req, retries rate limits and server errors, and decodes the response into the declared ApifyClient.Schemas.* structs.

Options

These options are accepted by every generated function, next to the operation's own options:

  • :config - an ApifyClient.Config to use. The options below override its fields.

  • :token, :base_url, :timeout_ms, :max_retries, :min_delay_between_retries_ms, :user_agent_suffix, :json_library - see ApifyClient.Config.new/1. Without a token, requests are sent unauthenticated (public endpoints only).

  • :content_type - content type of the request body (see "Request bodies")

  • :req_options - extra Req options for this request. They are merged over config :apify_client, :req_options, which tests can use to route every request to a stub:

    config :apify_client, req_options: [plug: {Req.Test, MyApp.Apify}]

Responses

  • The API's {"data": ...} envelope is removed: ApifyClient.API.Runs.get/2 returns {:ok, %ApifyClient.Schemas.Run{}}.
  • JSON is decoded into the declared schema structs (snake_case fields, DateTimes for timestamps). Untyped JSON such as Actor input, key-value records or Actor output stays as decoded JSON (string keys).
  • Non-JSON responses (logs, CSV/XLSX/JSONL dataset items, record files, zip archives) are returned as the raw binary.
  • Operations without a response body (204, HEAD) return :ok.
  • Errors are {:error, %ApifyClient.Error{}}, for API errors with the status code in details.status_code and the API's error type (such as "record-not-found") in details.error_type.

Request bodies

Schema structs are sent under the API's field names, leaving out nil fields; plain maps are sent unchanged, so they use the API's own (camelCase) names. Endpoints that accept any content type (key-value records, Actor input) send a binary body as it is; pass :content_type to set its type, for example content_type: "image/png".

Retries

Rate limits (429), server errors (5xx) and network errors are retried up to :max_retries times for every method, waiting a random time between d and 2d milliseconds, where d starts at :min_delay_between_retries_ms and doubles with every retry. This is the backoff the Apify API documentation recommends and the official JavaScript client uses.

Summary

Types

The request description every generated operation passes to request/1.

Functions

The ApifyClient.Config for opts: :config (or ApifyClient.Config.new/1 defaults) with the config options in opts applied on top.

The options in opts that configure the connection (:config, the ApifyClient.Config keys, :req_options and a replacement :client module), for passing them on to another call.

Performs a generated operation. Called by the generated ApifyClient.API.* functions; see the module documentation for options and return values.

Performs operation and returns the response body as a stream of chunks.

Types

operation()

@type operation() :: %{
  :method => :get | :post | :put | :patch | :delete | :head,
  :url => String.t(),
  :opts => keyword(),
  optional(:args) => keyword(),
  optional(:call) => {module(), atom()},
  optional(:query) => [{String.t(), term()}],
  optional(:body) => term(),
  optional(:request) => [{String.t(), term()}],
  optional(:response) => [{integer() | :default, term()}]
}

The request description every generated operation passes to request/1.

Functions

config(opts)

@spec config(keyword()) :: ApifyClient.Config.t()

The ApifyClient.Config for opts: :config (or ApifyClient.Config.new/1 defaults) with the config options in opts applied on top.

connection_opts(opts)

@spec connection_opts(keyword()) :: keyword()

The options in opts that configure the connection (:config, the ApifyClient.Config keys, :req_options and a replacement :client module), for passing them on to another call.

request(operation)

@spec request(operation()) :: :ok | {:ok, term()} | {:error, ApifyClient.Error.t()}

Performs a generated operation. Called by the generated ApifyClient.API.* functions; see the module documentation for options and return values.

stream(operation)

@spec stream(operation()) :: {:ok, Enumerable.t()} | {:error, ApifyClient.Error.t()}

Performs operation and returns the response body as a stream of chunks.

The request is made right away: errors (including the status of the response) come back as {:error, %ApifyClient.Error{}}. The chunks arrive in the caller's mailbox, so the stream must be consumed by the process that called this function. Telemetry completes once the response headers arrive.