Tornex.API (Tornex v0.3.0)
View SourceCore 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 ofcomment
(default: "tex/0.3.0") => Comment used in the API callsclient
(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.
Performs a blocking HTTP GET request against the Torn API for a Tornex.Query
or Tornex.SpecQuery
.
Types
@type error() :: {:error, :cf_challenge | :unknown | :content_type | Jason.DecodeError.t() | term()}
Functions
@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)
...
@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]}"
@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)
...