Model::TweetからHttpPostにエラーハンドリングを移動した(#67)

This commit is contained in:
keita
2021-03-19 22:55:34 +09:00
parent 73e0b9f902
commit 9413cd8ce2
20 changed files with 107 additions and 122 deletions
+2 -3
View File
@@ -13,9 +13,8 @@ void Create::id(const std::string& _id) {
CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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;
+2 -3
View File
@@ -14,9 +14,8 @@ void Destroy::id(const std::string& _id) {
CocoaTweet::API::Model::Tweet Destroy::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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;
+2 -3
View File
@@ -12,9 +12,8 @@ protected:
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;
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(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);
+2 -2
View File
@@ -15,7 +15,7 @@ extern "C" {
namespace CocoaTweet::API::Interface {
void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback) {
std::function<void(const std::string&)> _callback) {
auto url = url_;
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
@@ -92,7 +92,7 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
}
if (_callback) {
_callback(responseCode, rcv);
_callback(rcv);
}
}
} // namespace CocoaTweet::API::Interface
+1 -1
View File
@@ -16,7 +16,7 @@ protected:
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback);
std::function<void(const std::string&)> _callback);
};
} // namespace CocoaTweet::API::Interface
+29 -2
View File
@@ -1,5 +1,11 @@
#include <cocoatweet/api/interface/httpPost.h>
#include "cocoatweet/util/util.h"
#include <cocoatweet/exception/tweetNotFoundException.h>
#include <cocoatweet/exception/authenticateException.h>
#include <cocoatweet/exception/tweetDuplicateException.h>
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#include "nlohmann/json.hpp"
#include <iterator>
#include <memory>
#include <vector>
@@ -15,7 +21,7 @@ extern "C" {
namespace CocoaTweet::API::Interface {
void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback) {
std::function<void(const std::string&)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam();
@@ -103,8 +109,29 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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<int>() == 144) {
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 88 || error.get<int>() == 185) {
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
}
}
if (_callback) {
_callback(responseCode, rcv);
_callback(rcv);
}
}
} // namespace CocoaTweet::API::Interface
+1 -1
View File
@@ -16,7 +16,7 @@ protected:
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback);
std::function<void(const std::string&)> _callback);
};
} // namespace CocoaTweet::API::Interface
+5 -8
View File
@@ -43,9 +43,8 @@ CocoaTweet::API::Model::MediaStore Upload::process(
bodyParam_.insert_or_assign(
"media_type", mimeType.at(std::filesystem::path(media_).extension().string<char>()));
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,10 +75,8 @@ 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);
});
}
+1 -5
View File
@@ -3,11 +3,10 @@
#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"]);
}
@@ -26,9 +25,6 @@ MediaStore MediaStore::parse(const unsigned int _responseCode, const std::string
media.state(j["processing_info"]["state"]);
media.remain(j["processing_info"]["check_after_secs"].get<unsigned int>());
}
} else {
throw new CocoaTweet::Exception::Exception(j["error"]);
}
return media;
}
+2 -7
View File
@@ -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
+1 -22
View File
@@ -1,36 +1,15 @@
#include <cocoatweet/api/model/tweet.h>
#include <cocoatweet/exception/tweetNotFoundException.h>
#include <cocoatweet/exception/authenticateException.h>
#include <cocoatweet/exception/tweetDuplicateException.h>
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#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<int>() == 144) {
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 88 || error.get<int>() == 185) {
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
}
}
return tweet;
}
+2 -7
View File
@@ -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
+2 -3
View File
@@ -11,9 +11,8 @@ void Destroy::id(const std::string _id) {
CocoaTweet::API::Model::Tweet Destroy::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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;
}
+8 -8
View File
@@ -2,20 +2,20 @@
#include <iostream>
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<CocoaTweet::OAuth::OAuth1> _oauth){
CocoaTweet::API::Model::Tweet Retweet::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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
+4 -4
View File
@@ -5,14 +5,14 @@
#include <cocoatweet/api/model/tweet.h>
namespace CocoaTweet::API::Statuses {
class Retweet : public CocoaTweet::API::Interface::HttpPost{
public:
class Retweet : public CocoaTweet::API::Interface::HttpPost {
public:
Retweet();
void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
};
}
};
} // namespace CocoaTweet::API::Statuses
#endif
+2 -1
View File
@@ -79,7 +79,8 @@ CocoaTweet::API::Model::Tweet Status::Retweet(const std::string& _id) const {
return retweet.process(oauth_);
}
std::vector<CocoaTweet::API::Model::Tweet> Status::UserTimeline(const std::string& _screenName) const{
std::vector<CocoaTweet::API::Model::Tweet> Status::UserTimeline(
const std::string& _screenName) const {
CocoaTweet::API::Statuses::UserTimeline userTimeline;
userTimeline.screenName(_screenName);
return userTimeline.process(oauth_);
+2 -3
View File
@@ -55,9 +55,8 @@ void Update::failDMCommands(bool _fail) {
CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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;
}
+3 -3
View File
@@ -16,10 +16,10 @@ void UserTimeline::screenName(const std::string& _screenName) {
std::vector<CocoaTweet::API::Model::Tweet> UserTimeline::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
std::vector<CocoaTweet::API::Model::Tweet> 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;
}
});
+6 -6
View File
@@ -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()