diff --git a/src/cocoatweet/api/favorite/create.cc b/src/cocoatweet/api/favorite/create.cc index a181e1a..74a4358 100644 --- a/src/cocoatweet/api/favorite/create.cc +++ b/src/cocoatweet/api/favorite/create.cc @@ -13,10 +13,9 @@ void Create::id(const std::string& _id) { CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; - HttpPost::process(_oauth, - [&tweet](const unsigned int _responseCode, const std::string& _rcv) { - tweet = CocoaTweet::API::Model::Tweet(_responseCode, _rcv); - }); + HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { + tweet = CocoaTweet::API::Model::Tweet(_rcv); + }); return tweet; } diff --git a/src/cocoatweet/api/favorite/destroy.cc b/src/cocoatweet/api/favorite/destroy.cc index 126eac6..ef88c9a 100644 --- a/src/cocoatweet/api/favorite/destroy.cc +++ b/src/cocoatweet/api/favorite/destroy.cc @@ -14,10 +14,9 @@ void Destroy::id(const std::string& _id) { CocoaTweet::API::Model::Tweet Destroy::process( std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; - HttpPost::process(_oauth, - [&tweet](const unsigned int _responseCode, const std::string& _rcv) { - tweet = CocoaTweet::API::Model::Tweet(_responseCode, _rcv); - }); + HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { + tweet = CocoaTweet::API::Model::Tweet(_rcv); + }); return tweet; } diff --git a/src/cocoatweet/api/interface/httpBase.h b/src/cocoatweet/api/interface/httpBase.h index b5dffc7..e6314a3 100644 --- a/src/cocoatweet/api/interface/httpBase.h +++ b/src/cocoatweet/api/interface/httpBase.h @@ -12,9 +12,8 @@ protected: std::map bodyParam_; std::string url_; std::string contentType_; - virtual void process( - std::weak_ptr _oauth, - std::function _callback) = 0; + virtual void process(std::weak_ptr _oauth, + std::function _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); diff --git a/src/cocoatweet/api/interface/httpGet.cc b/src/cocoatweet/api/interface/httpGet.cc index 4dbdf0f..f1f26db 100644 --- a/src/cocoatweet/api/interface/httpGet.cc +++ b/src/cocoatweet/api/interface/httpGet.cc @@ -15,7 +15,7 @@ extern "C" { namespace CocoaTweet::API::Interface { void HttpGet::process(std::weak_ptr _oauth, - std::function _callback) { + std::function _callback) { auto url = url_; // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 @@ -92,7 +92,7 @@ void HttpGet::process(std::weak_ptr _oauth, } if (_callback) { - _callback(responseCode, rcv); + _callback(rcv); } } } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpGet.h b/src/cocoatweet/api/interface/httpGet.h index 86f2668..bf98421 100644 --- a/src/cocoatweet/api/interface/httpGet.h +++ b/src/cocoatweet/api/interface/httpGet.h @@ -16,7 +16,7 @@ protected: /// @param[in] std::function _callback : /// callback method for processing to response void process(std::weak_ptr _oauth, - std::function _callback); + std::function _callback); }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index ebed8cb..378c280 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -1,5 +1,11 @@ #include #include "cocoatweet/util/util.h" +#include +#include +#include +#include +#include +#include "nlohmann/json.hpp" #include #include #include @@ -15,7 +21,7 @@ extern "C" { namespace CocoaTweet::API::Interface { void HttpPost::process(std::weak_ptr _oauth, - std::function _callback) { + std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 auto oauth = _oauth.lock(); auto oauthParam = oauth->oauthParam(); @@ -103,8 +109,29 @@ void HttpPost::process(std::weak_ptr _oauth, exit(1); } + if ((responseCode / 100) == 4) { + auto j = nlohmann::json::parse(rcv); + auto error = j["errors"][0]["code"]; + auto message = j["errors"][0]["message"]; + if (j.count("error") != 0) { + // この形式はエラーコードを持たないのでエラー種別が特定できない + throw new CocoaTweet::Exception::Exception(j["error"]); + } + if (error.get() == 144) { + throw CocoaTweet::Exception::TweetNotFoundException(message.get().c_str()); + } else if (error.get() == 32) { + throw CocoaTweet::Exception::AuthenticateException(message.get().c_str()); + } else if (error.get() == 187) { + throw CocoaTweet::Exception::TweetDuplicateException(message.get().c_str()); + } else if (error.get() == 88 || error.get() == 185) { + throw CocoaTweet::Exception::RateLimitException(message.get().c_str()); + } else if (error.get() == 186) { + throw CocoaTweet::Exception::TweetTooLongException(message.get().c_str()); + } + } + if (_callback) { - _callback(responseCode, rcv); + _callback(rcv); } } } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index 2547796..441fe58 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -16,7 +16,7 @@ protected: /// @param[in] std::function _callback : /// callback method for processing to response void process(std::weak_ptr _oauth, - std::function _callback); + std::function _callback); }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/media/upload.cc b/src/cocoatweet/api/media/upload.cc index bd10fc0..43de855 100644 --- a/src/cocoatweet/api/media/upload.cc +++ b/src/cocoatweet/api/media/upload.cc @@ -43,10 +43,9 @@ CocoaTweet::API::Model::MediaStore Upload::process( bodyParam_.insert_or_assign( "media_type", mimeType.at(std::filesystem::path(media_).extension().string())); - HttpPost::process(_oauth, - [&media](const unsigned int _responseCode, const std::string& _rsv) { - media = CocoaTweet::API::Model::MediaStore::parse(_responseCode, _rsv); - }); + HttpPost::process(_oauth, [&media](const std::string& _rcv) { + media = CocoaTweet::API::Model::MediaStore::parse(_rcv); + }); bodyParam_.insert_or_assign("media_id", media.id()); } @@ -63,7 +62,7 @@ CocoaTweet::API::Model::MediaStore Upload::process( bodyParam_.insert_or_assign("command", "APPEND"); bodyParam_.insert_or_assign("segment_index", std::to_string(segment)); bodyParam_.insert_or_assign("media", data.substr(segment * chunk, chunk)); - HttpPost::process(_oauth, [](const unsigned int _responseCode, const std::string& _rsv) { + HttpPost::process(_oauth, [](const std::string& _rsv) { // std::cout << _responseCode << std::endl << _rsv<< std::endl; }); segment++; @@ -76,11 +75,9 @@ CocoaTweet::API::Model::MediaStore Upload::process( bodyParam_.insert_or_assign("command", "FINALIZE"); bodyParam_.erase("segment_index"); bodyParam_.erase("media"); - HttpPost::process(_oauth, - [&media](const unsigned int _responseCode, const std::string& _rsv) { - std::cout << _responseCode << std::endl << _rsv << std::endl; - media = CocoaTweet::API::Model::MediaStore::parse(_responseCode, _rsv); - }); + HttpPost::process(_oauth, [&media](const std::string& _rcv) { + media = CocoaTweet::API::Model::MediaStore::parse(_rcv); + }); } // STATUS if needed diff --git a/src/cocoatweet/api/model/mediaStore.cc b/src/cocoatweet/api/model/mediaStore.cc index 10f2cd0..a293b3a 100644 --- a/src/cocoatweet/api/model/mediaStore.cc +++ b/src/cocoatweet/api/model/mediaStore.cc @@ -3,31 +3,27 @@ #include "nlohmann/json.hpp" namespace CocoaTweet::API::Model { -MediaStore MediaStore::parse(const unsigned int _responseCode, const std::string& _json) { +MediaStore MediaStore::parse(const std::string& _json) { auto j = nlohmann::json::parse(_json); MediaStore media; - if (_responseCode / 100 == 2) { - if (j.count("media_id_string") != 0) { - media.id(j["media_id_string"]); - } + if (j.count("media_id_string") != 0) { + media.id(j["media_id_string"]); + } - if (j.count("size") != 0) { - media.size(j["size"]); - } + if (j.count("size") != 0) { + media.size(j["size"]); + } - if (j.count("expires_after_secs") != 0) { - media.expires(j["expires_after_secs"]); - } + if (j.count("expires_after_secs") != 0) { + media.expires(j["expires_after_secs"]); + } - if (j.count("processing_info") == 0) { - media.state("succeeded"); - } else { - media.state(j["processing_info"]["state"]); - media.remain(j["processing_info"]["check_after_secs"].get()); - } + if (j.count("processing_info") == 0) { + media.state("succeeded"); } else { - throw new CocoaTweet::Exception::Exception(j["error"]); + media.state(j["processing_info"]["state"]); + media.remain(j["processing_info"]["check_after_secs"].get()); } return media; diff --git a/src/cocoatweet/api/model/mediaStore.h b/src/cocoatweet/api/model/mediaStore.h index 145282b..047dbfd 100644 --- a/src/cocoatweet/api/model/mediaStore.h +++ b/src/cocoatweet/api/model/mediaStore.h @@ -15,18 +15,13 @@ public: MediaStore(const MediaStore&) = default; /// @brief constructor for create object from json response - /// @param[in] const unsigned int _responseCode : http status code which received when post - /// request /// @param[in] const std::string& _json : received content from twitter endpoint - MediaStore(const unsigned int _responseCode, const std::string& _json) - : MediaStore(MediaStore::parse(_responseCode, _json)) {} + MediaStore(const std::string& _json) : MediaStore(MediaStore::parse(_json)) {} /// @brief response parser for MediaStore object - /// @param[in] const unsigned int _responseCode : http status code which received when post - /// request /// @param[in] const std::string& _json : received content from twitter endpoint /// @param[out] CocoaTweet::API::Model::MediaStore - static MediaStore parse(const unsigned int _responseCode, const std::string& _json); + static MediaStore parse(const std::string& _json); /// @brief set id of tweet /// @param[in] const std::string _id : media id to set diff --git a/src/cocoatweet/api/model/tweet.cc b/src/cocoatweet/api/model/tweet.cc index 3bf3fb7..27064d0 100644 --- a/src/cocoatweet/api/model/tweet.cc +++ b/src/cocoatweet/api/model/tweet.cc @@ -1,36 +1,15 @@ #include -#include -#include -#include -#include -#include #include "nlohmann/json.hpp" namespace CocoaTweet::API::Model { -Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) { +Tweet Tweet::parse(const std::string& _json) { auto j = nlohmann::json::parse(_json); Tweet tweet; - if (_responseCode == 200) { - tweet.id(j["id_str"]); - tweet.createdAt(j["created_at"]); - tweet.text(j["text"]); - tweet.source(j["source"]); - } else { - auto error = j["errors"][0]["code"]; - auto message = j["errors"][0]["message"]; - if (error.get() == 144) { - throw CocoaTweet::Exception::TweetNotFoundException(message.get().c_str()); - } else if (error.get() == 32) { - throw CocoaTweet::Exception::AuthenticateException(message.get().c_str()); - } else if (error.get() == 187) { - throw CocoaTweet::Exception::TweetDuplicateException(message.get().c_str()); - } else if (error.get() == 88 || error.get() == 185) { - throw CocoaTweet::Exception::RateLimitException(message.get().c_str()); - } else if (error.get() == 186) { - throw CocoaTweet::Exception::TweetTooLongException(message.get().c_str()); - } - } + tweet.id(j["id_str"]); + tweet.createdAt(j["created_at"]); + tweet.text(j["text"]); + tweet.source(j["source"]); return tweet; } diff --git a/src/cocoatweet/api/model/tweet.h b/src/cocoatweet/api/model/tweet.h index 5ad6b6c..8e16a5c 100644 --- a/src/cocoatweet/api/model/tweet.h +++ b/src/cocoatweet/api/model/tweet.h @@ -15,18 +15,13 @@ public: Tweet(const Tweet&) = default; /// @brief constructor for create object from json response - /// @param[in] const unsigned int _responseCode : http status code which received when post - /// request /// @param[in] const std::string& _json : received content from twitter endpoint - Tweet(const unsigned int _responseCode, const std::string& _json) - : Tweet(Tweet::parse(_responseCode, _json)) {} + Tweet(const std::string& _json) : Tweet(Tweet::parse(_json)) {} /// @brief response parser for tweet object - /// @param[in] const unsigned int _responseCode : http status code which received when post - /// request /// @param[in] const std::string& _json : received content from twitter endpoint /// @param[out] CocoaTweet::API::Model::Tweet - static Tweet parse(const unsigned int _responseCode, const std::string& _json); + static Tweet parse(const std::string& _json); /// @brief set id of tweet /// @param[in] const std::string _id : tweet id to set diff --git a/src/cocoatweet/api/status/destroy.cc b/src/cocoatweet/api/status/destroy.cc index bee285e..caa0860 100644 --- a/src/cocoatweet/api/status/destroy.cc +++ b/src/cocoatweet/api/status/destroy.cc @@ -11,10 +11,9 @@ void Destroy::id(const std::string _id) { CocoaTweet::API::Model::Tweet Destroy::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); - }); + HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { + tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); + }); return tweet; } diff --git a/src/cocoatweet/api/status/retweet.cc b/src/cocoatweet/api/status/retweet.cc index 0f7877d..10203ce 100644 --- a/src/cocoatweet/api/status/retweet.cc +++ b/src/cocoatweet/api/status/retweet.cc @@ -2,20 +2,20 @@ #include namespace CocoaTweet::API::Statuses { - Retweet::Retweet(){} +Retweet::Retweet() {} - void Retweet::id(const std::string& _id){ +void Retweet::id(const std::string& _id) { contentType_ = "application/x-www-form-urlencoded"; url_ = "https://api.twitter.com/1.1/statuses/retweet/" + _id + ".json"; - } +} - CocoaTweet::API::Model::Tweet Retweet::process(std::weak_ptr _oauth){ +CocoaTweet::API::Model::Tweet Retweet::process( + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; - HttpPost::process(_oauth, - [&tweet](const unsigned int _responseCode, const std::string& _rcv) { - tweet = CocoaTweet::API::Model::Tweet(_responseCode, _rcv); - }); + HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { + tweet = CocoaTweet::API::Model::Tweet(_rcv); + }); return tweet; - } } +} // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/retweet.h b/src/cocoatweet/api/status/retweet.h index 500f5e4..add7a97 100644 --- a/src/cocoatweet/api/status/retweet.h +++ b/src/cocoatweet/api/status/retweet.h @@ -5,14 +5,14 @@ #include namespace CocoaTweet::API::Statuses { - class Retweet : public CocoaTweet::API::Interface::HttpPost{ - public: - Retweet(); +class Retweet : public CocoaTweet::API::Interface::HttpPost { +public: + Retweet(); - void id(const std::string& _id); + void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); - }; -} + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); +}; +} // namespace CocoaTweet::API::Statuses #endif \ No newline at end of file diff --git a/src/cocoatweet/api/status/status.cc b/src/cocoatweet/api/status/status.cc index babf1c8..bf3a4c9 100644 --- a/src/cocoatweet/api/status/status.cc +++ b/src/cocoatweet/api/status/status.cc @@ -79,9 +79,10 @@ CocoaTweet::API::Model::Tweet Status::Retweet(const std::string& _id) const { return retweet.process(oauth_); } -std::vector Status::UserTimeline(const std::string& _screenName) const{ +std::vector Status::UserTimeline( + const std::string& _screenName) const { CocoaTweet::API::Statuses::UserTimeline userTimeline; - userTimeline.screenName(_screenName); - return userTimeline.process(oauth_); + userTimeline.screenName(_screenName); + return userTimeline.process(oauth_); } } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.h b/src/cocoatweet/api/status/status.h index b7d4113..780772e 100644 --- a/src/cocoatweet/api/status/status.h +++ b/src/cocoatweet/api/status/status.h @@ -62,7 +62,7 @@ public: CocoaTweet::API::Model::Tweet Retweet(const std::string& _id) const; - std::vector UserTimeline(const std::string& _screenName) const; + std::vector UserTimeline(const std::string& _screenName) const; private: Options defaultOpt_; diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index 3395d65..866ad8b 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -55,10 +55,9 @@ void Update::failDMCommands(bool _fail) { 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); - }); + HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { + tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); + }); return tweet; } diff --git a/src/cocoatweet/api/status/userTimeline.cc b/src/cocoatweet/api/status/userTimeline.cc index 7679ce9..52f3405 100644 --- a/src/cocoatweet/api/status/userTimeline.cc +++ b/src/cocoatweet/api/status/userTimeline.cc @@ -16,10 +16,10 @@ void UserTimeline::screenName(const std::string& _screenName) { std::vector UserTimeline::process( std::weak_ptr _oauth) { std::vector tweet; - HttpGet::process(_oauth, [&tweet](const unsigned int _responseCode, const std::string& _rsv) { - auto json = nlohmann::json::parse(_rsv); + HttpGet::process(_oauth, [&tweet](const std::string& _rcv) { + auto json = nlohmann::json::parse(_rcv); for (auto j : json) { - tweet.push_back(CocoaTweet::API::Model::Tweet::parse(_responseCode, j.dump())); + tweet.push_back(CocoaTweet::API::Model::Tweet::parse(j.dump())); std::cout << j.dump() << std::endl; } }); diff --git a/test/api/model/tweet.cc b/test/api/model/tweet.cc index c7450c7..4420f5b 100644 --- a/test/api/model/tweet.cc +++ b/test/api/model/tweet.cc @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(test02) { "source" : "Twitter for Android" })"; - CocoaTweet::API::Model::Tweet tweet(200, json); + CocoaTweet::API::Model::Tweet tweet(json); BOOST_TEST(tweet.id() == "1234567890"); BOOST_TEST(tweet.createdAt() == "Thu Mar 04 00:00:00 +0000 2021"); BOOST_TEST(tweet.text() == "tweet"); @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(test03) { }] })"; - BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(401, json), + BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(json), CocoaTweet::Exception::AuthenticateException); } @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(test04) { }] })"; - BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(429, json), + BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(json), CocoaTweet::Exception::RateLimitException); } @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(test05) { }] })"; - BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(403, json), + BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(json), CocoaTweet::Exception::RateLimitException); } @@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE(test06) { }] })"; - BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(403, json), + BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(json), CocoaTweet::Exception::TweetTooLongException); } @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(test07) { }] })"; - BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(403, json), + BOOST_CHECK_THROW(CocoaTweet::API::Model::Tweet(json), CocoaTweet::Exception::TweetDuplicateException); } BOOST_AUTO_TEST_SUITE_END()