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