各種Exceptionの用意(#53)

Tweetオブジェクトを仮実装(#54)
Tweetオブジェクトのパースに当たり、httpPostのステータスコードを取得(#54)
各種オブジェクトにパースするためにhttpPostに渡すコールバックの引数を変更
This commit is contained in:
keita
2021-02-24 14:19:14 +09:00
parent b4497728b1
commit 1505b0b139
12 changed files with 35 additions and 26 deletions
+1 -1
View File
@@ -11,6 +11,6 @@ void Create::id(const std::string& _id) {
} }
void Create::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { void Create::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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 } // namespace CocoaTweet::API::Favorites
+1 -1
View File
@@ -11,6 +11,6 @@ void Destroy::id(const std::string& _id) {
} }
void Destroy::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { void Destroy::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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 } // namespace CocoaTweet::API::Favorites
+5 -2
View File
@@ -17,7 +17,7 @@ size_t HttpPost::curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::str
} }
void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback) { std::function<void(const unsigned int, const std::string&)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock(); auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam(); auto oauthParam = oauth->oauthParam();
@@ -60,6 +60,7 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
CURL* curl; CURL* curl;
CURLcode res; CURLcode res;
std::string rcv; std::string rcv;
long responseCode;
curl = curl_easy_init(); curl = curl_easy_init();
url_ = url_; url_ = url_;
std::cout << "URL : " << url_ << std::endl; std::cout << "URL : " << url_ << std::endl;
@@ -77,15 +78,17 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
headers = curl_slist_append(headers, oauthHeader.c_str()); headers = curl_slist_append(headers, oauthHeader.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
if (res != CURLE_OK) { if (res != CURLE_OK) {
std::cout << "curl error : " << res << std::endl; std::cout << "curl error : " << res << std::endl;
exit(1); exit(1);
} }
if (_callback) { if (_callback) {
_callback(rcv); _callback(responseCode, rcv);
} }
} }
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface
+1 -1
View File
@@ -13,7 +13,7 @@ protected:
std::string url_; std::string url_;
std::string contentType_; std::string contentType_;
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(std::string)> _callback); std::function<void(const unsigned int, const std::string&)> _callback);
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream); static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
}; };
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface
+2 -3
View File
@@ -10,7 +10,6 @@ Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) {
Tweet tweet; Tweet tweet;
if (_responseCode == 200) { if (_responseCode == 200) {
Tweet tweet;
tweet.id(j["id_str"]); tweet.id(j["id_str"]);
} else { } else {
auto error = j["errors"][0]["code"]; auto error = j["errors"][0]["code"];
@@ -23,11 +22,11 @@ Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) {
return tweet; return tweet;
} }
void Tweet::id(const std::string& _id) { void Tweet::id(const std::string _id) {
id_ = _id; id_ = _id;
} }
const std::string& Tweet::id() const { const std::string Tweet::id() const {
return id_; return id_;
} }
} // namespace CocoaTweet::API::Model } // namespace CocoaTweet::API::Model
+2 -2
View File
@@ -11,8 +11,8 @@ public:
Tweet(const unsigned int _responseCode, const std::string& _json) Tweet(const unsigned int _responseCode, const std::string& _json)
: Tweet(Tweet::parse(_responseCode, _json)) {} : Tweet(Tweet::parse(_responseCode, _json)) {}
static Tweet parse(const unsigned int _responseCode, const std::string& _json); static Tweet parse(const unsigned int _responseCode, const std::string& _json);
void id(const std::string& _id); void id(const std::string _id);
const std::string& id() const; const std::string id() const;
private: private:
std::string id_; std::string id_;
+4 -4
View File
@@ -8,12 +8,12 @@ void Destroy::id(const std::string _id) {
url_ = "https://api.twitter.com/1.1/statuses/destroy/" + _id + ".json"; url_ = "https://api.twitter.com/1.1/statuses/destroy/" + _id + ".json";
} }
void Destroy::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { CocoaTweet::API::Model::Tweet Destroy::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _srv) { HttpPost::process(_oauth, [&tweet](const unsigned int _responseCode, const std::string& _rsv) {
tweet = CocoaTweet::API::Model::Tweet::parse(100, _srv); tweet = CocoaTweet::API::Model::Tweet::parse(_responseCode, _rsv);
}); });
std::cout << "afw" << std::endl; return tweet;
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+2 -1
View File
@@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <cocoatweet/api/interface/httpPost.h> #include <cocoatweet/api/interface/httpPost.h>
#include <cocoatweet/api/model/tweet.h>
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
/// @brief class for using status/destroy:id endpoint /// @brief class for using status/destroy:id endpoint
@@ -19,7 +20,7 @@ public:
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object
/// @param[out] none /// @param[out] none
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+4 -4
View File
@@ -9,15 +9,15 @@ Status::Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
oauth_ = _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; CocoaTweet::API::Statuses::Update update;
update.status(_status); 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; CocoaTweet::API::Statuses::Destroy destroy;
destroy.id(_id); destroy.id(_id);
destroy.process(oauth_); return destroy.process(oauth_);
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+3 -2
View File
@@ -3,14 +3,15 @@
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/oauth/oauth.h"
#include <cocoatweet/api/model/tweet.h>
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
class Status : public groupInterface { class Status : public groupInterface {
public: public:
Status() = default; Status() = default;
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
void Update(const std::string& _status) const; CocoaTweet::API::Model::Tweet Update(const std::string& _status) const;
void Destroy(const std::string& _id) const; CocoaTweet::API::Model::Tweet Destroy(const std::string& _id) const;
private: private:
}; };
+7 -2
View File
@@ -6,13 +6,18 @@ Update::Update() {
contentType_ = "application/x-www-form-urlencoded"; contentType_ = "application/x-www-form-urlencoded";
url_ = "https://api.twitter.com/1.1/statuses/update.json"; url_ = "https://api.twitter.com/1.1/statuses/update.json";
} }
void Update::status(const std::string _status) { void Update::status(const std::string _status) {
status_ = _status; status_ = _status;
bodyParam_.insert_or_assign("status", status_); bodyParam_.insert_or_assign("status", status_);
} }
void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
HttpPost::process(_oauth, [](const std::string& _srv) { std::cout << _srv << std::endl; }); 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 } // namespace CocoaTweet::API::Statuses
+3 -3
View File
@@ -2,15 +2,15 @@
#define COCOATWEET_API_STATUSES_UPDATE_H_ #define COCOATWEET_API_STATUSES_UPDATE_H_
#include <memory> #include <memory>
#include "cocoatweet/api/interface/httpPost.h" #include <cocoatweet/api/interface/httpPost.h>
//#include "cocoatweet/oauth/oauth.h" #include <cocoatweet/api/model/tweet.h>
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
class Update : public CocoaTweet::API::Interface::HttpPost { class Update : public CocoaTweet::API::Interface::HttpPost {
public: public:
Update(); Update();
void status(const std::string _status); void status(const std::string _status);
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
private: private:
std::string status_; std::string status_;