Current section
Files
Jump to
Current section
Files
lib/teac/api.ex
defmodule Teac.Api do
@moduledoc false
@type broadcaster_ids :: [integer()]
@type token :: String.t()
@type client_id :: String.t() | nil
@type ok :: {:ok, map()}
@type error :: {:error, Teac.Error.t()}
def uri(path) do
Teac.api_uri() <> path
end
def headers(%Teac.Client{token: token, client_id: client_id}), do: headers(token, client_id)
def headers(token, client_id) do
[{"Authorization", "Bearer #{token}"}, {"Client-Id", client_id}]
end
def handle_response(rsp) do
case rsp do
%Req.Response{status: s, body: %{"data" => data}} when s in [200, 201, 202] -> {:ok, data}
%Req.Response{status: 204} -> {:ok, nil}
rsp -> {:error, Teac.Error.from_response(rsp)}
end
end
def handle_paginated_response(rsp) do
case rsp do
%Req.Response{status: s, body: %{"data" => data} = body} when s in [200, 201, 202] ->
cursor = get_in(body, ["pagination", "cursor"])
{:ok, data, cursor}
rsp ->
{:error, Teac.Error.from_response(rsp)}
end
end
end