View Source JikanEx.Base (JikanEx v0.1.6)

Creates the Tesla client for Jikan. Encodes/Decodes the JSON response. Called from JikanEx.client/1

Summary

Functions

Perform a GET request.

Perform a GET request.

Get the Base Url from a Tesla.Client

Creates a JikanEx Client. Takes a keyword list as first argument. If the base_url key is missing, uses the default (api.jikan.moe)

Perform request and raise in case of error.

Types

option()

@type option() ::
  {:method, Tesla.Env.method()}
  | {:url, Tesla.Env.url()}
  | {:query, Tesla.Env.query()}
  | {:headers, Tesla.Env.headers()}
  | {:body, Tesla.Env.body()}
  | {:opts, Tesla.Env.opts()}

Functions

get(client, url, opts)

Perform a GET request.

See request/1 or request/2 for options definition.

get("/users")
get("/users", query: [scope: "admin"])
get(client, "/users")
get(client, "/users", query: [scope: "admin"])

get!(client, url, opts)

@spec get!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a GET request.

See request!/1 or request!/2 for options definition.

get!("/users")
get!("/users", query: [scope: "admin"])
get!(client, "/users")
get!(client, "/users", query: [scope: "admin"])

get_base_url(client)

Get the Base Url from a Tesla.Client

Example

iex> client = JikanEx.client()
iex> JikanEx.Base.get_base_url(client)
"https://api.jikan.moe/v3/"
iex> client = JikanEx.client([base_url: "http://localhost:25039/v3/"])
iex> JikanEx.Base.get_base_url(client)
"http://localhost:25039/v3/"

new(options \\ [])

Creates a JikanEx Client. Takes a keyword list as first argument. If the base_url key is missing, uses the default (api.jikan.moe)

These client that's returned is passed to each request in JikanEx.Request.

Returns Tesla.Client

Examples

iex> JikanEx.Base.new([])
iex> JikanEx.Base.new()
%Tesla.Client{
  adapter: nil,
  fun: nil,
  post: [],
  pre: [
    {Tesla.Middleware.BaseUrl, :call, ["https://api.jikan.moe/v3/"]}
  ]
}
iex> JikanEx.Base.new([base_url: "http://localhost:8000/v3/"])
%Tesla.Client{
  adapter: nil,
  fun: nil,
  post: [],
  pre: [
    {Tesla.Middleware.BaseUrl, :call, ["http://localhost:8000/v3/"]}
  ]
}

request(client \\ %Tesla.Client{}, options)

@spec request(Tesla.Env.client(), [option()]) :: Tesla.Env.result()

Perform a request.

Options

  • :method - the request method, one of [:head, :get, :delete, :trace, :options, :post, :put, :patch]
  • :url - either full url e.g. "http://example.com/some/path" or just "/some/path" if using Tesla.Middleware.BaseUrl
  • :query - a keyword list of query params, e.g. [page: 1, per_page: 100]
  • :headers - a keyworld list of headers, e.g. [{"content-type", "text/plain"}]
  • :body - depends on used middleware:
    • by default it can be a binary
    • if using e.g. JSON encoding middleware it can be a nested map
    • if adapter supports it it can be a Stream with any of the above
  • :opts - custom, per-request middleware or adapter options

Examples

ExampleApi.request(method: :get, url: "/users/path")

# use shortcut methods
ExampleApi.get("/users/1")
ExampleApi.post(client, "/users", %{name: "Jon"})

request!(client \\ %Tesla.Client{}, options)

@spec request!(Tesla.Env.client(), [option()]) :: Tesla.Env.t() | no_return()

Perform request and raise in case of error.

This is similar to request/2 behaviour from Tesla 0.x

See request/2 for list of available options.