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

headers() ::
  [{atom(), binary()}]
  | [{binary(), binary()}]
  | %{required(binary()) => binary()}
  | any()

Specs

method() :: :get | :post | :put | :patch | :delete | :options | :head

Specs

response() :: {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()}

Link to this section Functions

Link to this function

call(client, method, path, body \\ "", headers \\ [])

View Source

Specs

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 existing access_token
  • method will be the HTTP verb to use. Currently either :get or :post
  • path relative http path for the endpoint to call. This will be relative of the config definded :investec_open_api, :base_url
  • body (optional) - body to send with the http call
  • headers (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}}
Link to this function

prepare_headers(client, headers)

View Source

Specs

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

Link to this section Callbacks

Link to this callback

request(method, binary, any, headers, keyword)

View Source

Specs

request(method(), binary(), any(), headers(), keyword()) :: response()