CPPKiteConnect
user.hpp
1 /*
2  * Licensed under the MIT License <http://opensource.org/licenses/MIT>.
3  * SPDX-License-Identifier: MIT
4  *
5  * Copyright (c) 2020-2022 Bhumit Attarde
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
22  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23  * USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <string>
29 
30 #include "../utils.hpp"
31 #include "rapidjson/include/rapidjson/document.h"
32 #include "rapidjson/include/rapidjson/rapidjson.h"
33 
34 namespace kiteconnect {
35 
36 using std::string;
37 namespace rj = rapidjson;
38 namespace kc = kiteconnect;
39 namespace utils = kc::internal::utils;
40 
42 struct userProfile {
43  userProfile() = default;
44  explicit userProfile(const rj::Value::Object& val) { parse(val); };
45 
46  void parse(const rj::Value::Object& val) {
47  userID = utils::json::get<string>(val, "user_id");
48  userName = utils::json::get<string>(val, "user_name");
49  userShortName = utils::json::get<string>(val, "user_shortname");
50  avatarURL = utils::json::get<string>(val, "avatar_url");
51  userType = utils::json::get<string>(val, "user_type");
52  email = utils::json::get<string>(val, "email");
53  broker = utils::json::get<string>(val, "broker");
54  products = utils::json::get<std::vector<string>>(val, "products");
55  orderTypes = utils::json::get<std::vector<string>>(val, "order_types");
56  exchanges = utils::json::get<std::vector<string>>(val, "exchanges");
57 
58  meta = utils::json::get<utils::json::JsonObject, Meta>(val, "meta");
59  };
60 
61  string userID;
62  string userName;
63  string userShortName;
64  string avatarURL;
65  string userType;
66  string email;
67  string broker;
68  std::vector<string> products;
69  std::vector<string> orderTypes;
70  std::vector<string> exchanges;
71  struct Meta {
72  Meta() = default;
73  explicit Meta(const rj::Value::Object& val) { parse(val); };
74 
75  void parse(const rj::Value::Object& val) {
76  dematConsent = utils::json::get<string>(val, "demat_consent");
77  }
78 
79  string dematConsent;
80  } meta;
81 };
82 
84 struct userTokens {
85  userTokens() = default;
86  explicit userTokens(const rj::Value::Object& val) { parse(val); };
87 
88  void parse(const rj::Value::Object& val) {
89  userID = utils::json::get<string>(val, "user_id");
90  accessToken = utils::json::get<string>(val, "access_token");
91  refreshToken = utils::json::get<string>(val, "refresh_token");
92  };
93 
94  string userID;
95  string accessToken;
96  string refreshToken;
97 };
98 
100 struct userSession {
101  userSession() = default;
102  explicit userSession(const rj::Value::Object& val) { parse(val); };
103 
104  void parse(const rj::Value::Object& val) {
105  profile.parse(val);
106  tokens.parse(val);
107  apiKey = utils::json::get<string>(val, "api_key");
108  publicToken = utils::json::get<string>(val, "public_token");
109  loginTime = utils::json::get<string>(val, "login_time");
110  };
111 
112  string apiKey;
113  string publicToken;
114  string loginTime;
115  userProfile profile;
116  userTokens tokens;
117 };
118 
121  availableMargins() = default;
122  explicit availableMargins(const rj::Value::Object& val) { parse(val); };
123 
124  void parse(const rj::Value::Object& val) {
125  adHocMargin = utils::json::get<double>(val, "adhoc_margin");
126  cash = utils::json::get<double>(val, "cash");
127  collateral = utils::json::get<double>(val, "collateral");
128  intradayPayin = utils::json::get<double>(val, "intraday_payin");
129  };
130 
131  double adHocMargin = -1;
132  double cash = -1;
133  double collateral = -1;
134  double intradayPayin = -1;
135 };
136 
138 struct usedMargins {
139  usedMargins() = default;
140  explicit usedMargins(const rj::Value::Object& val) { parse(val); };
141 
142  void parse(const rj::Value::Object& val) {
143  debits = utils::json::get<double>(val, "debits");
144  exposure = utils::json::get<double>(val, "exposure");
145  M2MRealised = utils::json::get<double>(val, "m2m_realised");
146  M2MUnrealised = utils::json::get<double>(val, "m2m_unrealised");
147  optionPremium = utils::json::get<double>(val, "option_premium");
148  payout = utils::json::get<double>(val, "payout");
149  span = utils::json::get<double>(val, "span");
150  holdingSales = utils::json::get<double>(val, "holding_sales");
151  turnover = utils::json::get<double>(val, "turnover");
152  };
153 
154  double debits = -1;
155  double exposure = -1;
156  double M2MRealised = -1;
157  double M2MUnrealised = -1;
158  double optionPremium = -1;
159  double payout = -1;
160  double span = -1;
161  double holdingSales = -1;
162  double turnover = -1;
163 };
164 
166 struct margins {
167  margins() = default;
168  explicit margins(const rj::Value::Object& val) { parse(val); };
169 
170  void parse(const rj::Value::Object& val) {
171  enabled = utils::json::get<bool>(val, "enabled");
172  net = utils::json::get<double>(val, "net");
173  available = utils::json::get<utils::json::JsonObject, availableMargins>(
174  val, "available");
175  used = utils::json::get<utils::json::JsonObject, usedMargins>(
176  val, "utilised");
177  };
178 
179  double net = -1;
180  bool enabled = false;
181  availableMargins available;
182  usedMargins used;
183 };
184 
186 struct allMargins {
187  allMargins() = default;
188  explicit allMargins(const rj::Value::Object& val) { parse(val); };
189 
190  void parse(const rj::Value::Object& val) {
191  equity =
192  utils::json::get<utils::json::JsonObject, margins>(val, "equity");
193  commodity = utils::json::get<utils::json::JsonObject, margins>(
194  val, "commodity");
195  };
196 
197  margins equity;
198  margins commodity;
199 };
200 
201 } // namespace kiteconnect
kiteconnect::allMargins
Represents all margins (equity and commodity).
Definition: user.hpp:186
kiteconnect::userTokens
Represents tokens received after successful authentication.
Definition: user.hpp:84
kiteconnect::margins
Represents user margins for a segment.
Definition: user.hpp:166
kiteconnect::usedMargins
Represents used margins of a single segment.
Definition: user.hpp:138
kiteconnect::userProfile::Meta
Definition: user.hpp:71
kiteconnect::userProfile
Represents an user's profile.
Definition: user.hpp:42
kiteconnect::availableMargins
Represents available margins of a single segment.
Definition: user.hpp:120
kiteconnect::userSession
Represents information of a user session.
Definition: user.hpp:100