HttpGetの実装に向けてHttpBaseの実装

This commit is contained in:
keita
2021-03-10 22:34:20 +09:00
parent ccdb26b18d
commit 00a568c016
3 changed files with 34 additions and 13 deletions
+25
View File
@@ -0,0 +1,25 @@
#ifndef COCOATWEET_API_INTERFACE_HTTPBASE_H_
#define COCOATWEET_API_INTERFACE_HTTPBASE_H_
#include <functional>
#include "cocoatweet/oauth/oauth.h"
namespace CocoaTweet::API::Interface {
class HttpBase {
public:
protected:
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
std::map<std::string, std::string> bodyParam_;
std::string url_;
std::string contentType_;
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback) = 0;
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream){
int realsize = _size * _nmemb;
_stream->append(_ptr, realsize);
return realsize;
}
};
} // namespace CocoaTweet::API::Interface
#endif
+7 -7
View File
@@ -9,13 +9,11 @@ extern "C" {
#include <curl/curl.h> #include <curl/curl.h>
} }
namespace CocoaTweet::API::Interface { #ifndef NDEBUG
size_t HttpPost::curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { #include <iostream>
int realsize = _size * _nmemb; #endif
_stream->append(_ptr, realsize);
return realsize;
}
namespace CocoaTweet::API::Interface {
void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback) { std::function<void(const unsigned int, const std::string&)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
@@ -38,7 +36,7 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
{ {
std::vector<std::string> tmp; std::vector<std::string> tmp;
for (const auto& [key, value] : bodyParam_) { for (const auto& [key, value] : bodyParam_) {
tmp.push_back(key + "=" + value); tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
} }
requestBody = CocoaTweet::Util::join(tmp, "&"); requestBody = CocoaTweet::Util::join(tmp, "&");
} }
@@ -69,12 +67,14 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "requestBody : " << requestBody << std::endl;
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif #endif
// Headerを保持するcurl_slist*を初期化 // Headerを保持するcurl_slist*を初期化
struct curl_slist* headers = NULL; struct curl_slist* headers = NULL;
// Authorizationをヘッダに追加 // Authorizationをヘッダに追加
headers = curl_slist_append(headers, oauthHeader.c_str()); headers = curl_slist_append(headers, oauthHeader.c_str());
headers = curl_slist_append(headers, ("Content-Type: " + contentType_).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
+2 -6
View File
@@ -3,18 +3,14 @@
#include <functional> #include <functional>
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/oauth/oauth.h"
#include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
class HttpPost { class HttpPost : public HttpBase{
public: public:
protected: protected:
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
std::map<std::string, std::string> bodyParam_;
std::string url_;
std::string contentType_;
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback); std::function<void(const unsigned int, const std::string&)> _callback);
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
}; };
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface