Model::TweetからHttpPostにエラーハンドリングを移動した(#67)
This commit is contained in:
@@ -13,10 +13,9 @@ 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;
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@ 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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -43,10 +43,9 @@ 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,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
|
||||
|
||||
@@ -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<unsigned int>());
|
||||
}
|
||||
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<unsigned int>());
|
||||
}
|
||||
|
||||
return media;
|
||||
|
||||
@@ -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,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());
|
||||
}
|
||||
}
|
||||
tweet.id(j["id_str"]);
|
||||
tweet.createdAt(j["created_at"]);
|
||||
tweet.text(j["text"]);
|
||||
tweet.source(j["source"]);
|
||||
|
||||
return tweet;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,10 +11,9 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
#include <cocoatweet/api/model/tweet.h>
|
||||
|
||||
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<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||
};
|
||||
}
|
||||
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||
};
|
||||
} // namespace CocoaTweet::API::Statuses
|
||||
|
||||
#endif
|
||||
@@ -79,9 +79,10 @@ 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_);
|
||||
userTimeline.screenName(_screenName);
|
||||
return userTimeline.process(oauth_);
|
||||
}
|
||||
} // namespace CocoaTweet::API::Statuses
|
||||
|
||||
@@ -62,7 +62,7 @@ public:
|
||||
|
||||
CocoaTweet::API::Model::Tweet Retweet(const std::string& _id) const;
|
||||
|
||||
std::vector<CocoaTweet::API::Model::Tweet> UserTimeline(const std::string& _screenName) const;
|
||||
std::vector<CocoaTweet::API::Model::Tweet> UserTimeline(const std::string& _screenName) const;
|
||||
|
||||
private:
|
||||
Options defaultOpt_;
|
||||
|
||||
@@ -55,10 +55,9 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user