CPPKiteConnect
api.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 #pragma clang diagnostic ignored "-Wundefined-inline"
28 
29 #include "PicoSHA2/picosha2.h"
30 
31 #include "../kite.hpp"
32 #include "../utils.hpp"
33 
34 namespace kiteconnect {
35 
36 inline kite::kite(string apikey): key(std::move(apikey)), client(root.c_str()) {
37  client.set_default_headers({ { "X-Kite-Version", version } });
38 };
39 
40 inline void kite::setApiKey(const string& arg) { key = arg; };
41 
42 inline string kite::getApiKey() const { return key; };
43 
44 inline string kite::loginURL() const {
45  return FMT(loginUrlFmt, "api_key"_a = key);
46 };
47 
48 inline void kite::setAccessToken(const string& arg) {
49  token = arg;
50  authorization = FMT("token {0}:{1}", key, token);
51 };
52 
53 inline string kite::getAccessToken() const { return token; };
54 
56  const string& requestToken, const string& apiSecret) {
57  return callApi<userSession, utils::json::JsonObject>("api.token",
58  {
59  { "api_key", key },
60  { "request_token", requestToken },
61  { "checksum",
62  picosha2::hash256_hex_string(key + requestToken + apiSecret) },
63  });
64 };
65 
66 inline bool kite::invalidateSession() {
67  utils::http::response res =
68  sendReq(endpoints.at("api.token.invalidate"), {}, { key, token });
69  return static_cast<bool>(res);
70 };
71 } // namespace kiteconnect
kiteconnect::kite::kite
kite(string apikey)
Construct a new kite object.
Definition: api.hpp:36
kiteconnect::kite::getApiKey
string getApiKey() const
Get current session's API key.
Definition: api.hpp:42
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
kiteconnect::kite::getAccessToken
string getAccessToken() const
Get access token set for current session.
Definition: api.hpp:53
kiteconnect::kite::setApiKey
void setApiKey(const string &arg)
Set the API key for current session.
Definition: api.hpp:40
kiteconnect::kite::generateSession
userSession generateSession(const string &requestToken, const string &apiSecret)
Generate an user session. Use this method to generate an access token.
Definition: api.hpp:55
kiteconnect::kite::loginURL
string loginURL() const
Get the login URL to which the user should be redirected to initiate the login flow.
Definition: api.hpp:44
kiteconnect::kite::invalidateSession
bool invalidateSession()
This method invalidates the access token and destroys current session.
Definition: api.hpp:66
kiteconnect::userSession
Represents information of a user session.
Definition: user.hpp:100
kiteconnect::kite::setAccessToken
void setAccessToken(const string &arg)
Set the access token current session.
Definition: api.hpp:48