Tornex.API (Tornex v0.3.0)

View Source

Core API functionality.

Tornex.API provides the core Torn API-related functionality to make the API calls.

Configuration

Tornex uses compile-time configuration for certain API-related functionality:

  • base_url (default: "https://api.torn.com") => base of the URL the remainder of the request URL is built off of
  • comment (default: "tex/0.3.0") => Comment used in the API calls
  • client (default: Tornex.HTTP.FinchClient) => HTTP client to use in API calls

To customize one of these, include the following in a config/*.exs:

config :tornex,
  base_url: "https://api.torn.com",
  comment: "Tornium",

Summary

Functions

Performs a blocking HTTP GET request against the Torn API for a Tornex.Query or Tornex.SpecQuery.

Converts a Tornex.Query or Tornex.SpecQuery to the URL required to make the HTTP request.

torn_get(query) deprecated

Performs a blocking HTTP GET request against the Torn API for a Tornex.Query or Tornex.SpecQuery.

Types

error()

@type error() ::
  {:error,
   :cf_challenge | :unknown | :content_type | Jason.DecodeError.t() | term()}

return()

@type return() :: list() | map()

Functions

get(query)

@spec get(Tornex.Query.t()) :: map() | list() | error()
@spec get(Tornex.SpecQuery.t()) :: binary() | error()

Performs a blocking HTTP GET request against the Torn API for a Tornex.Query or Tornex.SpecQuery.

Examples

iex> query = %Tornex.Query{resource: "user", resource_id: 1, key: "", key_owner: 1, nice: 0}
iex> Tornex.API.get(query)
...

query_to_url(query)

@spec query_to_url(Tornex.Query.t() | Tornex.SpecQuery.t()) :: String.t()

Converts a Tornex.Query or Tornex.SpecQuery to the URL required to make the HTTP request.

iex> query = %Tornex.Query{
...>   resource: "user",
...>   resource_id: 1,
...>   key: "apikey",
...>   key_owner: "apikey",
...>   nice: 10,
...>   selections: ["basic", "discord"]
...> }
iex> Tornex.API.query_to_url(query)
"https://api.torn.com/user/1?selections=basic,discord&key=apikey&comment=tex-#{Mix.Project.config()[:version]}"

iex> spec_query =
...>   Tornex.SpecQuery.new()
...>   |> Tornex.SpecQuery.put_path(Torngen.Client.Path.Faction.Id.Basic)
...>   |> Tornex.SpecQuery.put_path(Torngen.Client.Path.Faction.Id.Chains)
...>   |> Tornex.SpecQuery.put_parameter(:id, 89)
...>   |> Tornex.SpecQuery.put_parameter(:limit, 100)
...>   |> Tornex.SpecQuery.put_key("apikey")
iex> Tornex.API.query_to_url(spec_query)
"https://api.torn.com/v2/faction/89/?selections=chains,basic&limit=100&comment=tex-#{Mix.Project.config()[:version]}"

torn_get(query)

This function is deprecated. Use `get/1` instead.
@spec torn_get(Tornex.Query.t()) :: map() | list() | error()
@spec torn_get(Tornex.SpecQuery.t()) :: term() | error()

Performs a blocking HTTP GET request against the Torn API for a Tornex.Query or Tornex.SpecQuery.

Examples

iex> query = %Tornex.Query{resource: "user", resource_id: 1, key: "", key_owner: 1, nice: 0}
iex> Tornex.API.torn_get(query)
...