From d2425d009daa5e4c0c54340583621d1a7ba11c60 Mon Sep 17 00:00:00 2001 From: keita Date: Thu, 11 Mar 2021 21:40:10 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E6=9B=B8?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=82=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cocoatweet/api/interface/httpPost.h | 6 +++ src/cocoatweet/api/media/media.cc | 4 +- src/cocoatweet/api/media/media.h | 2 +- src/cocoatweet/api/media/upload.cc | 10 +++-- src/cocoatweet/api/media/upload.h | 15 ++++++- src/cocoatweet/api/model/mediaStore.h | 40 +++++++++++-------- src/cocoatweet/api/status/destroy.h | 2 +- src/cocoatweet/api/status/status.cc | 21 +++++----- src/cocoatweet/api/status/status.h | 53 +++++++++++++++++++------ src/cocoatweet/api/status/update.cc | 18 ++++----- src/cocoatweet/api/status/update.h | 19 ++++++--- 11 files changed, 127 insertions(+), 63 deletions(-) diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index e4768e7..2547796 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -6,9 +6,15 @@ #include namespace CocoaTweet::API::Interface { +/// @brief class for Send request with POST method class HttpPost : public HttpBase { public: protected: + /// @brief Send HTTP/POST using OAuth object + /// @param[in] std::weak_ptr _oauth : pointer to OAuth object to + /// authenticate + /// @param[in] std::function _callback : + /// callback method for processing to response void process(std::weak_ptr _oauth, std::function _callback); }; diff --git a/src/cocoatweet/api/media/media.cc b/src/cocoatweet/api/media/media.cc index 8826915..822e2ac 100644 --- a/src/cocoatweet/api/media/media.cc +++ b/src/cocoatweet/api/media/media.cc @@ -1,7 +1,7 @@ #include "cocoatweet/api/media/media.h" namespace CocoaTweet::API::Medias { - Media::Media(std::shared_ptr _oauth) { +Media::Media(std::shared_ptr _oauth) { oauth_ = _oauth; } @@ -10,4 +10,4 @@ CocoaTweet::API::Model::MediaStore Media::Upload(const std::string& _media) cons upload.media(_media); return upload.process(oauth_); } -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/media/media.h b/src/cocoatweet/api/media/media.h index 0cd46b0..53220b0 100644 --- a/src/cocoatweet/api/media/media.h +++ b/src/cocoatweet/api/media/media.h @@ -23,6 +23,6 @@ public: private: }; -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::Medias #endif diff --git a/src/cocoatweet/api/media/upload.cc b/src/cocoatweet/api/media/upload.cc index 1410419..01dff7d 100644 --- a/src/cocoatweet/api/media/upload.cc +++ b/src/cocoatweet/api/media/upload.cc @@ -21,15 +21,14 @@ void Upload::mediaId(const std::string& _mediaId) {} CocoaTweet::API::Model::MediaStore Upload::process( std::weak_ptr _oauth) { + auto backup = bodyParam_; CocoaTweet::API::Model::MediaStore media; std::ifstream ifs(media_, std::ios::binary); - ifs.seekg(0, std::ios::end); - unsigned long long size = ifs.tellg(); - bodyParam_.insert_or_assign("total_bytes", std::to_string(size)); - ifs.seekg(0); std::string data((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); ifs.close(); + bodyParam_.insert_or_assign("total_bytes", std::to_string(data.size())); + // INIT { contentType_ = "application/x-www-form-urlencoded"; @@ -72,6 +71,7 @@ CocoaTweet::API::Model::MediaStore Upload::process( 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); }); } @@ -79,6 +79,8 @@ CocoaTweet::API::Model::MediaStore Upload::process( // STATUS if needed {} + bodyParam_ = backup; + return media; } } // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/media/upload.h b/src/cocoatweet/api/media/upload.h index 1d28f9c..a021855 100644 --- a/src/cocoatweet/api/media/upload.h +++ b/src/cocoatweet/api/media/upload.h @@ -7,16 +7,29 @@ #include namespace CocoaTweet::API::Medias { +/// @brief entry point for using media/upload endpoint class Upload : public CocoaTweet::API::Interface::HttpPost { private: std::string media_; static const std::map mimeType; - ; public: + /// @brief default constructor Upload(); + + /// @brief set media file with absolute path + /// @param[in] const std::string& _media : absolute path to media should be uploaded + /// @param[out] none void media(const std::string& _media); + + /// @brief set media id(no affect to process. will be obsoleted) void mediaId(const std::string& _mediaId); + + /// @brief upload media + /// @param[in] std::weak_ptr _oauth : pointer to OAuth object for + /// authenticate + /// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post + /// tweet. CocoaTweet::API::Model::MediaStore process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/model/mediaStore.h b/src/cocoatweet/api/model/mediaStore.h index c9af26d..145282b 100644 --- a/src/cocoatweet/api/model/mediaStore.h +++ b/src/cocoatweet/api/model/mediaStore.h @@ -21,55 +21,61 @@ public: MediaStore(const unsigned int _responseCode, const std::string& _json) : MediaStore(MediaStore::parse(_responseCode, _json)) {} - /// @brief response parser for tweet object + /// @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::Tweet + /// @param[out] CocoaTweet::API::Model::MediaStore static MediaStore parse(const unsigned int _responseCode, const std::string& _json); /// @brief set id of tweet - /// @param[in] const std::string _id : tweet id to set + /// @param[in] const std::string _id : media id to set /// @param[out] none void id(const std::string _id); - /// @brief set created time of tweet - /// @param[in] const std::string _at : tweet created time to set + /// @brief set media size in byte + /// @param[in] const unsigned int _size : media size in byte /// @param[out] none void size(const unsigned int _size); - /// @brief set tweet text - /// @param[in] const std::string _text : text of tweet to set + /// @brief set remaining time to expire the media + /// @param[in] const unsigned int : remaining time to expire the media ib sec /// @param[out] none void expires(const unsigned int _ex); - /// @brief set tweet source - /// @param[in] const std::string _source : source information to set + /// @brief set media processing status + /// @param[in] const std::string _state : media processed status /// @param[out] none void state(const std::string _state); + /// @brief set how second need for upload complete + /// @param[in] needed time to upload complete on server in second + /// @param[out] none void remain(const unsigned int _remain); - /// @brief get tweet id + /// @brief get media id /// @param[in] none - /// @param[out] const std::string : tweet id + /// @param[out] const std::string : media id const std::string id() const; - /// @brief get tweet create time + /// @brief get media size /// @param[in] none - /// @param[out] const std::string : time of tweet created time + /// @param[out] const unsigned int : media size in byte const unsigned int size() const; - /// @brief get tweet text + /// @brief get remaining time to expire the media /// @param[in] none - /// @param[out] const std::string : tweet text + /// @param[out] const unsigned int : remaining time to expire the media ib sec const unsigned int expire() const; - /// @brief get tweet source information + /// @brief get media processing status /// @param[in] none - /// @param[out] const std::string : source information + /// @param[out] const std::string _state : media processed status const std::string state() const; + /// @brief set how second need for upload complete + /// @param[in] none + /// @param[out] needed time to upload complete on server in second const unsigned int remain() const; private: diff --git a/src/cocoatweet/api/status/destroy.h b/src/cocoatweet/api/status/destroy.h index 9cc8dad..6e2a036 100644 --- a/src/cocoatweet/api/status/destroy.h +++ b/src/cocoatweet/api/status/destroy.h @@ -19,7 +19,7 @@ public: /// @brief process request for endpoint /// @param[in] std::weak_ptr _oauth : pointer to oauth object - /// @param[out] none + /// @param[out] CocoaTweet::API::Model::Tweet : request result CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.cc b/src/cocoatweet/api/status/status.cc index a5c1ad8..dff2237 100644 --- a/src/cocoatweet/api/status/status.cc +++ b/src/cocoatweet/api/status/status.cc @@ -13,43 +13,44 @@ CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status) const { return update.process(oauth_); } -CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status, const Options _options) const{ +CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status, + const Options _options) const { CocoaTweet::API::Statuses::Update update; update.status(_status); - if(_options.replyToStatusId != defaultOpt_.replyToStatusId){ + if (_options.replyToStatusId != defaultOpt_.replyToStatusId) { update.replyToStatusId(_options.replyToStatusId); } - if(_options.autoPopulateReplyMetaData != defaultOpt_.autoPopulateReplyMetaData){ + if (_options.autoPopulateReplyMetaData != defaultOpt_.autoPopulateReplyMetaData) { update.autoPopulateReplyMetaData(_options.autoPopulateReplyMetaData); } - if(_options.excludeReplyUserId != defaultOpt_.excludeReplyUserId){ + if (_options.excludeReplyUserId != defaultOpt_.excludeReplyUserId) { update.excludeReplyUserId(_options.excludeReplyUserId); } - if(_options.attachmentUrl != defaultOpt_.attachmentUrl){ + if (_options.attachmentUrl != defaultOpt_.attachmentUrl) { update.attachmentUrl(_options.attachmentUrl); } - if(_options.coord != defaultOpt_.coord){ + if (_options.coord != defaultOpt_.coord) { update.coord(_options.coord); } - if(_options.displayCoord != defaultOpt_.displayCoord){ + if (_options.displayCoord != defaultOpt_.displayCoord) { update.displayCoord(_options.displayCoord); } - if(_options.trimUser != defaultOpt_.trimUser){ + if (_options.trimUser != defaultOpt_.trimUser) { update.trimUser(_options.trimUser); } - if(_options.enableDMCommands != defaultOpt_.enableDMCommands){ + if (_options.enableDMCommands != defaultOpt_.enableDMCommands) { update.enableDMCommands(_options.enableDMCommands); } - if(_options.failDMCommands != defaultOpt_.failDMCommands){ + if (_options.failDMCommands != defaultOpt_.failDMCommands) { update.failDMCommands(_options.failDMCommands); } diff --git a/src/cocoatweet/api/status/status.h b/src/cocoatweet/api/status/status.h index f9a62e4..034d2cd 100644 --- a/src/cocoatweet/api/status/status.h +++ b/src/cocoatweet/api/status/status.h @@ -7,33 +7,60 @@ #include #include namespace CocoaTweet::API::Statuses { + +/// @brief Entory point for statuses/* class Status : public groupInterface { public: + struct Options { + std::string replyToStatusId; + bool autoPopulateReplyMetaData = false; + std::vector excludeReplyUserId; + std::string attachmentUrl; + std::pair coord; + bool displayCoord = false; + bool trimUser = false; + bool enableDMCommands = false; + bool failDMCommands = true; + }; -struct Options{ - std::string replyToStatusId; - bool autoPopulateReplyMetaData = false; - std::vector excludeReplyUserId; - std::string attachmentUrl; - std::pair coord; - bool displayCoord = false; - bool trimUser = false; - bool enableDMCommands = false; - bool failDMCommands = true; -}; + /// @brief primary constructor to allow for create NON-INITIALIZED object Status() = default; + + /// @brief constructor which finally should to be called. + /// @param[in] std::shared_ptr : pointer to OAuth object Status(std::shared_ptr _oauth); + + /// @brief send request to statuses/update with specified status + /// @details this function throws CocoaTweet::Exception::* if something happen + /// @param[in] const std::string& : tweet text + /// @param[out] CocoaTweet::API::Model::Tweet : Tweet result CocoaTweet::API::Model::Tweet Update(const std::string& _status) const; - CocoaTweet::API::Model::Tweet Update(const std::string& _status, const Options _options) const; + /// @brief send request to statuses/update with specified status + /// @details this function throws CocoaTweet::Exception::* if something happen + /// @param[in] const std::string& : tweet text + /// @param[in] const CocoaTweet::API::Statuses::Status::Options option : status update options + /// for more parameters + /// @param[out] CocoaTweet::API::Model::Tweet : Tweet result + CocoaTweet::API::Model::Tweet Update(const std::string& _status, + const Options _options) const; + /// @brief send request to statuses/update with specified status + /// @details this function throws CocoaTweet::Exception::* if something happen + /// @param[in] const std::string& : tweet text + /// @param[in] std::vector _mediaId : media id which posted with tweet + /// @param[out] CocoaTweet::API::Model::Tweet : Tweet result CocoaTweet::API::Model::Tweet Update(const std::string& _status, std::vector _mediaId) const; + /// @brief send request to statuses/destroy with specified id + /// @details this function throws CocoaTweet::Exception::* if something happen + /// @param[in] const std::string& : tweet id which should be delete + /// @param[out] CocoaTweet::API::Model::Tweet : Destroy result CocoaTweet::API::Model::Tweet Destroy(const std::string& _id) const; private: -Options defaultOpt_; + Options defaultOpt_; }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index 3f2a171..3395d65 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -16,40 +16,40 @@ void Update::mediaId(const std::vector _media) { bodyParam_.insert_or_assign("media_ids", CocoaTweet::Util::join(_media, ",")); } -void Update::replyToStatusId(const std::string _reply){ +void Update::replyToStatusId(const std::string _reply) { bodyParam_.insert_or_assign("in_reply_to_status_id", _reply); } -void Update::autoPopulateReplyMetaData(bool _meta){ +void Update::autoPopulateReplyMetaData(bool _meta) { bodyParam_.insert_or_assign("auto_populate_reply_metadata", std::to_string(_meta)); } -void Update::excludeReplyUserId(const std::vector _ex){ +void Update::excludeReplyUserId(const std::vector _ex) { bodyParam_.insert_or_assign("exclude_reply_user_ids", CocoaTweet::Util::join(_ex, ",")); } -void Update::attachmentUrl(const std::string _url){ +void Update::attachmentUrl(const std::string _url) { bodyParam_.insert_or_assign("attachment_url", _url); } -void Update::coord(std::pair _coord){ +void Update::coord(std::pair _coord) { bodyParam_.insert_or_assign("lat", _coord.first); bodyParam_.insert_or_assign("long", _coord.second); } -void Update::displayCoord(bool _disp){ +void Update::displayCoord(bool _disp) { bodyParam_.insert_or_assign("display_coordinates", std::to_string(_disp)); } -void Update::trimUser(bool _trim){ +void Update::trimUser(bool _trim) { bodyParam_.insert_or_assign("trim_user", std::to_string(_trim)); } -void Update::enableDMCommands(bool _enable){ +void Update::enableDMCommands(bool _enable) { bodyParam_.insert_or_assign("enable_dmcommands", std::to_string(_enable)); } -void Update::failDMCommands(bool _fail){ +void Update::failDMCommands(bool _fail) { bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail)); } diff --git a/src/cocoatweet/api/status/update.h b/src/cocoatweet/api/status/update.h index 8ee384e..1f3ff38 100644 --- a/src/cocoatweet/api/status/update.h +++ b/src/cocoatweet/api/status/update.h @@ -8,9 +8,15 @@ #include namespace CocoaTweet::API::Statuses { +/// @brief class for using statuses/update endpoint class Update : public CocoaTweet::API::Interface::HttpPost { public: + /// @brief primary constructor Update(); + + /// @brief set tweet text for sending request to statuses/update + /// @param[in] const std::string _status : tweet text + /// @param[out] none void status(const std::string _status); void mediaId(const std::vector _media); @@ -23,16 +29,19 @@ public: void attachmentUrl(const std::string _url); -void coord(std::pair _coord); + void coord(std::pair _coord); -void displayCoord(bool _disp); + void displayCoord(bool _disp); -void trimUser(bool _trim); + void trimUser(bool _trim); -void enableDMCommands(bool _enable); + void enableDMCommands(bool _enable); -void failDMCommands(bool _fail); + void failDMCommands(bool _fail); + /// @brief process request for endpoint + /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[out] CocoaTweet::API::Model::Tweet : request result CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); private: