Investec Open API v0.1.0 InvestecOpenApi.Client View Source

This module deals with authenticating storing access_token between api calls.

Link to this section Summary

Functions

Authenticate returning a session that can be used for subsequent API calls

Automatically check if the access_token is expired or not. If it is expired, a new session will automatically be started.

Link to this section Types

Specs

t() :: %InvestecOpenApi.Client{
  access_token: binary(),
  client_id: binary(),
  client_secret: binary(),
  expires_at: integer(),
  method: atom()
}

Link to this section Functions

Link to this function

call_generate_token(client)

View Source

Specs

call_generate_token(t()) :: {t(), InvestecOpenApi.HTTP.response()}

Specs

new() :: {:error, any()} | {:ok, t()}

Authenticate returning a session that can be used for subsequent API calls

Methods:

  1. Using new/1 to use client_id and client_secret defined in application config.

Example

iex> {:ok, client} = InvestecOpenApi.Client.new()
...> client.access_token
"Ms9OsZkyrhBZd5yQJgfEtiDy4t2c"

Please note that it will raise error if client_id and client_secret is not found in your config.

  1. Using new(client_id, client_secret) to authenticate with specific credentials

Example

iex> {:ok, client} = InvestecOpenApi.Client.new("thisIsmysecretclientId", "verySuperSecureSecret")
...> client.access_token
"Ms9OsZkyrhBZd5yQJgfEtiDy4t2c"
  1. Using an existing %Client{} as parameter. This is useful when you want to re-authenticate when a session has expired.

Example

iex> {:ok, client} = InvestecOpenApi.Client.new()
...> assert client.access_token == "Ms9OsZkyrhBZd5yQJgfEtiDy4t2c"
...> {:ok, client} = InvestecOpenApi.Client.new(client)
...> client.access_token
"Ms9OsZkyrhBZd5yQJgfEtiDy4t2c"

Specs

new(t()) :: {:ok, t()} | {:error, any()}

Specs

new(binary(), binary()) :: {:error, any()} | {:ok, t()}
Link to this function

validate_access_token_still_valid(client)

View Source

Specs

validate_access_token_still_valid(t()) :: t()

Automatically check if the access_token is expired or not. If it is expired, a new session will automatically be started.

Example

iex> client = InvestecOpenApi.Client.validate_access_token_still_valid(%InvestecOpenApi.Client{
...>    access_token: "oldExpiredToken",
...>    client_id: "thisIsmysecretclientId",
...>    client_secret: "verySuperSecureSecret",
...>    expires_at: 0, # Simulating an expired token
...>    method: :token
...> })
...> refute client.access_token ==  "oldExpiredToken"
...> client_update = InvestecOpenApi.Client.validate_access_token_still_valid(client)
...> assert client_update.access_token == client.access_token
...> client.access_token
"Ms9OsZkyrhBZd5yQJgfEtiDy4t2c"