CPPKiteConnect
internal.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 <functional>
29 #include <type_traits>
30 
31 #include "kitepp/exceptions.hpp"
32 #include "uri-parser/include/parser.hpp"
33 
34 #include "../kite.hpp"
35 #include "../utils.hpp"
36 
37 namespace kiteconnect {
38 
39 inline string kite::getAuth() const { return authorization; }
40 
41 inline string kite::encodeSymbolsList(const std::vector<string>& symbols) {
42  string symbolsList;
43  for (const auto& symbol : symbols) {
44  size_t colonPos = symbol.find_first_of(':');
45  if (colonPos == std::string::npos) {
46  throw libException("invalid symbol");
47  };
48  string exchange = symbol.substr(0, colonPos);
49  string ticker = symbol.substr(colonPos + 1);
50 
51  symbolsList.append(
52  FMT("i={0}:{1}&", exchange, parser::encodeUrl(ticker)));
53  };
54 
55  if (!symbolsList.empty()) { symbolsList.pop_back(); };
56  return symbolsList;
57 }
58 
59 // GMock requires mock methods to be virtual (hi-perf dep injection is not
60 // possible here ಥ﹏ಥ). Macro used to eliminate vptr overhead.
61 inline utils::http::response kite::sendReq(
62  const utils::http::endpoint& endpoint, const utils::http::Params& body,
63  const utils::FmtArgs& fmtArgs) {
64  if (endpoint.contentType == utils::http::CONTENT_TYPE::JSON) {
65  return utils::http::request { endpoint.method, endpoint.Path(fmtArgs),
66  getAuth(), body, endpoint.contentType, endpoint.responseType,
67  body.begin()->second }
68  .send(client);
69  }
70  return utils::http::request { endpoint.method, endpoint.Path(fmtArgs),
71  getAuth(), body, endpoint.contentType, endpoint.responseType }
72  .send(client);
73 };
74 
75 template <class Res, class Data, bool UseCustomParser>
76 inline Res kite::callApi(const string& service, const utils::http::Params& body,
77  const utils::FmtArgs& fmtArgs,
78  utils::json::CustomParser<Res, Data, UseCustomParser> customParser) {
79  utils::http::response res = sendReq(endpoints.at(service), body, fmtArgs);
80  if (!res) {
81  kiteconnect::internal::throwException(
82  res.errorType, res.code, res.message);
83  }
84  return utils::json::parse<Res, Data, UseCustomParser>(
85  res.data, customParser);
86 }
87 } // namespace kiteconnect
kiteconnect::kite::sendReq
utils::http::response sendReq(const utils::http::endpoint &endpoint, const utils::http::Params &body, const utils::FmtArgs &fmtArgs)
send a http request with the context used by kite
Definition: internal.hpp:61