linux版本中间件

base.h 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #ifndef __AIP_BASE_H__
  16. #define __AIP_BASE_H__
  17. #define __AIP_SDK_VERSION__ "0.3.2"
  18. #include <memory>
  19. #include <cstring>
  20. #include "http.h"
  21. #include "json/json.h"
  22. #include "base64.h"
  23. #include "curl/curl.h"
  24. #include "utils.h"
  25. namespace aip {
  26. static const char* CURL_ERROR_CODE = "curl_error_code";
  27. static const std::string ACCESS_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token";
  28. static const std::map<std::string, std::string> null;
  29. class AipBase
  30. {
  31. private:
  32. std::string _app_id;
  33. int _expired_time;
  34. bool _is_bce;
  35. bool _has_decide_type;
  36. std::string _scope;
  37. protected:
  38. std::string getAccessToken()
  39. {
  40. time_t now = time(NULL);
  41. if (!access_token.empty())
  42. {
  43. return this->access_token;
  44. }
  45. if (now < this->_expired_time - 60 * 60 * 24)
  46. {
  47. return this->access_token;
  48. }
  49. std::string response;
  50. std::map<std::string, std::string> params;
  51. params["grant_type"] = "client_credentials";
  52. params["client_id"] = this->ak;
  53. params["client_secret"] = this->sk;
  54. int status_code = this->client.get(
  55. ACCESS_TOKEN_URL,
  56. &params,
  57. nullptr,
  58. &response
  59. );
  60. Json::Value obj;
  61. if (status_code != CURLcode::CURLE_OK) {
  62. obj[CURL_ERROR_CODE] = status_code;
  63. return obj.toStyledString();
  64. }
  65. std::string error;
  66. std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
  67. reader->parse(response.data(), response.data() + response.size(), &obj, &error);
  68. this->access_token = obj["access_token"].asString();
  69. this->_expired_time = obj["expires_in"].asInt() + (int) now;
  70. this->_scope = obj["scope"].asString();
  71. return this->access_token;
  72. }
  73. public:
  74. std::string ak;
  75. std::string sk;
  76. HttpClient client;
  77. Json::CharReaderBuilder crbuilder;
  78. std::string access_token;
  79. AipBase(const std::string & app_id, const std::string & ak, const std::string & sk):
  80. _app_id(app_id),
  81. _is_bce(false),
  82. _has_decide_type(false),
  83. ak(ak),
  84. sk(sk)
  85. {
  86. if (_app_id == "")
  87. {
  88. }
  89. }
  90. void setConnectionTimeoutInMillis(int connect_timeout)
  91. {
  92. this->client.setConnectTimeout(connect_timeout);
  93. }
  94. void setSocketTimeoutInMillis(int socket_timeout)
  95. {
  96. this->client.setSocketTimeout(socket_timeout);
  97. }
  98. void setDebug(bool debug)
  99. {
  100. this->client.setDebug(debug);
  101. }
  102. std::string getAk() {
  103. return ak;
  104. }
  105. Json::Value request(
  106. std::string url,
  107. std::map<std::string, std::string> const & params,
  108. std::string const & data,
  109. std::map<std::string, std::string> const & headers)
  110. {
  111. std::string response;
  112. Json::Value obj;
  113. std::string body;
  114. auto headers_for_sign = headers;
  115. auto temp_params = params;
  116. temp_params["charset"] = "UTF-8";
  117. this->prepare_request(url, temp_params, headers_for_sign);
  118. int status_code = this->client.post(url, &temp_params, data, &headers_for_sign, &response);
  119. if (status_code != CURLcode::CURLE_OK) {
  120. obj[CURL_ERROR_CODE] = status_code;
  121. return obj;
  122. }
  123. std::string error;
  124. std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
  125. reader->parse(response.data(), response.data() + response.size(), &obj, &error);
  126. return obj;
  127. }
  128. Json::Value request(
  129. std::string url,
  130. std::map<std::string, std::string> const & params,
  131. std::map<std::string, std::string> const & data,
  132. std::map<std::string, std::string> const & headers)
  133. {
  134. std::string response;
  135. Json::Value obj;
  136. auto headers_for_sign = headers;
  137. auto temp_params = params;
  138. this->prepare_request(url, temp_params, headers_for_sign);
  139. int status_code = this->client.post(url, &temp_params, data, &headers_for_sign, &response);
  140. if (status_code != CURLcode::CURLE_OK) {
  141. obj[CURL_ERROR_CODE] = status_code;
  142. return obj;
  143. }
  144. std::string error;
  145. std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
  146. reader->parse(response.data(), response.data() + response.size(), &obj, &error);
  147. return obj;
  148. }
  149. void prepare_request(std::string url,
  150. std::map<std::string, std::string> & params,
  151. std::map<std::string, std::string> & headers)
  152. {
  153. params["aipSdk"] = "C";
  154. params["aipSdkVersion"] = __AIP_SDK_VERSION__;
  155. if (_has_decide_type) {
  156. if (_is_bce) {
  157. std::string method = "POST";
  158. sign(method, url, params, headers, ak, sk);
  159. } else {
  160. params["access_token"] = this->getAccessToken();
  161. }
  162. return;
  163. }
  164. if (getAccessToken() == "") {
  165. _is_bce = true;
  166. } else {
  167. const char * t = std::strstr(this->_scope.c_str(), "brain_all_scope");
  168. if (t == NULL)
  169. {
  170. _is_bce = true;
  171. }
  172. }
  173. _has_decide_type = true;
  174. prepare_request(url, params, headers);
  175. }
  176. };
  177. }
  178. #endif