# `ApifyClient`
[🔗](https://github.com/social-growth-studio/apify-client-ex/blob/v1.0.0/lib/apify_client.ex#L1)

Elixir client for the [Apify API](https://docs.apify.com/api/v2).

## Setup

Every call takes its connection settings as options: a prebuilt
`config: %ApifyClient.Config{}` or individual ones such as `token: "..."`.
Without a token, requests are sent unauthenticated, which works only for
public endpoints. Nothing is read from the environment.

The client doesn't read the application environment for these; build the
configuration from your own:

    defmodule MyApp.Apify do
      def config do
        ApifyClient.Config.new(token: Application.fetch_env!(:my_app, :apify_token))
      end
    end

    ApifyClient.Runs.get(run_id, config: MyApp.Apify.config())

See `ApifyClient.Config` for the settings and `ApifyClient.Client` for the
options every call accepts.

## Usage

The modules directly under `ApifyClient` cover the common tasks:

  * `ApifyClient.Actors` - start Actors, optionally waiting for the run; build them
  * `ApifyClient.Tasks` - start saved Actor tasks, optionally waiting for the run
  * `ApifyClient.Runs` - get, wait for and abort runs
  * `ApifyClient.Builds` - get, wait for and abort builds
  * `ApifyClient.Datasets` - list, stream and push dataset items
  * `ApifyClient.KeyValueStores` - get, set, delete and stream records and keys
  * `ApifyClient.Logs` - get and stream run and build logs
  * `ApifyClient.Pagination` - stream every item of any paginated list endpoint

```elixir
{:ok, run} =
  ApifyClient.Actors.call("apify/web-scraper", %{
    "startUrls" => [%{"url" => "https://example.com"}]
  })

if ApifyClient.Runs.succeeded?(run) do
  items = ApifyClient.Datasets.stream_items(run.default_dataset_id) |> Enum.to_list()
end
```

The whole API is available under `ApifyClient.API`, one module per resource,
generated from Apify's OpenAPI description. Its functions are named after the
URL path:

| Request                                  | Function                                   |
| ---------------------------------------- | ------------------------------------------ |
| `GET /v2/actor-runs/{runId}`             | `ApifyClient.API.Runs.get/2`               |
| `POST /v2/actor-runs/{runId}/abort`      | `ApifyClient.API.Runs.abort/2`             |
| `GET /v2/actors/{actorId}/runs`          | `ApifyClient.API.Actors.Runs.list/2`       |
| `PUT /v2/key-value-stores/{id}/records/{key}` | `ApifyClient.API.KeyValueStores.Records.update/4` |
| `GET /v2/users/me`                       | `ApifyClient.API.Users.Me.get/1`           |

Responses are decoded into the structs in `ApifyClient.Schemas`, with
snake_case fields:

    {:ok, %ApifyClient.Schemas.Run{status: "SUCCEEDED", default_dataset_id: id}} =
      ApifyClient.API.Runs.get(run_id)

Errors are `{:error, %ApifyClient.Error{}}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
