Skip to content

Commit ad0dd52

Browse files
committed
chore: improve client explanation guide
1 parent f4e6f28 commit ad0dd52

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

guides/explanations/0.client.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,47 @@ and responses—such as adding headers or handling authentication—while adapte
66
handle the underlying HTTP communication. For more details, see the sections on
77
[middleware](./2.middleware.md) and [adapters](./3.adapter.md).
88

9+
## Creating a Client
10+
11+
A client is created using `Tesla.client/2`, which takes a list of middleware
12+
and an adapter.
13+
14+
```elixir
15+
client = Tesla.client([Tesla.Middleware.PathParams, Tesla.Middleware.Logger])
16+
```
17+
18+
You can then use the client to make requests:
19+
20+
```elixir
21+
Tesla.get(client, "/users/123")
22+
```
23+
24+
### Passing Options to Middleware
25+
26+
You can pass options to middleware by registering the middleware as a tuple
27+
of two elements, where the first element is the middleware module and the
28+
second is the options.
29+
30+
```elixir
31+
client = Tesla.client(
32+
[{Tesla.Middleware.BaseUrl, "https://api.example.com"}]
33+
)
34+
```
35+
36+
### Passing Adapter
37+
38+
By default, the global adapter is used. You can override this by passing an
39+
adapter to the client.
40+
41+
```elixir
42+
client = Tesla.client([], Tesla.Adapter.Mint)
43+
```
44+
You can also pass options to the adapter.
45+
46+
```elixir
47+
client = Tesla.client([], {Tesla.Adapter.Mint, pool: :my_pool})
48+
```
49+
950
## Single-Client (Singleton) Pattern
1051

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

0 commit comments

Comments
 (0)