post処理をinterface側に実装(#51)

This commit is contained in:
keita
2021-02-18 11:34:23 +09:00
parent c43da34fe4
commit e45fc37c12
4 changed files with 91 additions and 92 deletions
@@ -1,4 +1,13 @@
#include <cocoatweet/api/interface/postInterface.h>
#include "cocoatweet/util/util.h"
#include <iterator>
#include <memory>
#include <vector>
#include <sstream>
#include <iostream>
extern "C" {
#include <curl/curl.h>
}
namespace CocoaTweet::API::Interface {
size_t postInterface::curlCallback_(char* _ptr, size_t _size, size_t _nmemb,
@@ -7,4 +16,82 @@ size_t postInterface::curlCallback_(char* _ptr, size_t _size, size_t _nmemb,
_stream->append(_ptr, realsize);
return realsize;
}
void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam();
auto sigingParam = oauthParam;
for (const auto [k, v] : bodyParam_) {
sigingParam.insert_or_assign(k, v);
}
auto signature = oauth->signature(sigingParam, "POST", url_);
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
std::cout << "signature : " << signature["oauth_signature"] << std::endl;
oauthParam.merge(signature);
// リクエストボディの構築
std::string requestBody = "";
{
std::vector<std::string> tmp;
for (const auto& [key, value] : bodyParam_) {
tmp.push_back(key + "=" + value);
}
std::stringstream os;
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
requestBody = os.str();
requestBody.erase(requestBody.size() - std::char_traits<char>::length("&"));
}
std::cout << "request Body -> " << requestBody << std::endl;
// ヘッダの構築
std::string oauthHeader = "authorization: OAuth ";
{
std::vector<std::string> tmp;
for (const auto& [key, value] : oauthParam) {
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
}
std::stringstream os;
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, ","));
oauthHeader += os.str();
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
}
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
// do post
CURL* curl;
CURLcode res;
std::string rcv;
curl = curl_easy_init();
url_ = url_; // + "?status=" + status_;
std::cout << "URL : " << url_ << std::endl;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Headerを保持するcurl_slist*を初期化
struct curl_slist* headers = NULL;
// Authorizationをヘッダに追加
headers = curl_slist_append(headers, oauthHeader.c_str());
// headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if (res != CURLE_OK) {
std::cout << "curl error : " << res << std::endl;
exit(1);
}
if (_callback) {
_callback(rcv);
}
}
} // namespace CocoaTweet::API::Interface
+3 -3
View File
@@ -7,12 +7,12 @@
namespace CocoaTweet::API::Interface {
class postInterface {
public:
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback) = 0;
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback);
protected:
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
std::map<std::string, std::string> param_;
std::map<std::string, std::string> bodyParam_;
std::string url_;
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
};
+1 -87
View File
@@ -1,13 +1,4 @@
#include "cocoatweet/api/status/update.h"
#include "cocoatweet/util/util.h"
#include <iterator>
#include <memory>
#include <vector>
#include <sstream>
#include <iostream>
extern "C" {
#include <curl/curl.h>
}
namespace CocoaTweet::API::Statuses {
Update::Update() {
@@ -15,84 +6,7 @@ Update::Update() {
}
void Update::status(const std::string _status) {
status_ = _status;
param_.insert_or_assign("status", status_);
bodyParam_.insert_or_assign("status", status_);
}
void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam();
auto sigingParam = oauthParam;
for (const auto [k, v] : param_) {
sigingParam.insert_or_assign(k, v);
}
auto signature = oauth->signature(sigingParam, "POST", url_);
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
std::cout << "signature : " << signature["oauth_signature"] << std::endl;
oauthParam.merge(signature);
// リクエストボディの構築
std::string requestBody = "";
{
std::vector<std::string> tmp;
for (const auto& [key, value] : param_) {
tmp.push_back(key + "=" + value);
}
std::stringstream os;
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
requestBody = os.str();
requestBody.erase(requestBody.size() - std::char_traits<char>::length("&"));
}
std::cout << "request Body -> " << requestBody << std::endl;
// ヘッダの構築
std::string oauthHeader = "authorization: OAuth ";
{
std::vector<std::string> tmp;
for (const auto& [key, value] : oauthParam) {
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
}
std::stringstream os;
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, ","));
oauthHeader += os.str();
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
}
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
// do post
CURL* curl;
CURLcode res;
std::string rcv;
curl = curl_easy_init();
url_ = url_; // + "?status=" + status_;
std::cout << "URL : " << url_ << std::endl;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Headerを保持するcurl_slist*を初期化
struct curl_slist* headers = NULL;
// Authorizationをヘッダに追加
headers = curl_slist_append(headers, oauthHeader.c_str());
// headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if (res != CURLE_OK) {
std::cout << "curl error : " << res << std::endl;
exit(1);
}
if (_callback) {
_callback(rcv);
}
}
} // namespace CocoaTweet::API::Statuses
-2
View File
@@ -11,8 +11,6 @@ class Update : public CocoaTweet::API::Interface::postInterface {
public:
Update();
void status(const std::string _status);
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback);
private:
std::string status_;