コメント書いたり

This commit is contained in:
keita
2021-03-11 21:40:10 +09:00
parent 16daa46810
commit d2425d009d
11 changed files with 127 additions and 63 deletions
+6
View File
@@ -6,9 +6,15 @@
#include <cocoatweet/api/interface/httpBase.h> #include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
/// @brief class for Send request with POST method
class HttpPost : public HttpBase { class HttpPost : public HttpBase {
public: public:
protected: protected:
/// @brief Send HTTP/POST using OAuth object
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to OAuth object to
/// authenticate
/// @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, void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
std::function<void(const unsigned int, const std::string&)> _callback); std::function<void(const unsigned int, const std::string&)> _callback);
}; };
+1 -1
View File
@@ -10,4 +10,4 @@ CocoaTweet::API::Model::MediaStore Media::Upload(const std::string& _media) cons
upload.media(_media); upload.media(_media);
return upload.process(oauth_); return upload.process(oauth_);
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Medias
+1 -1
View File
@@ -23,6 +23,6 @@ public:
private: private:
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Medias
#endif #endif
+6 -4
View File
@@ -21,15 +21,14 @@ void Upload::mediaId(const std::string& _mediaId) {}
CocoaTweet::API::Model::MediaStore Upload::process( CocoaTweet::API::Model::MediaStore Upload::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
auto backup = bodyParam_;
CocoaTweet::API::Model::MediaStore media; CocoaTweet::API::Model::MediaStore media;
std::ifstream ifs(media_, std::ios::binary); 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<char>(ifs)), std::istreambuf_iterator<char>()); std::string data((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ifs.close(); ifs.close();
bodyParam_.insert_or_assign("total_bytes", std::to_string(data.size()));
// INIT // INIT
{ {
contentType_ = "application/x-www-form-urlencoded"; contentType_ = "application/x-www-form-urlencoded";
@@ -72,6 +71,7 @@ CocoaTweet::API::Model::MediaStore Upload::process(
bodyParam_.erase("media"); bodyParam_.erase("media");
HttpPost::process(_oauth, HttpPost::process(_oauth,
[&media](const unsigned int _responseCode, const std::string& _rsv) { [&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); media = CocoaTweet::API::Model::MediaStore::parse(_responseCode, _rsv);
}); });
} }
@@ -79,6 +79,8 @@ CocoaTweet::API::Model::MediaStore Upload::process(
// STATUS if needed // STATUS if needed
{} {}
bodyParam_ = backup;
return media; return media;
} }
} // namespace CocoaTweet::API::Medias } // namespace CocoaTweet::API::Medias
+14 -1
View File
@@ -7,16 +7,29 @@
#include <filesystem> #include <filesystem>
namespace CocoaTweet::API::Medias { namespace CocoaTweet::API::Medias {
/// @brief entry point for using media/upload endpoint
class Upload : public CocoaTweet::API::Interface::HttpPost { class Upload : public CocoaTweet::API::Interface::HttpPost {
private: private:
std::string media_; std::string media_;
static const std::map<std::string, std::string> mimeType; static const std::map<std::string, std::string> mimeType;
;
public: public:
/// @brief default constructor
Upload(); 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); void media(const std::string& _media);
/// @brief set media id(no affect to process. will be obsoleted)
void mediaId(const std::string& _mediaId); void mediaId(const std::string& _mediaId);
/// @brief upload media
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _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<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::MediaStore process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
}; };
} // namespace CocoaTweet::API::Medias } // namespace CocoaTweet::API::Medias
+23 -17
View File
@@ -21,55 +21,61 @@ public:
MediaStore(const unsigned int _responseCode, const std::string& _json) MediaStore(const unsigned int _responseCode, const std::string& _json)
: MediaStore(MediaStore::parse(_responseCode, _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 /// @param[in] const unsigned int _responseCode : http status code which received when post
/// request /// request
/// @param[in] const std::string& _json : received content from twitter endpoint /// @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); static MediaStore parse(const unsigned int _responseCode, const std::string& _json);
/// @brief set id of tweet /// @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 /// @param[out] none
void id(const std::string _id); void id(const std::string _id);
/// @brief set created time of tweet /// @brief set media size in byte
/// @param[in] const std::string _at : tweet created time to set /// @param[in] const unsigned int _size : media size in byte
/// @param[out] none /// @param[out] none
void size(const unsigned int _size); void size(const unsigned int _size);
/// @brief set tweet text /// @brief set remaining time to expire the media
/// @param[in] const std::string _text : text of tweet to set /// @param[in] const unsigned int : remaining time to expire the media ib sec
/// @param[out] none /// @param[out] none
void expires(const unsigned int _ex); void expires(const unsigned int _ex);
/// @brief set tweet source /// @brief set media processing status
/// @param[in] const std::string _source : source information to set /// @param[in] const std::string _state : media processed status
/// @param[out] none /// @param[out] none
void state(const std::string _state); 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); void remain(const unsigned int _remain);
/// @brief get tweet id /// @brief get media id
/// @param[in] none /// @param[in] none
/// @param[out] const std::string : tweet id /// @param[out] const std::string : media id
const std::string id() const; const std::string id() const;
/// @brief get tweet create time /// @brief get media size
/// @param[in] none /// @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; const unsigned int size() const;
/// @brief get tweet text /// @brief get remaining time to expire the media
/// @param[in] none /// @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; const unsigned int expire() const;
/// @brief get tweet source information /// @brief get media processing status
/// @param[in] none /// @param[in] none
/// @param[out] const std::string : source information /// @param[out] const std::string _state : media processed status
const std::string state() const; 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; const unsigned int remain() const;
private: private:
+1 -1
View File
@@ -19,7 +19,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] CocoaTweet::API::Model::Tweet : request result
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 } // namespace CocoaTweet::API::Statuses
+2 -1
View File
@@ -13,7 +13,8 @@ CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status) const {
return update.process(oauth_); 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; CocoaTweet::API::Statuses::Update update;
update.status(_status); update.status(_status);
+29 -2
View File
@@ -7,9 +7,10 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
/// @brief Entory point for statuses/*
class Status : public groupInterface { class Status : public groupInterface {
public: public:
struct Options { struct Options {
std::string replyToStatusId; std::string replyToStatusId;
bool autoPopulateReplyMetaData = false; bool autoPopulateReplyMetaData = false;
@@ -21,15 +22,41 @@ struct Options{
bool enableDMCommands = false; bool enableDMCommands = false;
bool failDMCommands = true; bool failDMCommands = true;
}; };
/// @brief primary constructor to allow for create NON-INITIALIZED object
Status() = default; Status() = default;
/// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _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;
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<std::string> _mediaId : media id which posted with tweet
/// @param[out] CocoaTweet::API::Model::Tweet : Tweet result
CocoaTweet::API::Model::Tweet Update(const std::string& _status, CocoaTweet::API::Model::Tweet Update(const std::string& _status,
std::vector<std::string> _mediaId) const; std::vector<std::string> _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; CocoaTweet::API::Model::Tweet Destroy(const std::string& _id) const;
private: private:
+9
View File
@@ -8,9 +8,15 @@
#include <memory> #include <memory>
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
/// @brief class for using statuses/update endpoint
class Update : public CocoaTweet::API::Interface::HttpPost { class Update : public CocoaTweet::API::Interface::HttpPost {
public: public:
/// @brief primary constructor
Update(); 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 status(const std::string _status);
void mediaId(const std::vector<std::string> _media); void mediaId(const std::vector<std::string> _media);
@@ -33,6 +39,9 @@ void enableDMCommands(bool _enable);
void failDMCommands(bool _fail); void failDMCommands(bool _fail);
/// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
private: private: