@@ -8,6 +8,49 @@ defmodule KiteConnectEx.User do
8
8
alias KiteConnectEx.Request
9
9
10
10
@ login_url "https://kite.zerodha.com/connect/login"
11
+ @ create_session_path "/session/token"
12
+
13
+ @ keys [
14
+ access_token: nil ,
15
+ api_key: nil ,
16
+ avatar_url: nil ,
17
+ broker: nil ,
18
+ email: nil ,
19
+ exchanges: nil ,
20
+ login_time: nil ,
21
+ meta: nil ,
22
+ order_types: nil ,
23
+ products: nil ,
24
+ public_token: nil ,
25
+ refresh_token: nil ,
26
+ silo: nil ,
27
+ user_id: nil ,
28
+ user_name: nil ,
29
+ user_shortname: nil ,
30
+ user_type: nil
31
+ ]
32
+
33
+ @ type t :: % __MODULE__ {
34
+ access_token: String . t ( ) ,
35
+ api_key: String . t ( ) ,
36
+ avatar_url: String . t ( ) ,
37
+ broker: String . t ( ) ,
38
+ email: String . t ( ) ,
39
+ exchanges: List . t ( ) ,
40
+ login_time: String . t ( ) ,
41
+ meta: map ( ) ,
42
+ order_types: List . t ( ) ,
43
+ products: List . t ( ) ,
44
+ public_token: String . t ( ) ,
45
+ refresh_token: String . t ( ) ,
46
+ silo: String . t ( ) ,
47
+ user_id: String . t ( ) ,
48
+ user_name: String . t ( ) ,
49
+ user_shortname: String . t ( ) ,
50
+ user_type: String . t ( )
51
+ }
52
+
53
+ defstruct [ :original_json | @ keys ]
11
54
12
55
@ doc """
13
56
Get KiteConnect login endpoint
@@ -27,4 +70,70 @@ defmodule KiteConnectEx.User do
27
70
defp api_key do
28
71
Application . get_env ( :kite_connect_ex , :api_key )
29
72
end
73
+
74
+ @ doc """
75
+ Generate `access_token` using the `request_token`
76
+
77
+ ## Example
78
+
79
+ {:ok, user_profile} = KiteConnectEx.User.create_session("request-token")
80
+ """
81
+ @ spec create_session ( String . t ( ) ) :: { :ok , % __MODULE__ { } } | Response . error ( )
82
+ def create_session ( request_token ) when is_binary ( request_token ) do
83
+ params = % {
84
+ api_key: api_key ( ) ,
85
+ request_token: request_token ,
86
+ checksum: generate_checksum ( request_token )
87
+ }
88
+
89
+ Request . post ( @ create_session_path , params , [ ] , request_options ( ) )
90
+ |> case do
91
+ { :ok , user_data } ->
92
+ user = % __MODULE__ { new ( user_data ) | original_json: user_data }
93
+
94
+ { :ok , user }
95
+
96
+ { :error , error , status } ->
97
+ { :error , error , status }
98
+ end
99
+ end
100
+
101
+ defp generate_checksum ( request_token ) do
102
+ :crypto . hash ( :sha256 , api_key ( ) <> request_token <> api_secret ( ) )
103
+ |> Base . encode16 ( case: :lower )
104
+ end
105
+
106
+ defp api_secret do
107
+ Application . get_env ( :kite_connect_ex , :api_secret )
108
+ end
109
+
110
+ defp request_options do
111
+ Application . get_env ( :kite_connect_ex , :request_options , [ ] )
112
+ end
113
+
114
+ defp new ( attributes ) when is_map ( attributes ) do
115
+ struct ( __MODULE__ , % {
116
+ access_token: attributes [ "access_token" ] ,
117
+ api_key: attributes [ "api_key" ] ,
118
+ avatar_url: attributes [ "avatar_url" ] ,
119
+ broker: attributes [ "broker" ] ,
120
+ email: attributes [ "email" ] ,
121
+ exchanges: attributes [ "exchanges" ] ,
122
+ login_time: attributes [ "login_time" ] ,
123
+ meta: attributes [ "meta" ] ,
124
+ order_types: attributes [ "order_types" ] ,
125
+ products: attributes [ "products" ] ,
126
+ public_token: attributes [ "public_token" ] ,
127
+ refresh_token: attributes [ "refresh_token" ] ,
128
+ silo: attributes [ "silo" ] ,
129
+ user_id: attributes [ "user_id" ] ,
130
+ user_name: attributes [ "user_name" ] ,
131
+ user_shortname: attributes [ "user_shortname" ] ,
132
+ user_type: attributes [ "user_type" ]
133
+ } )
134
+ end
135
+
136
+ defp new ( _ ) do
137
+ struct ( __MODULE__ , % { } )
138
+ end
30
139
end
0 commit comments