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
+14 -18
View File
@@ -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;
+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
+5 -26
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());
}
}
tweet.id(j["id_str"]);
tweet.createdAt(j["created_at"]);
tweet.text(j["text"]);
tweet.source(j["source"]);
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