Skip to content

Commit

Permalink
chore: improve client explanation guide
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Oct 25, 2024
1 parent f4e6f28 commit ad0dd52
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions guides/explanations/0.client.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@ and responses—such as adding headers or handling authentication—while adapte
handle the underlying HTTP communication. For more details, see the sections on
[middleware](./2.middleware.md) and [adapters](./3.adapter.md).

## Creating a Client

A client is created using `Tesla.client/2`, which takes a list of middleware
and an adapter.

```elixir
client = Tesla.client([Tesla.Middleware.PathParams, Tesla.Middleware.Logger])
```

You can then use the client to make requests:

```elixir
Tesla.get(client, "/users/123")
```

### Passing Options to Middleware

You can pass options to middleware by registering the middleware as a tuple
of two elements, where the first element is the middleware module and the
second is the options.

```elixir
client = Tesla.client(
[{Tesla.Middleware.BaseUrl, "https://api.example.com"}]
)
```

### Passing Adapter

By default, the global adapter is used. You can override this by passing an
adapter to the client.

```elixir
client = Tesla.client([], Tesla.Adapter.Mint)
```
You can also pass options to the adapter.

```elixir
client = Tesla.client([], {Tesla.Adapter.Mint, pool: :my_pool})
```

## Single-Client (Singleton) Pattern

A common approach in applications is to encapsulate client creation within a
Expand Down

0 comments on commit ad0dd52

Please sign in to comment.