diff --git a/src/cocoatweet/api/favorite/create.cc b/src/cocoatweet/api/favorite/create.cc index 75e8f4f..cf65ae1 100644 --- a/src/cocoatweet/api/favorite/create.cc +++ b/src/cocoatweet/api/favorite/create.cc @@ -11,6 +11,6 @@ void Create::id(const std::string& _id) { } void Create::process(std::weak_ptr _oauth) { - HttpPost::process(_oauth, [](const std::string& _srv) { std::cout << _srv << std::endl; }); + HttpPost::process(_oauth, [](const unsigned int _, const std::string& _srv) { std::cout << _srv << std::endl; }); } } // namespace CocoaTweet::API::Favorites diff --git a/src/cocoatweet/api/favorite/destroy.cc b/src/cocoatweet/api/favorite/destroy.cc index a808463..2d55da4 100644 --- a/src/cocoatweet/api/favorite/destroy.cc +++ b/src/cocoatweet/api/favorite/destroy.cc @@ -11,6 +11,6 @@ void Destroy::id(const std::string& _id) { } void Destroy::process(std::weak_ptr _oauth) { - HttpPost::process(_oauth, [](const std::string& _srv) { std::cout << _srv << std::endl; }); + HttpPost::process(_oauth, [](const unsigned int _,const std::string& _srv) { std::cout << _srv << std::endl; }); } } // namespace CocoaTweet::API::Favorites diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index 56ab08f..c1b0421 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -17,7 +17,7 @@ size_t HttpPost::curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::str } void HttpPost::process(std::weak_ptr _oauth, - std::function _callback) { + std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 auto oauth = _oauth.lock(); auto oauthParam = oauth->oauthParam(); @@ -60,6 +60,7 @@ void HttpPost::process(std::weak_ptr _oauth, CURL* curl; CURLcode res; std::string rcv; + long responseCode; curl = curl_easy_init(); url_ = url_; std::cout << "URL : " << url_ << std::endl; @@ -77,15 +78,17 @@ void HttpPost::process(std::weak_ptr _oauth, headers = curl_slist_append(headers, oauthHeader.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); res = curl_easy_perform(curl); + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); curl_easy_cleanup(curl); } + if (res != CURLE_OK) { std::cout << "curl error : " << res << std::endl; exit(1); } if (_callback) { - _callback(rcv); + _callback(responseCode, rcv); } } } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index ed3b676..2fd3167 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -13,7 +13,7 @@ protected: std::string url_; std::string contentType_; void process(std::weak_ptr _oauth, - std::function _callback); + 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/model/tweet.cc b/src/cocoatweet/api/model/tweet.cc index 590b579..8ed8c7c 100644 --- a/src/cocoatweet/api/model/tweet.cc +++ b/src/cocoatweet/api/model/tweet.cc @@ -10,7 +10,6 @@ Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) { Tweet tweet; if (_responseCode == 200) { - Tweet tweet; tweet.id(j["id_str"]); } else { auto error = j["errors"][0]["code"]; @@ -23,11 +22,11 @@ Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) { return tweet; } -void Tweet::id(const std::string& _id) { +void Tweet::id(const std::string _id) { id_ = _id; } -const std::string& Tweet::id() const { +const std::string Tweet::id() const { return id_; } } // namespace CocoaTweet::API::Model diff --git a/src/cocoatweet/api/model/tweet.h b/src/cocoatweet/api/model/tweet.h index d039479..085362d 100644 --- a/src/cocoatweet/api/model/tweet.h +++ b/src/cocoatweet/api/model/tweet.h @@ -11,8 +11,8 @@ public: Tweet(const unsigned int _responseCode, const std::string& _json) : Tweet(Tweet::parse(_responseCode, _json)) {} static Tweet parse(const unsigned int _responseCode, const std::string& _json); - void id(const std::string& _id); - const std::string& id() const; + void id(const std::string _id); + const std::string id() const; private: std::string id_; diff --git a/src/cocoatweet/api/status/destroy.cc b/src/cocoatweet/api/status/destroy.cc index 339894a..e8d89d8 100644 --- a/src/cocoatweet/api/status/destroy.cc +++ b/src/cocoatweet/api/status/destroy.cc @@ -8,12 +8,12 @@ void Destroy::id(const std::string _id) { url_ = "https://api.twitter.com/1.1/statuses/destroy/" + _id + ".json"; } -void Destroy::process(std::weak_ptr _oauth) { +CocoaTweet::API::Model::Tweet Destroy::process(std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; - HttpPost::process(_oauth, [&tweet](const std::string& _srv) { - tweet = CocoaTweet::API::Model::Tweet::parse(100, _srv); + HttpPost::process(_oauth, [&tweet](const unsigned int _responseCode, const std::string& _rsv) { + tweet = CocoaTweet::API::Model::Tweet::parse(_responseCode, _rsv); }); - std::cout << "afw" << std::endl; + return tweet; } } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/destroy.h b/src/cocoatweet/api/status/destroy.h index 24eab78..70247d5 100644 --- a/src/cocoatweet/api/status/destroy.h +++ b/src/cocoatweet/api/status/destroy.h @@ -3,6 +3,7 @@ #include #include +#include namespace CocoaTweet::API::Statuses { /// @brief class for using status/destroy:id endpoint @@ -19,7 +20,7 @@ public: /// @brief process request for endpoint /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] none - void process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet 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 0f42ce9..7ea485d 100644 --- a/src/cocoatweet/api/status/status.cc +++ b/src/cocoatweet/api/status/status.cc @@ -9,15 +9,15 @@ Status::Status(std::shared_ptr _oauth) { oauth_ = _oauth; } -void Status::Update(const std::string& _status) const { +CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status) const { CocoaTweet::API::Statuses::Update update; update.status(_status); - update.process(oauth_); + return update.process(oauth_); } -void Status::Destroy(const std::string& _id) const { +CocoaTweet::API::Model::Tweet Status::Destroy(const std::string& _id) const { CocoaTweet::API::Statuses::Destroy destroy; destroy.id(_id); - destroy.process(oauth_); + return destroy.process(oauth_); } } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.h b/src/cocoatweet/api/status/status.h index 7dec316..282583b 100644 --- a/src/cocoatweet/api/status/status.h +++ b/src/cocoatweet/api/status/status.h @@ -3,14 +3,15 @@ #include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/oauth/oauth.h" +#include namespace CocoaTweet::API::Statuses { class Status : public groupInterface { public: Status() = default; Status(std::shared_ptr _oauth); - void Update(const std::string& _status) const; - void Destroy(const std::string& _id) const; + CocoaTweet::API::Model::Tweet Update(const std::string& _status) const; + CocoaTweet::API::Model::Tweet Destroy(const std::string& _id) const; private: }; diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index 5605524..4a1f88b 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -6,13 +6,18 @@ 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) { status_ = _status; bodyParam_.insert_or_assign("status", status_); } -void Update::process(std::weak_ptr _oauth) { - HttpPost::process(_oauth, [](const std::string& _srv) { std::cout << _srv << std::endl; }); +CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr _oauth) { + CocoaTweet::API::Model::Tweet tweet; + HttpPost::process(_oauth, [&tweet](const unsigned int _responseCode, const std::string& _rsv) { + tweet = CocoaTweet::API::Model::Tweet::parse(_responseCode, _rsv); + }); + return tweet; } } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.h b/src/cocoatweet/api/status/update.h index 6f7cfb0..529bb90 100644 --- a/src/cocoatweet/api/status/update.h +++ b/src/cocoatweet/api/status/update.h @@ -2,15 +2,15 @@ #define COCOATWEET_API_STATUSES_UPDATE_H_ #include -#include "cocoatweet/api/interface/httpPost.h" -//#include "cocoatweet/oauth/oauth.h" +#include +#include namespace CocoaTweet::API::Statuses { class Update : public CocoaTweet::API::Interface::HttpPost { public: Update(); void status(const std::string _status); - void process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); private: std::string status_;