diff --git a/src/cocoatweet/api/api.cc b/src/cocoatweet/api/api.cc index 93aebd3..1b3804f 100644 --- a/src/cocoatweet/api/api.cc +++ b/src/cocoatweet/api/api.cc @@ -8,22 +8,21 @@ namespace CocoaTweet::API { API::API(CocoaTweet::Authentication::Key _key) { swapKey(_key); - } -void API::swapKey(const CocoaTweet::Authentication::Key _key){ - if(_key.authType() == CocoaTweet::Authentication::Key::OAUTH10A){ - oauth_ = std::make_shared(_key); - }else if(_key.authType() == CocoaTweet::Authentication::Key::OAUTH2){ - oauth_ = std::make_shared(_key); +void API::swapKey(const CocoaTweet::Authentication::Key _key) { + if (_key.authType() == CocoaTweet::Authentication::Key::OAUTH10A) { + oauth_ = std::make_shared(_key); + } else if (_key.authType() == CocoaTweet::Authentication::Key::OAUTH2) { + oauth_ = std::make_shared(_key); } user_ = Users::User(oauth_); status_ = Statuses::Status(oauth_); favorite_ = Favorites::Favorite(oauth_); media_ = Medias::Media(oauth_); directMessage_ = DirectMessages::DirectMessage(oauth_); - oauth1_ = OAuth1::OAuth(oauth_); - oauth2_ = OAuth2::OAuth2(oauth_); + oauth1_ = OAuth1::OAuth(oauth_); + oauth2_ = OAuth2::OAuth2(oauth_); } // const std::string& API::generateBearerToken() const { diff --git a/src/cocoatweet/api/directMessage/directMessage.cc b/src/cocoatweet/api/directMessage/directMessage.cc index 18c7c7a..1ae89b1 100644 --- a/src/cocoatweet/api/directMessage/directMessage.cc +++ b/src/cocoatweet/api/directMessage/directMessage.cc @@ -2,7 +2,8 @@ #include namespace CocoaTweet::API::DirectMessages { -DirectMessage::DirectMessage(std::shared_ptr _oauth) { +DirectMessage::DirectMessage( + std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/directMessage/directMessage.h b/src/cocoatweet/api/directMessage/directMessage.h index d4b2044..a9e1603 100644 --- a/src/cocoatweet/api/directMessage/directMessage.h +++ b/src/cocoatweet/api/directMessage/directMessage.h @@ -15,7 +15,8 @@ public: DirectMessage() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object DirectMessage(std::shared_ptr _oauth); void messageCreate(const std::string& _recipient, const std::string& _message); diff --git a/src/cocoatweet/api/directMessage/new.h b/src/cocoatweet/api/directMessage/new.h index b620330..ded38d6 100644 --- a/src/cocoatweet/api/directMessage/new.h +++ b/src/cocoatweet/api/directMessage/new.h @@ -17,7 +17,8 @@ public: void message(const std::string& _message); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer + /// to oauth object /// @param[out] CocoaTweet::API::Model::Tweet : request result void process(std::weak_ptr _oauth); diff --git a/src/cocoatweet/api/favorite/create.cc b/src/cocoatweet/api/favorite/create.cc index cf27dcc..4fa36d8 100644 --- a/src/cocoatweet/api/favorite/create.cc +++ b/src/cocoatweet/api/favorite/create.cc @@ -11,7 +11,8 @@ void Create::id(const std::string& _id) { bodyParam_.insert_or_assign("id", _id); } -CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr _oauth) { +CocoaTweet::API::Model::Tweet Create::process( + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet(_rcv); diff --git a/src/cocoatweet/api/favorite/create.h b/src/cocoatweet/api/favorite/create.h index d8bf406..5d7ee18 100644 --- a/src/cocoatweet/api/favorite/create.h +++ b/src/cocoatweet/api/favorite/create.h @@ -9,7 +9,8 @@ class Create : public CocoaTweet::API::Interface::HttpPost { public: Create(); void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/favorite/destroy.h b/src/cocoatweet/api/favorite/destroy.h index 7e669bf..cad1e36 100644 --- a/src/cocoatweet/api/favorite/destroy.h +++ b/src/cocoatweet/api/favorite/destroy.h @@ -9,7 +9,8 @@ class Destroy : public CocoaTweet::API::Interface::HttpPost { public: Destroy(); void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/interface/httpGet.cc b/src/cocoatweet/api/interface/httpGet.cc index 9ac4ffe..d35f1ff 100644 --- a/src/cocoatweet/api/interface/httpGet.cc +++ b/src/cocoatweet/api/interface/httpGet.cc @@ -109,7 +109,7 @@ void HttpGet::process(std::weak_ptr() == 144) { throw CocoaTweet::Exception::TweetNotFoundException(message.get().c_str()); } else if (error.get() == 32) { @@ -130,10 +130,12 @@ void HttpGet::process(std::weak_ptr().c_str()); } else if (error.get() == 186) { throw CocoaTweet::Exception::TweetTooLongException(message.get().c_str()); - }else if(error.get() == 170){ - throw CocoaTweet::Exception::MissingRequiredParamException(message.get().c_str()); - }else if(error.get() == 220){ - throw CocoaTweet::Exception::CredentialNotAllowedException(message.get().c_str()); + } else if (error.get() == 170) { + throw CocoaTweet::Exception::MissingRequiredParamException( + message.get().c_str()); + } else if (error.get() == 220) { + throw CocoaTweet::Exception::CredentialNotAllowedException( + message.get().c_str()); } else { } } diff --git a/src/cocoatweet/api/interface/httpGet.h b/src/cocoatweet/api/interface/httpGet.h index 68f3d47..c356623 100644 --- a/src/cocoatweet/api/interface/httpGet.h +++ b/src/cocoatweet/api/interface/httpGet.h @@ -11,8 +11,8 @@ class HttpGet : public virtual 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::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, diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index 56104ab..b3640f2 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -26,7 +26,7 @@ namespace CocoaTweet::API::Interface { void HttpPost::process(std::weak_ptr _oauth, std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 - auto oauth = _oauth.lock(); + auto oauth = _oauth.lock(); // auto oauthParam = oauth->oauthParam(); // auto sigingParam = oauthParam; // if (contentType_ == "application/x-www-form-urlencoded") { @@ -71,7 +71,6 @@ void HttpPost::process(std::weak_ptrcalculateAuthHeader(bodyParam_, "POST", url_); @@ -128,12 +127,12 @@ void HttpPost::process(std::weak_ptr() == 144) { @@ -148,10 +147,12 @@ void HttpPost::process(std::weak_ptr().c_str()); } else if (error.get() == 186) { throw CocoaTweet::Exception::TweetTooLongException(message.get().c_str()); - }else if(error.get() == 170){ - throw CocoaTweet::Exception::MissingRequiredParamException(message.get().c_str()); - }else if(error.get() == 220){ - throw CocoaTweet::Exception::CredentialNotAllowedException(message.get().c_str()); + } else if (error.get() == 170) { + throw CocoaTweet::Exception::MissingRequiredParamException( + message.get().c_str()); + } else if (error.get() == 220) { + throw CocoaTweet::Exception::CredentialNotAllowedException( + message.get().c_str()); } } diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index d307984..deb1d10 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -11,8 +11,8 @@ 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::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, diff --git a/src/cocoatweet/api/media/media.h b/src/cocoatweet/api/media/media.h index 9df9927..3aa3660 100644 --- a/src/cocoatweet/api/media/media.h +++ b/src/cocoatweet/api/media/media.h @@ -16,7 +16,8 @@ public: Media() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object Media(std::shared_ptr _oauth); CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const; diff --git a/src/cocoatweet/api/media/upload.h b/src/cocoatweet/api/media/upload.h index 5355207..630288b 100644 --- a/src/cocoatweet/api/media/upload.h +++ b/src/cocoatweet/api/media/upload.h @@ -26,11 +26,12 @@ public: void mediaId(const std::string& _mediaId); /// @brief upload media - /// @param[in] std::weak_ptr _oauth : pointer to OAuth object for - /// authenticate + /// @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); + CocoaTweet::API::Model::MediaStore process( + std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/model/oauthToken.cc b/src/cocoatweet/api/model/oauthToken.cc index 7933473..3fc3e80 100644 --- a/src/cocoatweet/api/model/oauthToken.cc +++ b/src/cocoatweet/api/model/oauthToken.cc @@ -1,19 +1,20 @@ #include "oauthToken.h" -namespace CocoaTweet::API::Model{ +namespace CocoaTweet::API::Model { - OAuthToken::OAuthToken(): oauthToken_(""), oauthTokenSecret_(""){} - OAuthToken::OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret): oauthToken_(_oauthToken), oauthTokenSecret_(_oauthTokenSecret) {} - const std::string& OAuthToken::oauthToken() const { - return oauthToken_; - } - const std::string& OAuthToken::oauthTokenSecret() const{ - return oauthTokenSecret_; - } - void OAuthToken::oauthToken(const std::string& _oauthToken) { - oauthToken_ = _oauthToken; - } - void OAuthToken::oauthTokenSecret(const std::string& _oauthTokenSecret){ - oauthTokenSecret_ = _oauthTokenSecret; - } -} \ No newline at end of file +OAuthToken::OAuthToken() : oauthToken_(""), oauthTokenSecret_("") {} +OAuthToken::OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret) + : oauthToken_(_oauthToken), oauthTokenSecret_(_oauthTokenSecret) {} +const std::string& OAuthToken::oauthToken() const { + return oauthToken_; +} +const std::string& OAuthToken::oauthTokenSecret() const { + return oauthTokenSecret_; +} +void OAuthToken::oauthToken(const std::string& _oauthToken) { + oauthToken_ = _oauthToken; +} +void OAuthToken::oauthTokenSecret(const std::string& _oauthTokenSecret) { + oauthTokenSecret_ = _oauthTokenSecret; +} +} // namespace CocoaTweet::API::Model \ No newline at end of file diff --git a/src/cocoatweet/api/model/oauthToken.h b/src/cocoatweet/api/model/oauthToken.h index 38a2d2f..8aad786 100644 --- a/src/cocoatweet/api/model/oauthToken.h +++ b/src/cocoatweet/api/model/oauthToken.h @@ -1,20 +1,20 @@ #ifndef COCOATWEET_API_MODEL_OAUTHTOKEN_H_ #define COCOATWEET_API_MODEL_OAUTHTOKEN_H_ -#include +#include -namespace CocoaTweet::API::Model{ - class OAuthToken{ - std::string oauthToken_; - std::string oauthTokenSecret_; +namespace CocoaTweet::API::Model { +class OAuthToken { + std::string oauthToken_; + std::string oauthTokenSecret_; - public: - OAuthToken(); - OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret); - const std::string& oauthToken() const; - const std::string& oauthTokenSecret() const; - void oauthToken(const std::string& _oauthToken); - void oauthTokenSecret(const std::string& _oauthTokenSecret); - }; -} +public: + OAuthToken(); + OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret); + const std::string& oauthToken() const; + const std::string& oauthTokenSecret() const; + void oauthToken(const std::string& _oauthToken); + void oauthTokenSecret(const std::string& _oauthTokenSecret); +}; +} // namespace CocoaTweet::API::Model #endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/accessToken.cc b/src/cocoatweet/api/oauth1/accessToken.cc index 82b85d0..3deb982 100644 --- a/src/cocoatweet/api/oauth1/accessToken.cc +++ b/src/cocoatweet/api/oauth1/accessToken.cc @@ -2,32 +2,33 @@ #include #include -namespace CocoaTweet::API::OAuth1{ - AccessToken::AccessToken(){ - contentType_ = "application/x-www-form-urlencoded"; - url_ = "https://api.twitter.com/oauth/access_token"; - } +namespace CocoaTweet::API::OAuth1 { +AccessToken::AccessToken() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/access_token"; +} - void AccessToken::oauthVerifier(const std::string& _verifier){ - bodyParam_.insert_or_assign("oauth_verifier", _verifier); - } +void AccessToken::oauthVerifier(const std::string& _verifier) { + bodyParam_.insert_or_assign("oauth_verifier", _verifier); +} - void AccessToken::oauthToken(const CocoaTweet::API::Model::OAuthToken _token){ - oauthToken_ = _token; - } +void AccessToken::oauthToken(const CocoaTweet::API::Model::OAuthToken _token) { + oauthToken_ = _token; +} - const CocoaTweet::API::Model::OAuthToken AccessToken::process(std::weak_ptr _oauth){ - CocoaTweet::API::Model::OAuthToken oauthToken; - HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv){ - auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); - if(mp.count("oauth_token")){ - oauthToken.oauthToken(mp.at("oauth_token")); - } - if(mp.count("oauth_token_secret")){ - oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); - } - std::cout << _rcv << std::endl; - }); - return oauthToken; +const CocoaTweet::API::Model::OAuthToken AccessToken::process( + std::weak_ptr _oauth) { + CocoaTweet::API::Model::OAuthToken oauthToken; + HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { + auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); + if (mp.count("oauth_token")) { + oauthToken.oauthToken(mp.at("oauth_token")); } -} \ No newline at end of file + if (mp.count("oauth_token_secret")) { + oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); + } + std::cout << _rcv << std::endl; + }); + return oauthToken; +} +} // namespace CocoaTweet::API::OAuth1 \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/accessToken.h b/src/cocoatweet/api/oauth1/accessToken.h index 3c0ef09..232fa19 100644 --- a/src/cocoatweet/api/oauth1/accessToken.h +++ b/src/cocoatweet/api/oauth1/accessToken.h @@ -3,18 +3,20 @@ #include #include -namespace CocoaTweet::API::OAuth1{ - class AccessToken: public CocoaTweet::API::Interface::HttpPost { +namespace CocoaTweet::API::OAuth1 { +class AccessToken : public CocoaTweet::API::Interface::HttpPost { private: - CocoaTweet::API::Model::OAuthToken oauthToken_; + CocoaTweet::API::Model::OAuthToken oauthToken_; + public: - AccessToken(); + AccessToken(); - void oauthVerifier(const std::string& _verifier); - void oauthToken(const CocoaTweet::API::Model::OAuthToken _token); + void oauthVerifier(const std::string& _verifier); + void oauthToken(const CocoaTweet::API::Model::OAuthToken _token); - const CocoaTweet::API::Model::OAuthToken process(std::weak_ptr _oauth) ; - }; -} + const CocoaTweet::API::Model::OAuthToken process( + std::weak_ptr _oauth); +}; +} // namespace CocoaTweet::API::OAuth1 #endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/authorize.cc b/src/cocoatweet/api/oauth1/authorize.cc index 0cd7093..86a1b20 100644 --- a/src/cocoatweet/api/oauth1/authorize.cc +++ b/src/cocoatweet/api/oauth1/authorize.cc @@ -3,31 +3,33 @@ #include #include -namespace CocoaTweet::API::OAuth1{ - Authorize::Authorize(){ - contentType_ = "application/x-www-form-urlencoded"; - url_ = "https://api.twitter.com/oauth/authorize"; - } +namespace CocoaTweet::API::OAuth1 { +Authorize::Authorize() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/authorize"; +} - void Authorize::oauthToken(const std::string& _oauthToken){ - bodyParam_.insert_or_assign("oauth_token", _oauthToken); - } +void Authorize::oauthToken(const std::string& _oauthToken) { + bodyParam_.insert_or_assign("oauth_token", _oauthToken); +} - void Authorize::forceLogin(const bool _forceLogin){ - bodyParam_.insert_or_assign("force_login", std::to_string(static_cast(_forceLogin))); - } +void Authorize::forceLogin(const bool _forceLogin) { + bodyParam_.insert_or_assign("force_login", std::to_string(static_cast(_forceLogin))); +} - void Authorize::screenName(const std::string& _screenName){ - bodyParam_.insert_or_assign("screen_name", _screenName); - } +void Authorize::screenName(const std::string& _screenName) { + bodyParam_.insert_or_assign("screen_name", _screenName); +} - const std::string Authorize::process(std::weak_ptr __unused__) { - std::vector tmp; - std::string query = ""; - for (const auto& [key, value] : bodyParam_) { - tmp.push_back(key + "=" + value); - query = CocoaTweet::Util::join(tmp, "&"); - } - return url_ + "?" + query;; - } -} \ No newline at end of file +const std::string Authorize::process( + std::weak_ptr __unused__) { + std::vector tmp; + std::string query = ""; + for (const auto& [key, value] : bodyParam_) { + tmp.push_back(key + "=" + value); + query = CocoaTweet::Util::join(tmp, "&"); + } + return url_ + "?" + query; + ; +} +} // namespace CocoaTweet::API::OAuth1 \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/authorize.h b/src/cocoatweet/api/oauth1/authorize.h index c79116d..025a3ed 100644 --- a/src/cocoatweet/api/oauth1/authorize.h +++ b/src/cocoatweet/api/oauth1/authorize.h @@ -2,19 +2,20 @@ #define COCOATWEET_API_OAUTH1_AUTHORIZE_H #include -namespace CocoaTweet::API::OAuth1{ - class Authorize: public CocoaTweet::API::Interface::HttpPost { +namespace CocoaTweet::API::OAuth1 { +class Authorize : public CocoaTweet::API::Interface::HttpPost { public: - Authorize(); + Authorize(); - void oauthToken(const std::string& _oauthToken); + void oauthToken(const std::string& _oauthToken); - void forceLogin(const bool _forceLogin); + void forceLogin(const bool _forceLogin); - void screenName(const std::string& _screenName); + void screenName(const std::string& _screenName); - const std::string process(std::weak_ptr __unused__) ; - }; -} + const std::string process( + std::weak_ptr __unused__); +}; +} // namespace CocoaTweet::API::OAuth1 #endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/oauth.cc b/src/cocoatweet/api/oauth1/oauth.cc index 71e7636..a7afb93 100644 --- a/src/cocoatweet/api/oauth1/oauth.cc +++ b/src/cocoatweet/api/oauth1/oauth.cc @@ -2,34 +2,36 @@ #include namespace CocoaTweet::API::OAuth1 { - OAuth::OAuth(std::shared_ptr _oauth){ - oauth_ = _oauth; - } +OAuth::OAuth(std::shared_ptr _oauth) { + oauth_ = _oauth; +} - CocoaTweet::API::Model::OAuthToken OAuth::requestToken(const std::string& _oauthCallback) const{ - auto key = oauth_.lock()->key(); - key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); - auto oauth = std::make_shared(key); +CocoaTweet::API::Model::OAuthToken OAuth::requestToken( + const std::string& _oauthCallback) const { + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + auto oauth = std::make_shared(key); - CocoaTweet::API::OAuth1::RequestToken requestToken; - requestToken.oauthCallback(_oauthCallback); - return requestToken.process(oauth); - } + CocoaTweet::API::OAuth1::RequestToken requestToken; + requestToken.oauthCallback(_oauthCallback); + return requestToken.process(oauth); +} - const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const{ - CocoaTweet::API::OAuth1::Authorize authorize; - authorize.oauthToken(_oauthToken.oauthToken()); - return authorize.process(oauth_); - } +const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const { + CocoaTweet::API::OAuth1::Authorize authorize; + authorize.oauthToken(_oauthToken.oauthToken()); + return authorize.process(oauth_); +} - const CocoaTweet::API::Model::OAuthToken OAuth::accessToken(const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const{ - auto key = oauth_.lock()->key(); - key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); - key.accessToken(_oauthToken.oauthToken()); - key.accessTokenSecret(_oauthToken.oauthTokenSecret()); - auto oauth = std::make_shared(key); - CocoaTweet::API::OAuth1::AccessToken accessToken; - accessToken.oauthVerifier(_verifier); - return accessToken.process(oauth); - } -} // namespace CocoaTweet::API::Statuses +const CocoaTweet::API::Model::OAuthToken OAuth::accessToken( + const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const { + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + key.accessToken(_oauthToken.oauthToken()); + key.accessTokenSecret(_oauthToken.oauthTokenSecret()); + auto oauth = std::make_shared(key); + CocoaTweet::API::OAuth1::AccessToken accessToken; + accessToken.oauthVerifier(_verifier); + return accessToken.process(oauth); +} +} // namespace CocoaTweet::API::OAuth1 diff --git a/src/cocoatweet/api/oauth1/oauth.h b/src/cocoatweet/api/oauth1/oauth.h index 1cb2439..75453d0 100644 --- a/src/cocoatweet/api/oauth1/oauth.h +++ b/src/cocoatweet/api/oauth1/oauth.h @@ -15,16 +15,17 @@ class OAuth : public groupInterface { public: /// @brief primary constructor OAuth() = default; - + /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object OAuth(std::shared_ptr _oauth); CocoaTweet::API::Model::OAuthToken requestToken(const std::string& _oauthCallback) const; const std::string authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const; - const CocoaTweet::API::Model::OAuthToken accessToken(const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const; - + const CocoaTweet::API::Model::OAuthToken accessToken( + const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const; }; -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::OAuth1 #endif diff --git a/src/cocoatweet/api/oauth1/requestToken.cc b/src/cocoatweet/api/oauth1/requestToken.cc index 95c6128..44fd54e 100644 --- a/src/cocoatweet/api/oauth1/requestToken.cc +++ b/src/cocoatweet/api/oauth1/requestToken.cc @@ -2,28 +2,29 @@ #include #include -namespace CocoaTweet::API::OAuth1{ - RequestToken::RequestToken(){ - contentType_ = "application/x-www-form-urlencoded"; - url_ = "https://api.twitter.com/oauth/request_token"; - } +namespace CocoaTweet::API::OAuth1 { +RequestToken::RequestToken() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/request_token"; +} - void RequestToken::oauthCallback(const std::string& _oauthCallback){ - bodyParam_.insert_or_assign("oauth_callback", _oauthCallback); - } +void RequestToken::oauthCallback(const std::string& _oauthCallback) { + bodyParam_.insert_or_assign("oauth_callback", _oauthCallback); +} - CocoaTweet::API::Model::OAuthToken RequestToken::process(std::weak_ptr _oauth){ - CocoaTweet::API::Model::OAuthToken oauthToken; - HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv){ - auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); - if(mp.count("oauth_token")){ - oauthToken.oauthToken(mp.at("oauth_token")); - } - if(mp.count("oauth_token_secret")){ - oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); - } - std::cout << _rcv << std::endl; - }); - return oauthToken; +CocoaTweet::API::Model::OAuthToken RequestToken::process( + std::weak_ptr _oauth) { + CocoaTweet::API::Model::OAuthToken oauthToken; + HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { + auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); + if (mp.count("oauth_token")) { + oauthToken.oauthToken(mp.at("oauth_token")); } -} \ No newline at end of file + if (mp.count("oauth_token_secret")) { + oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); + } + std::cout << _rcv << std::endl; + }); + return oauthToken; +} +} // namespace CocoaTweet::API::OAuth1 \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/requestToken.h b/src/cocoatweet/api/oauth1/requestToken.h index 32e4d6e..39741b3 100644 --- a/src/cocoatweet/api/oauth1/requestToken.h +++ b/src/cocoatweet/api/oauth1/requestToken.h @@ -4,14 +4,15 @@ #include #include -namespace CocoaTweet::API::OAuth1{ - class RequestToken: public CocoaTweet::API::Interface::HttpPost { - public: - RequestToken(); - void oauthCallback(const std::string& _oauthCallback); +namespace CocoaTweet::API::OAuth1 { +class RequestToken : public CocoaTweet::API::Interface::HttpPost { +public: + RequestToken(); + void oauthCallback(const std::string& _oauthCallback); - CocoaTweet::API::Model::OAuthToken process(std::weak_ptr _oauth); - }; -} + CocoaTweet::API::Model::OAuthToken process( + std::weak_ptr _oauth); +}; +} // namespace CocoaTweet::API::OAuth1 #endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth2/oauth2.cc b/src/cocoatweet/api/oauth2/oauth2.cc index 1313f93..fb11ce8 100644 --- a/src/cocoatweet/api/oauth2/oauth2.cc +++ b/src/cocoatweet/api/oauth2/oauth2.cc @@ -2,15 +2,15 @@ #include namespace CocoaTweet::API::OAuth2 { - OAuth2::OAuth2(std::shared_ptr _oauth){ - oauth_ = _oauth; - } +OAuth2::OAuth2(std::shared_ptr _oauth) { + oauth_ = _oauth; +} - const std::string OAuth2::token() const{ - auto key = oauth_.lock()->key(); - auto oauth = std::make_shared(key); +const std::string OAuth2::token() const { + auto key = oauth_.lock()->key(); + auto oauth = std::make_shared(key); - CocoaTweet::API::OAuth2::Token token; - return token.process(oauth); - } -} // namespace CocoaTweet::API::Statuses + CocoaTweet::API::OAuth2::Token token; + return token.process(oauth); +} +} // namespace CocoaTweet::API::OAuth2 diff --git a/src/cocoatweet/api/oauth2/oauth2.h b/src/cocoatweet/api/oauth2/oauth2.h index fb497cf..62d386c 100644 --- a/src/cocoatweet/api/oauth2/oauth2.h +++ b/src/cocoatweet/api/oauth2/oauth2.h @@ -13,14 +13,14 @@ class OAuth2 : public groupInterface { public: /// @brief primary constructor OAuth2() = default; - + /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object OAuth2(std::shared_ptr _oauth); const std::string token() const; - }; -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::OAuth2 #endif diff --git a/src/cocoatweet/api/oauth2/token.cc b/src/cocoatweet/api/oauth2/token.cc index 42bac58..3de92b9 100644 --- a/src/cocoatweet/api/oauth2/token.cc +++ b/src/cocoatweet/api/oauth2/token.cc @@ -1,18 +1,16 @@ #include #include "nlohmann/json.hpp" +namespace CocoaTweet::API::OAuth2 { +Token::Token() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth2/token"; -namespace CocoaTweet::API::OAuth2{ - Token::Token(){ - contentType_ = "application/x-www-form-urlencoded"; - url_ = "https://api.twitter.com/oauth2/token"; + bodyParam_.insert_or_assign("grant_type", "client_credentials"); +} - bodyParam_.insert_or_assign("grant_type", "client_credentials"); - } - - - -const std::string Token::process(std::weak_ptr _oauth) { +const std::string Token::process( + std::weak_ptr _oauth) { auto basic = std::make_shared(_oauth.lock()->key()); std::string bearer = ""; HttpPost::process(basic, [&bearer](const std::string& _rcv) { @@ -22,4 +20,4 @@ const std::string Token::process(std::weak_ptr #include -namespace CocoaTweet::API::OAuth2{ - class Token: public CocoaTweet::API::Interface::HttpPost{ - public: - Token(); - const std::string process(std::weak_ptr _oauth); - }; -} +namespace CocoaTweet::API::OAuth2 { +class Token : public CocoaTweet::API::Interface::HttpPost { +public: + Token(); + const std::string process( + std::weak_ptr _oauth); +}; +} // namespace CocoaTweet::API::OAuth2 #endif \ No newline at end of file diff --git a/src/cocoatweet/api/status/destroy.h b/src/cocoatweet/api/status/destroy.h index 449b956..34f0b38 100644 --- a/src/cocoatweet/api/status/destroy.h +++ b/src/cocoatweet/api/status/destroy.h @@ -18,9 +18,11 @@ public: void id(const std::string _id); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @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); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/retweet.h b/src/cocoatweet/api/status/retweet.h index 392812f..c6eb01a 100644 --- a/src/cocoatweet/api/status/retweet.h +++ b/src/cocoatweet/api/status/retweet.h @@ -11,7 +11,8 @@ public: void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.h b/src/cocoatweet/api/status/status.h index e2b8d06..4c2e738 100644 --- a/src/cocoatweet/api/status/status.h +++ b/src/cocoatweet/api/status/status.h @@ -28,7 +28,8 @@ public: Status() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object Status(std::shared_ptr _oauth); /// @brief send request to statuses/update with specified status diff --git a/src/cocoatweet/api/status/unretweet.h b/src/cocoatweet/api/status/unretweet.h index b095929..b21502a 100644 --- a/src/cocoatweet/api/status/unretweet.h +++ b/src/cocoatweet/api/status/unretweet.h @@ -11,7 +11,8 @@ public: void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index a37fbc2..2b79d25 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -53,7 +53,8 @@ void Update::failDMCommands(bool _fail) { bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail)); } -CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr _oauth) { +CocoaTweet::API::Model::Tweet Update::process( + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); diff --git a/src/cocoatweet/api/status/update.h b/src/cocoatweet/api/status/update.h index e0f3699..f11b82a 100644 --- a/src/cocoatweet/api/status/update.h +++ b/src/cocoatweet/api/status/update.h @@ -40,9 +40,11 @@ public: void failDMCommands(bool _fail); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @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); + CocoaTweet::API::Model::Tweet process( + std::weak_ptr _oauth); private: std::string status_; diff --git a/src/cocoatweet/api/status/userTimeline.h b/src/cocoatweet/api/status/userTimeline.h index 8c8f432..8de981a 100644 --- a/src/cocoatweet/api/status/userTimeline.h +++ b/src/cocoatweet/api/status/userTimeline.h @@ -19,7 +19,8 @@ public: void screenName(const std::string& _screenName); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer + /// to oauth object /// @param[out] std::vector : request result std::vector process( std::weak_ptr _oauth); diff --git a/src/cocoatweet/api/user/show.cc b/src/cocoatweet/api/user/show.cc index 968b3ed..731a4e1 100644 --- a/src/cocoatweet/api/user/show.cc +++ b/src/cocoatweet/api/user/show.cc @@ -9,14 +9,14 @@ Show::Show() { } void Show::screenName(const std::string& _screenName) { - if(bodyParam_.count("user_id") > 0){ + if (bodyParam_.count("user_id") > 0) { bodyParam_.erase("user_id"); } bodyParam_.insert_or_assign("screen_name", _screenName); } void Show::id(const std::string& _id) { - if(bodyParam_.count("screen_name") > 0){ + if (bodyParam_.count("screen_name") > 0) { bodyParam_.erase("screen_name"); } bodyParam_.insert_or_assign("id", _id); @@ -31,4 +31,4 @@ CocoaTweet::API::Model::User Show::process( return user; } -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::Users diff --git a/src/cocoatweet/api/user/show.h b/src/cocoatweet/api/user/show.h index dcde18e..5074ce0 100644 --- a/src/cocoatweet/api/user/show.h +++ b/src/cocoatweet/api/user/show.h @@ -23,7 +23,8 @@ public: void screenName(const std::string& _screenName); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer + /// to oauth object /// @param[out] CocoaTweet::API::Model::User : request result CocoaTweet::API::Model::User process( std::weak_ptr _oauth); @@ -31,6 +32,6 @@ public: private: std::string status_; }; -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::Users #endif diff --git a/src/cocoatweet/api/user/user.cc b/src/cocoatweet/api/user/user.cc index 0a06b07..773ec3a 100644 --- a/src/cocoatweet/api/user/user.cc +++ b/src/cocoatweet/api/user/user.cc @@ -1,14 +1,14 @@ #include #include -namespace CocoaTweet::API::Users{ - User::User(std::shared_ptr _oauth) { +namespace CocoaTweet::API::Users { +User::User(std::shared_ptr _oauth) { oauth_ = _oauth; } - CocoaTweet::API::Model::User User::show(const std::string& _screenName)const{ - CocoaTweet::API::Users::Show show; - show.screenName(_screenName); - return show.process(oauth_); - } -} \ No newline at end of file +CocoaTweet::API::Model::User User::show(const std::string& _screenName) const { + CocoaTweet::API::Users::Show show; + show.screenName(_screenName); + return show.process(oauth_); +} +} // namespace CocoaTweet::API::Users \ No newline at end of file diff --git a/src/cocoatweet/api/user/user.h b/src/cocoatweet/api/user/user.h index c4b904f..318ed2a 100644 --- a/src/cocoatweet/api/user/user.h +++ b/src/cocoatweet/api/user/user.h @@ -13,14 +13,14 @@ class User : public groupInterface { public: /// @brief primary constructor User() = default; - + /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object + /// @param[in] std::shared_ptr : pointer to + /// OAuth object User(std::shared_ptr _oauth); CocoaTweet::API::Model::User show(const std::string& _screenName) const; - }; -} // namespace CocoaTweet::API::Statuses +} // namespace CocoaTweet::API::Users #endif diff --git a/src/cocoatweet/authentication/authenticate.cc b/src/cocoatweet/authentication/authenticate.cc index 0239127..c316199 100644 --- a/src/cocoatweet/authentication/authenticate.cc +++ b/src/cocoatweet/authentication/authenticate.cc @@ -27,8 +27,8 @@ OAuth1::OAuth1() { method_ = AuthenticationMethod::OAUTH10A; } -OAuth1::OAuth1(const Key _key){ - key_ = _key; +OAuth1::OAuth1(const Key _key) { + key_ = _key; method_ = AuthenticationMethod::OAUTH10A; } @@ -103,7 +103,6 @@ const std::string OAuth1::version() const { return "1.0"; } - std::map OAuth1::oauthParam() const { auto tmp = std::map{{"oauth_nonce", nonce()}, {"oauth_signature_method", method()}, diff --git a/src/cocoatweet/authentication/authenticate.h b/src/cocoatweet/authentication/authenticate.h index 9bbfaac..4cec920 100644 --- a/src/cocoatweet/authentication/authenticate.h +++ b/src/cocoatweet/authentication/authenticate.h @@ -8,7 +8,7 @@ #include namespace CocoaTweet::Authentication { -class OAuth1: public AuthenticatorBase { +class OAuth1 : public AuthenticatorBase { public: OAuth1(); OAuth1(const Key _key); @@ -17,7 +17,7 @@ public: const std::string& _url); // const std::string& generateBearerToken(); - + const std::string calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url); @@ -28,7 +28,6 @@ public: std::map oauthParam() const; std::string hmacSha1(std::string _key, std::string _data); const std::string base64(const std::string& _raw); - }; } // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/authentication/authenticator.h b/src/cocoatweet/authentication/authenticator.h index 3581e0d..4d1baa2 100644 --- a/src/cocoatweet/authentication/authenticator.h +++ b/src/cocoatweet/authentication/authenticator.h @@ -2,22 +2,17 @@ #define COCOATWEET_AUTHENTICATION_AUTHENTICATORBASE_H #include -namespace CocoaTweet::Authentication{ -class AuthenticatorBase{ +namespace CocoaTweet::Authentication { +class AuthenticatorBase { public: - enum class AuthenticationMethod{ - OAUTH10A, - OAUTH2, - BASIC, - PLAIN, - NONE - }; - + enum class AuthenticationMethod { OAUTH10A, OAUTH2, BASIC, PLAIN, NONE }; + virtual const std::string calculateAuthHeader(std::map _bodyParam, - const std::string& _method, const std::string& _url) = 0; - - const Key key() const{ - return key_; + const std::string& _method, + const std::string& _url) = 0; + + const Key key() const { + return key_; } protected: @@ -29,6 +24,6 @@ protected: return realsize; } }; -} +} // namespace CocoaTweet::Authentication #endif diff --git a/src/cocoatweet/authentication/basic.cc b/src/cocoatweet/authentication/basic.cc index 357aa0e..b97f63d 100644 --- a/src/cocoatweet/authentication/basic.cc +++ b/src/cocoatweet/authentication/basic.cc @@ -27,20 +27,18 @@ Basic::Basic() { method_ = AuthenticationMethod::BASIC; } -Basic::Basic(const Key _key){ - key_ = _key; +Basic::Basic(const Key _key) { + key_ = _key; method_ = AuthenticationMethod::BASIC; } const std::string Basic::calculateAuthHeader(std::map _bodyParam, - const std::string& _method, - const std::string& _url) { - + const std::string& _method, + const std::string& _url) { auto signature = key_.consumerKey() + ":" + key_.consumerSecret(); auto k64Signature = base64(signature); auto authHeader = std::string("Authorization: Basic ") + k64Signature; return authHeader; - } const std::string Basic::base64(const std::string& _raw) { @@ -73,5 +71,4 @@ const std::string Basic::base64(const std::string& _raw) { return base64; } - } // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/authentication/basic.h b/src/cocoatweet/authentication/basic.h index 1ac8f88..06e3fd6 100644 --- a/src/cocoatweet/authentication/basic.h +++ b/src/cocoatweet/authentication/basic.h @@ -8,17 +8,16 @@ #include namespace CocoaTweet::Authentication { -class Basic: public AuthenticatorBase { +class Basic : public AuthenticatorBase { public: Basic(); Basic(const Key _key); // const std::string& generateBearerToken(); - + const std::string calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url); const std::string base64(const std::string& _raw); - }; } // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/authentication/bearer.cc b/src/cocoatweet/authentication/bearer.cc index e52f227..543b7b8 100644 --- a/src/cocoatweet/authentication/bearer.cc +++ b/src/cocoatweet/authentication/bearer.cc @@ -27,18 +27,16 @@ Bearer::Bearer() { method_ = AuthenticationMethod::OAUTH2; } -Bearer::Bearer(const Key _key){ - key_ = _key; +Bearer::Bearer(const Key _key) { + key_ = _key; method_ = AuthenticationMethod::OAUTH2; } const std::string Bearer::calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url) { - - auto authHeader = std::string("Authorization: Bearer ") + key_.bearerToken(); + auto authHeader = std::string("Authorization: Bearer ") + key_.bearerToken(); return authHeader; - } } // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/authentication/bearer.h b/src/cocoatweet/authentication/bearer.h index ab7285f..7d2f5c6 100644 --- a/src/cocoatweet/authentication/bearer.h +++ b/src/cocoatweet/authentication/bearer.h @@ -8,12 +8,11 @@ #include namespace CocoaTweet::Authentication { -class Bearer: public AuthenticatorBase { +class Bearer : public AuthenticatorBase { public: Bearer(); Bearer(const Key _key); - const std::string calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url); }; diff --git a/src/cocoatweet/authentication/key.h b/src/cocoatweet/authentication/key.h index 8d75b92..e51cb6f 100644 --- a/src/cocoatweet/authentication/key.h +++ b/src/cocoatweet/authentication/key.h @@ -7,10 +7,7 @@ namespace CocoaTweet::Authentication { class Key { public: - enum AUTH_TYPE{ - OAUTH10A, - OAUTH2 - }; + enum AUTH_TYPE { OAUTH10A, OAUTH2 }; private: std::string consumerKey_; @@ -18,17 +15,21 @@ private: std::string accessToken_; std::string accessTokenSecret_; std::string bearerToken_; - AUTH_TYPE authType_; + AUTH_TYPE authType_; + public: Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {} Key(const std::string& _consumerKey, const std::string& _consumerSecret, - const std::string& _accessToken, const std::string& _accessTokenSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH10A) + const std::string& _accessToken, const std::string& _accessTokenSecret, + const AUTH_TYPE _authType = AUTH_TYPE::OAUTH10A) : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), accessToken_(_accessToken), - accessTokenSecret_(_accessTokenSecret), authType_(_authType) {} - Key(const std::string& _consumerKey, const std::string& _consumerSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2) - : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType){} + accessTokenSecret_(_accessTokenSecret), + authType_(_authType) {} + Key(const std::string& _consumerKey, const std::string& _consumerSecret, + const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2) + : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType) {} void consumerKey(const std::string& _consumerKey) { consumerKey_ = _consumerKey; diff --git a/src/cocoatweet/authentication/plain.h b/src/cocoatweet/authentication/plain.h index 7fe3c49..e83eb82 100644 --- a/src/cocoatweet/authentication/plain.h +++ b/src/cocoatweet/authentication/plain.h @@ -3,9 +3,8 @@ #include -namespace CocoaTweet::Authentication{ - class Plain: public AuthenticatorBase { - }; -} +namespace CocoaTweet::Authentication { +class Plain : public AuthenticatorBase {}; +} // namespace CocoaTweet::Authentication #endif diff --git a/src/cocoatweet/oauth/authorize.h b/src/cocoatweet/oauth/authorize.h index db5033a..8b6efb3 100644 --- a/src/cocoatweet/oauth/authorize.h +++ b/src/cocoatweet/oauth/authorize.h @@ -5,18 +5,18 @@ #include #include -namespace CocoaTweet::Authentication{ -class Auth: public groupInterface { +namespace CocoaTweet::Authentication { +class Auth : public groupInterface { public: enum AuthType { OAuth, Bearer }; - -Auth(); -Auth(Key _key); -authorize(const bool app_only); + + Auth(); + Auth(Key _key); + authorize(const bool app_only); private: -Key key_; -} + Key key_; } +} // namespace CocoaTweet::Authentication #endif \ No newline at end of file diff --git a/src/cocoatweet/util/util.cc b/src/cocoatweet/util/util.cc index 771e53d..5fe81c2 100644 --- a/src/cocoatweet/util/util.cc +++ b/src/cocoatweet/util/util.cc @@ -36,34 +36,34 @@ std::string join(const std::vector _vec, const std::string& _delim) return str; } +std::unordered_map parse(const std::string str, const char _delim, + const char _conn) { + int first = 0; + int last = str.find_first_of(_delim); + std::vector result; -std::unordered_map parse(const std::string str, const char _delim, const char _conn){ - int first = 0; - int last = str.find_first_of(_delim); - std::vector result; - - while (first < str.size()) { - result.push_back(str.substr(first, last - first)); - first = last + 1; - last = str.find_first_of(_delim, first); - if (last == std::string::npos) last = str.size(); - } + while (first < str.size()) { + result.push_back(str.substr(first, last - first)); + first = last + 1; + last = str.find_first_of(_delim, first); + if (last == std::string::npos) last = str.size(); + } - auto mp = std::unordered_map(); - for(auto elm: result){ - int pos = elm.find_first_of(_conn); + auto mp = std::unordered_map(); + for (auto elm : result) { + int pos = elm.find_first_of(_conn); - std::cout << pos << std::endl; - if(pos == std::string::npos){ - mp.insert_or_assign(elm, ""); - }else{ - auto key = elm.substr(0, pos); - auto val = elm.substr(pos + 1); - mp.insert_or_assign(key, val); - } - } + std::cout << pos << std::endl; + if (pos == std::string::npos) { + mp.insert_or_assign(elm, ""); + } else { + auto key = elm.substr(0, pos); + auto val = elm.substr(pos + 1); + mp.insert_or_assign(key, val); + } + } - return mp; + return mp; } } // namespace CocoaTweet::Util diff --git a/src/cocoatweet/util/util.h b/src/cocoatweet/util/util.h index 64d172f..5392b7d 100644 --- a/src/cocoatweet/util/util.h +++ b/src/cocoatweet/util/util.h @@ -9,7 +9,8 @@ namespace CocoaTweet::Util { std::string urlEncode(const std::string& _str); std::string join(const std::vector _vec, const std::string& _delim); -std::unordered_map parse(const std::string str, const char _delim, const char _conn); +std::unordered_map parse(const std::string str, const char _delim, + const char _conn); } // namespace CocoaTweet::Util #endif diff --git a/src/main.cc b/src/main.cc index fdc0563..f67905b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,35 +2,58 @@ #include #include +#include +#include +#include +#include +#include + auto main() -> int { // Generate Key object // auto consumerKey = "your consumer key"; // auto consumerSecret = "your consumer secret"; // auto accessToken = "your access token"; // auto accessTokenSecret = "your access token secret"; - // CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key(consumerKey, + // CocoaTweet::Authentication::Key key = CocoaTweet::Authentication::Key(consumerKey, // consumerSecret, // accessToken, // accessTokenSecret); - // also can generate Key object from JSON file - // CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json"); + try { + // also can generate Key object from JSON file + CocoaTweet::Authentication::Key key = + CocoaTweet::Authentication::Key::fromJsonFile("api_key.json"); + std::shared_ptr oauth = + std::make_shared(key); - // Generate API Entry object using Key object - // CocoaTweet::API::API api(key); + // Generate API Entry object using Key object + CocoaTweet::API::API api(key); + auto bearerToken = api.oauth2().token(); + key.bearerToken(bearerToken); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH2); + api.swapKey(key); - // if you want to access api using Bearer Token, call this for get BearerToken; - // NOTE: call this, always authenticate with Bearer Token, which is Read Only Token - // try { + auto user = api.user().show("milkcocoa9692"); + std::cout << user.id() << std::endl; + // api.directMessage().messageCreate( + // user.id(), + // "これはクソみたいなスパムDMです。\nCocoaTwitterLibraryで、user/" + // "showが叩けるようになったので、外部サービスでuser_idを調べずともscreen_" + // "nameで引っ張ってきてクソDMを投げられるようになりました👏\nその記念にAPIから発砲🔫←センス最" + // "悪\nhttps://github.com/koron0902/CocoaTweet"); + // if you want to access api using Bearer Token, call this for get BearerToken; + // NOTE: call this, always authenticate with Bearer Token, which is Read Only Token + // try { // api.generateBearerToken(); // Now, you can use a twitter api - // auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library"); + auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library!!"); + // std::cout << status.id() << std::endl; // api.favorite().create(status.id()); // api.favorite().destroy(status.id()); // api.status().destroy(status.id()); // auto timeline = api.status().userTimeline("milkcocoa0902"); - // } catch (CocoaTweet::Exception::Exception e) { - // std::cout << e.what() << std::endl; - // } + } catch (CocoaTweet::Exception::Exception e) { + std::cout << e.what() << std::endl; + } } diff --git a/test/oauth/key.cc b/test/oauth/key.cc index de2c8ba..e6dc8ad 100644 --- a/test/oauth/key.cc +++ b/test/oauth/key.cc @@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(test01) { BOOST_AUTO_TEST_CASE(test02) { CocoaTweet::Authentication::Key key("consumerKey", "consumerSecret", "accessToken", - "accessTokenSecret"); + "accessTokenSecret"); BOOST_TEST(key.consumerKey() == "consumerKey"); BOOST_TEST(key.consumerSecret() == "consumerSecret");