Skip to content

Commit

Permalink
Add TeslaClient implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraBeatris committed Oct 21, 2023
1 parent 317bc58 commit 4576525
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/workos/client/tesla_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@ defmodule WorkOs.Client.TeslaClient do
Tesla client for WorkOs. This is the default HTTP client used.
"""
@behaviour WorkOs.Client

@doc """
Sends a request to a WorkOs API endpoint, given list of request opts.
"""
@spec request(Resend.Client.t(), Keyword.t()) ::
{:ok, %{body: map(), status: pos_integer()}} | {:error, any()}
def request(client, opts) do
opts = Keyword.take(opts, [:method, :url, :query, :headers, :body, :opts])
Tesla.request(new(client), opts)
end

@doc """
Returns a new `Tesla.Client`, configured for calling the WorkOs API.
"""
@spec new(Resend.Client.t()) :: Tesla.Client.t()
def new(client) do
Tesla.client([
Tesla.Middleware.Logger,
{Tesla.Middleware.BaseUrl, client.base_url},
Tesla.Middleware.PathParams,
Tesla.Middleware.JSON,
{Tesla.Middleware.Headers, [{"Authorization", "Bearer #{client.api_key}"}]}
])
end
end

0 comments on commit 4576525

Please sign in to comment.