TinkoffInvest.Api (tinkoff_invest v0.2.1) View Source

This module provides two simple requests: GET and POST

payload map converted to query string on request

You will need to define your custom TinkoffInvest.Model to make this work or use existing one.

Examples:

TinkoffInvest.Api.request("/orders", :get, YourCustomModel)
TinkoffInvest.Api.request("/orders", :get, YourCustomModel, %{someQueryParam: true}) # /orders?someParam=true
TinkoffInvest.Api.request("/orders", :post, YourCustomModel, %{someQueryParam: true})
TinkoffInvest.Api.request("/orders", :post, YourCustomModel, %{someQueryParam: true}, %{bodyParam: true})

Please notice that :post request accepts both query and body payloads preferably as maps

Link to this section Summary

Functions

Build body payload and encodes it to JSON if needed.

Builds query payload from map. Account id provided by default in config though can be overridden

Allows you to send request to api if you need custom method that is not currently implemented

Transforms body response and encodes it to TinkoffInvest.Model.Api.Response

Link to this section Types

Specs

method() :: :get | :post

Link to this section Functions

Link to this function

build_body_payload(payload)

View Source

Specs

build_body_payload(map() | nil | String.t()) :: String.t()

Build body payload and encodes it to JSON if needed.

iex>TinkoffInvest.Api.build_body_payload(nil)
""

iex>TinkoffInvest.Api.build_body_payload("[123]")
"[123]"

iex>TinkoffInvest.Api.build_body_payload(%{myField: true})
"{\"myField\":true}"
Link to this function

build_payload(path, payload)

View Source

Specs

build_payload(String.t(), map() | nil) :: String.t()

Builds query payload from map. Account id provided by default in config though can be overridden

Examples

iex>TinkoffInvest.change_account_id("123")
:ok
iex>TinkoffInvest.Api.build_payload("/orders", %{myQueryParam: true, someOtherParam: 2})
"/orders?brokerAccountId=123&myQueryParam=true&someOtherParam=2"

You can override broker account id:

iex>TinkoffInvest.Api.build_payload("/orders", %{brokerAccountId: "SB1111", myQueryParam: true, someOtherParam: 2})
"/orders?brokerAccountId=SB1111&myQueryParam=true&someOtherParam=2"
Link to this function

request(path, method, module, queryPayload \\ nil, body \\ %{})

View Source

Specs

request(String.t(), method(), module(), map() | nil, map() | nil) ::
  TinkoffInvest.Model.Api.Response.t()

Allows you to send request to api if you need custom method that is not currently implemented

Transforms body response and encodes it to TinkoffInvest.Model.Api.Response

iex>TinkoffInvest.Api.to_response(%HTTPoison.Response{body: "SOME_ERROR", status_code: 404, request: %HTTPoison.Request{url: "/orders"}})
%TinkoffInvest.Model.Api.Response{payload: %{"code" => nil, "message" => "SOME_ERROR"}, request_url: "/orders", status: nil, status_code: 404, tracking_id: nil}

iex>TinkoffInvest.Api.to_response(%HTTPoison.Response{body: nil, status_code: 404, request: %HTTPoison.Request{url: "/orders"}})
%TinkoffInvest.Model.Api.Response{payload: %{"code" => nil, "message" => nil}, request_url: "/orders", status: nil, status_code: 404, tracking_id: nil}

iex>TinkoffInvest.Api.to_response(%HTTPoison.Response{body: %{"payload" => %{"code" => "SOME_ERR", "message" => "Well, error"}}, status_code: 404, request: %HTTPoison.Request{url: "/orders"}})
%TinkoffInvest.Model.Api.Response{payload: %{"code" => "SOME_ERR", "message" => "Well, error"}, request_url: "/orders", status: nil, status_code: 404, tracking_id: nil}