Skip to content

Commit d284829

Browse files
authored
Merge pull request #1 from scripbox/feature/add-instruments-api
Add Support for Instructions API
2 parents 7370c44 + f7f37a5 commit d284829

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

lib/kite_connect_ex.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule KiteConnectEx do
2-
alias KiteConnectEx.{User, Response, Portfolio}
2+
alias KiteConnectEx.{User, Response, Portfolio, Instrument}
33

44
@api_url "https://api.kite.trade"
55

@@ -84,4 +84,14 @@ defmodule KiteConnectEx do
8484
"""
8585
@spec holdings(String.t()) :: {:ok, List.t()} | Response.error()
8686
defdelegate holdings(access_token), to: Portfolio
87+
88+
@doc """
89+
Get list of all tradable `instruments` by `exchange`
90+
91+
## Example
92+
93+
{:ok, instruments} = KiteConnectEx.instruments("access-token")
94+
"""
95+
@spec instruments(String.t(), String.t()) :: {:ok, List.t()} | Response.error()
96+
defdelegate instruments(access_token, exchange), to: Instrument
8797
end

lib/kite_connect_ex/instrument.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule KiteConnectEx.Instrument do
2+
@moduledoc """
3+
Module to retrieve a list of tradable `instruments`
4+
"""
5+
6+
alias KiteConnectEx.Portfolio.Holding
7+
alias KiteConnectEx.Request
8+
9+
@instruments_base_path "/instruments"
10+
11+
@doc """
12+
Get all tradable `instruments` by `exchange`
13+
14+
## Example
15+
16+
{:ok, instruments} = KiteConnectEx.Portfolio.instruments("access-token", "NSE")
17+
"""
18+
@spec instruments(String.t(), String.t()) :: {:ok, List.t()} | Response.error()
19+
def instruments(access_token, exchange) when is_binary(access_token) do
20+
Request.get(
21+
instruments_path(exchange),
22+
nil,
23+
auth_header(access_token),
24+
KiteConnectEx.request_options()
25+
)
26+
|> case do
27+
{:ok, instruments_csv_dump} ->
28+
{:ok, instruments_csv_dump}
29+
30+
{:error, error} ->
31+
{:error, error}
32+
end
33+
end
34+
35+
defp instruments_path(exchange) do
36+
@instruments_base_path <> "/" <> exchange
37+
end
38+
39+
defp auth_header(access_token) do
40+
[{"Authorization", "token " <> KiteConnectEx.api_key() <> ":" <> access_token}]
41+
end
42+
end

lib/kite_connect_ex/response.ex

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,44 @@ defmodule KiteConnectEx.Response do
77
@success_status "success"
88
@error_status "error"
99

10+
11+
@csv_content_type {"Content-Type", "text/csv"}
12+
1013
@doc false
1114
@spec parse_response(HTTPoison.Response.t()) :: success | error
1215
def parse_response(%HTTPoison.Response{} = response) do
1316
case response do
14-
%{body: body, status_code: status} when status in @success_status_codes ->
15-
body =
16-
body
17-
|> Jason.decode!()
18-
|> parse_body()
17+
%{body: body, headers: headers, status_code: status} when status in @success_status_codes ->
18+
{:ok, do_parse_response(headers, body)}
1919

20-
{:ok, body}
20+
%{body: body, headers: headers, status_code: _status} ->
21+
{:error, do_parse_response(headers, body)}
22+
end
23+
end
2124

22-
%{body: body, status_code: _status} ->
23-
error =
24-
body
25-
|> Jason.decode!()
26-
|> parse_body()
25+
defp do_parse_response(headers, body) do
26+
has_csv_header?(headers)
27+
|> case do
28+
true ->
29+
body
2730

28-
{:error, error}
31+
_ ->
32+
body
33+
|> Jason.decode!()
34+
|> parse_body()
2935
end
3036
end
3137

38+
defp has_csv_header?([]), do: false
39+
40+
defp has_csv_header?([header | _tail]) when header == @csv_content_type do
41+
true
42+
end
43+
44+
defp has_csv_header?([_header | tail]) do
45+
has_csv_header?(tail)
46+
end
47+
3248
@doc false
3349
@spec parse_error(HTTPoison.Error.t()) :: error
3450
def parse_error(%HTTPoison.Error{} = error) do

0 commit comments

Comments
 (0)