From 8600dff03773ecbb4a69a75db0e8804be670d8d9 Mon Sep 17 00:00:00 2001 From: keita Date: Sat, 20 Feb 2021 22:29:49 +0900 Subject: [PATCH] =?UTF-8?q?interface=E5=91=A8=E3=82=8A=E3=81=AE=E8=B2=A0?= =?UTF-8?q?=E5=82=B5=E3=82=92=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cocoatweet/api/favorite/create.cc | 5 +++++ src/cocoatweet/api/favorite/create.h | 1 + src/cocoatweet/api/favorite/destroy.cc | 5 +++++ src/cocoatweet/api/favorite/destroy.h | 1 + src/cocoatweet/api/favorite/favorite.cc | 4 ++-- src/cocoatweet/api/interface/postInterface.cc | 6 ++++-- src/cocoatweet/api/interface/postInterface.h | 6 ++++-- src/cocoatweet/api/status/destroy.cc | 5 +++++ src/cocoatweet/api/status/destroy.h | 1 + src/cocoatweet/api/status/status.cc | 4 ++-- src/cocoatweet/api/status/update.cc | 5 +++++ src/cocoatweet/api/status/update.h | 1 + 12 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/cocoatweet/api/favorite/create.cc b/src/cocoatweet/api/favorite/create.cc index fe28295..8e447e6 100644 --- a/src/cocoatweet/api/favorite/create.cc +++ b/src/cocoatweet/api/favorite/create.cc @@ -2,10 +2,15 @@ namespace CocoaTweet::API::Favorites { Create::Create() { + contentType_ = "application/x-www-form-urlencoded"; url_ = "https://api.twitter.com/1.1/favorites/create.json"; } void Create::id(const std::string& _id) { bodyParam_.insert_or_assign("id", _id); } + +void Create::process(std::weak_ptr _oauth){ + postInterface::process(_oauth, [](const std::string& _srv){std::cout << _srv << std::endl;}); +} } // namespace CocoaTweet::API::Favorites diff --git a/src/cocoatweet/api/favorite/create.h b/src/cocoatweet/api/favorite/create.h index 2d1e873..b5707b9 100644 --- a/src/cocoatweet/api/favorite/create.h +++ b/src/cocoatweet/api/favorite/create.h @@ -8,6 +8,7 @@ class Create : public CocoaTweet::API::Interface::postInterface { public: Create(); void id(const std::string& _id); + void process(std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/favorite/destroy.cc b/src/cocoatweet/api/favorite/destroy.cc index 967137e..cae4689 100644 --- a/src/cocoatweet/api/favorite/destroy.cc +++ b/src/cocoatweet/api/favorite/destroy.cc @@ -2,10 +2,15 @@ namespace CocoaTweet::API::Favorites { Destroy::Destroy() { + contentType_ = "application/x-www-form-urlencoded"; url_ = "https://api.twitter.com/1.1/favorites/destroy.json"; } void Destroy::id(const std::string& _id) { bodyParam_.insert_or_assign("id", _id); } + +void Destroy::process(std::weak_ptr _oauth){ + postInterface::process(_oauth, [](const std::string& _srv){std::cout << _srv << std::endl;}); +} } // namespace CocoaTweet::API::Favorites diff --git a/src/cocoatweet/api/favorite/destroy.h b/src/cocoatweet/api/favorite/destroy.h index 423a916..cf408d3 100644 --- a/src/cocoatweet/api/favorite/destroy.h +++ b/src/cocoatweet/api/favorite/destroy.h @@ -8,6 +8,7 @@ class Destroy : public CocoaTweet::API::Interface::postInterface { public: Destroy(); void id(const std::string& _id); + void process(std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/favorite/favorite.cc b/src/cocoatweet/api/favorite/favorite.cc index e0e1a1b..6bdb88d 100644 --- a/src/cocoatweet/api/favorite/favorite.cc +++ b/src/cocoatweet/api/favorite/favorite.cc @@ -12,12 +12,12 @@ Favorite::Favorite(std::shared_ptr _oauth) { void Favorite::Create(const std::string& _id) const { CocoaTweet::API::Favorites::Create create; create.id(_id); - create.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; }); + create.process(oauth_); } void Favorite::Destroy(const std::string& _id) const { CocoaTweet::API::Favorites::Destroy destroy; destroy.id(_id); - destroy.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; }); + destroy.process(oauth_); } } // namespace CocoaTweet::API::Favorites diff --git a/src/cocoatweet/api/interface/postInterface.cc b/src/cocoatweet/api/interface/postInterface.cc index 9298ef1..bdf3035 100644 --- a/src/cocoatweet/api/interface/postInterface.cc +++ b/src/cocoatweet/api/interface/postInterface.cc @@ -23,8 +23,10 @@ void postInterface::process(std::weak_ptr _oauth, auto oauth = _oauth.lock(); auto oauthParam = oauth->oauthParam(); auto sigingParam = oauthParam; - for (const auto [k, v] : bodyParam_) { - sigingParam.insert_or_assign(k, v); + if(contentType_ == "application/x-www-form-urlencoded"){ + for (const auto [k, v] : bodyParam_) { + sigingParam.insert_or_assign(k, v); + } } auto signature = oauth->signature(sigingParam, "POST", url_); diff --git a/src/cocoatweet/api/interface/postInterface.h b/src/cocoatweet/api/interface/postInterface.h index 42f73f0..71be2c4 100644 --- a/src/cocoatweet/api/interface/postInterface.h +++ b/src/cocoatweet/api/interface/postInterface.h @@ -3,17 +3,19 @@ #include #include "cocoatweet/oauth/oauth.h" +#include namespace CocoaTweet::API::Interface { class postInterface { public: - void process(std::weak_ptr _oauth, - std::function _callback); protected: std::weak_ptr oauth_; std::map bodyParam_; std::string url_; + std::string contentType_; + void process(std::weak_ptr _oauth, + std::function _callback); static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream); }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/status/destroy.cc b/src/cocoatweet/api/status/destroy.cc index d4f7e51..2424202 100644 --- a/src/cocoatweet/api/status/destroy.cc +++ b/src/cocoatweet/api/status/destroy.cc @@ -3,7 +3,12 @@ namespace CocoaTweet::API::Statuses { Destroy::Destroy() {} void Destroy::id(const std::string _id) { + contentType_ = "application/x-www-form-urlencoded"; url_ = "https://api.twitter.com/1.1/statuses/destroy/" + _id + ".json"; } +void Destroy::process(std::weak_ptr _oauth){ + postInterface::process(_oauth, [](const std::string& _srv){std::cout << _srv << std::endl;}); +} + } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/destroy.h b/src/cocoatweet/api/status/destroy.h index ac410a1..45e9a4d 100644 --- a/src/cocoatweet/api/status/destroy.h +++ b/src/cocoatweet/api/status/destroy.h @@ -11,6 +11,7 @@ class Destroy : public CocoaTweet::API::Interface::postInterface { public: Destroy(); void id(const std::string _id); + void process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.cc b/src/cocoatweet/api/status/status.cc index 6390ac2..0f42ce9 100644 --- a/src/cocoatweet/api/status/status.cc +++ b/src/cocoatweet/api/status/status.cc @@ -12,12 +12,12 @@ Status::Status(std::shared_ptr _oauth) { void Status::Update(const std::string& _status) const { CocoaTweet::API::Statuses::Update update; update.status(_status); - update.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; }); + update.process(oauth_); } void Status::Destroy(const std::string& _id) const { CocoaTweet::API::Statuses::Destroy destroy; destroy.id(_id); - destroy.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; }); + destroy.process(oauth_); } } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index 621ef29..d09106a 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -2,6 +2,7 @@ namespace CocoaTweet::API::Statuses { Update::Update() { + contentType_ = "application/x-www-form-urlencoded"; url_ = "https://api.twitter.com/1.1/statuses/update.json"; } void Update::status(const std::string _status) { @@ -9,4 +10,8 @@ void Update::status(const std::string _status) { bodyParam_.insert_or_assign("status", status_); } +void Update::process(std::weak_ptr _oauth){ + postInterface::process(_oauth, [](const std::string& _srv){std::cout << _srv << std::endl;}); +} + } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.h b/src/cocoatweet/api/status/update.h index db12d8c..848046b 100644 --- a/src/cocoatweet/api/status/update.h +++ b/src/cocoatweet/api/status/update.h @@ -11,6 +11,7 @@ class Update : public CocoaTweet::API::Interface::postInterface { public: Update(); void status(const std::string _status); + void process(std::weak_ptr _oauth); private: std::string status_;