File tree Expand file tree Collapse file tree 3 files changed +81
-13
lines changed Expand file tree Collapse file tree 3 files changed +81
-13
lines changed Original file line number Diff line number Diff line change 1
1
defmodule KiteConnectEx do
2
- alias KiteConnectEx . { User , Response , Portfolio }
2
+ alias KiteConnectEx . { User , Response , Portfolio , Instrument }
3
3
4
4
@ api_url "https://api.kite.trade"
5
5
@@ -84,4 +84,14 @@ defmodule KiteConnectEx do
84
84
"""
85
85
@ spec holdings ( String . t ( ) ) :: { :ok , List . t ( ) } | Response . error ( )
86
86
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
87
97
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -7,28 +7,44 @@ defmodule KiteConnectEx.Response do
7
7
@ success_status "success"
8
8
@ error_status "error"
9
9
10
+
11
+ @ csv_content_type { "Content-Type" , "text/csv" }
12
+
10
13
@ doc false
11
14
@ spec parse_response ( HTTPoison.Response . t ( ) ) :: success | error
12
15
def parse_response ( % HTTPoison.Response { } = response ) do
13
16
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 ) }
19
19
20
- { :ok , body }
20
+ % { body: body , headers: headers , status_code: _status } ->
21
+ { :error , do_parse_response ( headers , body ) }
22
+ end
23
+ end
21
24
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
27
30
28
- { :error , error }
31
+ _ ->
32
+ body
33
+ |> Jason . decode! ( )
34
+ |> parse_body ( )
29
35
end
30
36
end
31
37
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
+
32
48
@ doc false
33
49
@ spec parse_error ( HTTPoison.Error . t ( ) ) :: error
34
50
def parse_error ( % HTTPoison.Error { } = error ) do
You can’t perform that action at this time.
0 commit comments