Investec Open API v0.1.0 InvestecOpenApi.HTTP behaviour View Source
InvestecOpenApi.HTTP
This module is used to prepare for the http calls to the outside world.
It makes use of HTTPoison, but can easily be swopped out with a different HTTP client if required.
http_handler is defined in test config to use the InvestecOpenApi.HTTP.Faker module,
otherwise it will default to using InvestecOpenApi.HTTP.External.
Link to this section Summary
Functions
The main InvestecOpenApi.HTTP.call/5 method deals with preparing authentication headers,
and then making the call to the api endpoint over HTTP using config defined http_handler.
prepare_headers/2 understands how to add the Authorization header entry for different Api calls.
Some of the calls require basic-auth and others authenticate using a bearer-token.
Link to this section Types
Specs
Specs
method() :: :get | :post | :put | :patch | :delete | :options | :head
Specs
response() :: {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()}
Link to this section Functions
Specs
call(InvestecOpenApi.Client.t(), method(), binary(), String.t(), headers()) :: {InvestecOpenApi.Client.t(), response()}
The main InvestecOpenApi.HTTP.call/5 method deals with preparing authentication headers,
and then making the call to the api endpoint over HTTP using config defined http_handler.
Parameters:
%InvestecOpenApi.Client{}which has the details to start an authenticated session or us an existingaccess_tokenmethodwill be the HTTP verb to use. Currently either:getor:postpathrelative http path for the endpoint to call. This will be relative of the config definded:investec_open_api, :base_urlbody(optional) - body to send with the http callheaders(optional) - additional http headers required (over and above authentication headers)
Example
iex> {:ok, client} = InvestecOpenApi.Client.new()
...> {client, {:ok, %HTTPoison.Response{body: body}}} = InvestecOpenApi.HTTP.call(client, :get, "/za/pb/v1/accounts", "", [])
...> Jason.decode!(body)
%{"data" => %{"accounts" => [%{"accountId" => "172878438321553632224", "accountName" => "Mr John Doe", "accountNumber" => "10010206147", "productName" => "Private Bank Account", "referenceName" => "My Investec Private Bank Account"}]}, "links" => %{"self" => "https://openapi.investec.com/za/pb/v1/accounts"}, "meta" => %{"totalPages" => 1}} Specs
prepare_headers(InvestecOpenApi.Client.t(), headers()) :: {InvestecOpenApi.Client.t(), headers()}
prepare_headers/2 understands how to add the Authorization header entry for different Api calls.
Some of the calls require basic-auth and others authenticate using a bearer-token.
It will also check for validity of the access_token and if it is close to expiry (within 60 seconds)
then a new token will be requested - see InvestecOpenApi.Client.validate_access_token_still_valid/1 for more details