From ad0dd52840c98ea06f1626110ca78cf1b2dfa3d4 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Fri, 25 Oct 2024 00:05:05 -0400 Subject: [PATCH] chore: improve client explanation guide --- guides/explanations/0.client.md | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/guides/explanations/0.client.md b/guides/explanations/0.client.md index 8adc3e1a..f1d45758 100644 --- a/guides/explanations/0.client.md +++ b/guides/explanations/0.client.md @@ -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