Skip to content

Commit fc0ba35

Browse files
author
Maneesh
committed
Added Support for API
1 parent 7370c44 commit fc0ba35

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule KiteConnectEx.Instrument do
2+
@moduledoc """
3+
Module to represent a user's portfolio
4+
"""
5+
6+
alias KiteConnectEx.Portfolio.Holding
7+
alias KiteConnectEx.Request
8+
9+
@instruments_base_path "/instruments"
10+
11+
@doc """
12+
Get user's portfolio `holdings`
13+
14+
## Example
15+
16+
{:ok, holdings} = KiteConnectEx.Portfolio.holdings("access-token")
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(instruments_path(exchange), nil, auth_header(access_token), KiteConnectEx.request_options())
21+
|> case do
22+
{:ok, instruments_csv_dump} ->
23+
{:ok, instruments_csv_dump}
24+
25+
{:error, error} ->
26+
{:error, error}
27+
end
28+
end
29+
30+
defp instruments_path(exchange) do
31+
@instruments_base_path <> "/" <> exchange
32+
end
33+
34+
defp auth_header(access_token) do
35+
[{"Authorization", "token " <> KiteConnectEx.api_key() <> ":" <> access_token}]
36+
end
37+
end

lib/kite_connect_ex/request.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ defmodule KiteConnectEx.Request do
1818
@spec api_version() :: String.t()
1919
def api_version, do: @api_version
2020

21+
@spec get_csv(String.t(), map() | nil, list, list) :: Response.success() | Response.error()
22+
def get_csv(path, query, headers, opts \\ []) do
23+
HTTPoison.get(
24+
build_url(path, query),
25+
build_headers(headers),
26+
request_options(opts)
27+
)
28+
|> parse_csv_response()
29+
end
30+
2131
@spec get(String.t(), map() | nil, list, list) :: Response.success() | Response.error()
2232
def get(path, query, headers, opts \\ []) do
2333
HTTPoison.get(
@@ -107,6 +117,10 @@ defmodule KiteConnectEx.Request do
107117
Response.parse_response(response)
108118
end
109119

120+
defp parse_csv_response({:ok, response}) do
121+
Response.parse_csv_response(response)
122+
end
123+
110124
defp parse_response({:error, error}) do
111125
Response.parse_error(error)
112126
end

lib/kite_connect_ex/response.ex

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

10+
@doc false
11+
@spec parse_csv_response(HTTPoison.Response.t()) :: success | error
12+
def parse_csv_response(%HTTPoison.Response{} = response) do
13+
case response do
14+
%{body: body, status_code: status} when status in @success_status_codes ->
15+
{:ok, body}
16+
17+
%{body: body, status_code: _status} ->
18+
{:error, KiteConnectEx.Error.new(body)}
19+
end
20+
end
21+
1022
@doc false
1123
@spec parse_response(HTTPoison.Response.t()) :: success | error
1224
def parse_response(%HTTPoison.Response{} = response) do
1325
case response do
1426
%{body: body, status_code: status} when status in @success_status_codes ->
15-
body =
16-
body
17-
|> Jason.decode!()
18-
|> parse_body()
19-
2027
{:ok, body}
2128

2229
%{body: body, status_code: _status} ->

0 commit comments

Comments
 (0)