code format

This commit is contained in:
keita
2022-04-23 18:04:03 +09:00
parent 136700d154
commit 611cfc2e31
52 changed files with 391 additions and 350 deletions
-1
View File
@@ -8,7 +8,6 @@
namespace CocoaTweet::API { namespace CocoaTweet::API {
API::API(CocoaTweet::Authentication::Key _key) { API::API(CocoaTweet::Authentication::Key _key) {
swapKey(_key); swapKey(_key);
} }
void API::swapKey(const CocoaTweet::Authentication::Key _key) { void API::swapKey(const CocoaTweet::Authentication::Key _key) {
@@ -2,7 +2,8 @@
#include <cocoatweet/api/directMessage/new.h> #include <cocoatweet/api/directMessage/new.h>
namespace CocoaTweet::API::DirectMessages { namespace CocoaTweet::API::DirectMessages {
DirectMessage::DirectMessage(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) { DirectMessage::DirectMessage(
std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
@@ -15,7 +15,8 @@ public:
DirectMessage() = default; DirectMessage() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
DirectMessage(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); DirectMessage(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
void messageCreate(const std::string& _recipient, const std::string& _message); void messageCreate(const std::string& _recipient, const std::string& _message);
+2 -1
View File
@@ -17,7 +17,8 @@ public:
void message(const std::string& _message); void message(const std::string& _message);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
+2 -1
View File
@@ -11,7 +11,8 @@ void Create::id(const std::string& _id) {
bodyParam_.insert_or_assign("id", _id); bodyParam_.insert_or_assign("id", _id);
} }
CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) { CocoaTweet::API::Model::Tweet Create::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet(_rcv); tweet = CocoaTweet::API::Model::Tweet(_rcv);
+2 -1
View File
@@ -9,7 +9,8 @@ class Create : public CocoaTweet::API::Interface::HttpPost {
public: public:
Create(); Create();
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
}; };
+2 -1
View File
@@ -9,7 +9,8 @@ class Destroy : public CocoaTweet::API::Interface::HttpPost {
public: public:
Destroy(); Destroy();
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
}; };
+4 -2
View File
@@ -131,9 +131,11 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBas
} else if (error.get<int>() == 186) { } else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
} else if (error.get<int>() == 170) { } else if (error.get<int>() == 170) {
throw CocoaTweet::Exception::MissingRequiredParamException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::MissingRequiredParamException(
message.get<std::string>().c_str());
} else if (error.get<int>() == 220) { } else if (error.get<int>() == 220) {
throw CocoaTweet::Exception::CredentialNotAllowedException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::CredentialNotAllowedException(
message.get<std::string>().c_str());
} else { } else {
} }
} }
+2 -2
View File
@@ -11,8 +11,8 @@ class HttpGet : public virtual HttpBase {
public: public:
protected: protected:
/// @brief Send HTTP/POST using OAuth object /// @brief Send HTTP/POST using OAuth object
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object to /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// authenticate /// to OAuth object to authenticate
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback : /// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response /// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth, void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
+4 -3
View File
@@ -71,7 +71,6 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBa
// oauthHeader += CocoaTweet::Util::join(tmp, ","); // oauthHeader += CocoaTweet::Util::join(tmp, ",");
// } // }
auto oauthHeader = std::string(); auto oauthHeader = std::string();
if (contentType_ == "application/x-www-form-urlencoded") { if (contentType_ == "application/x-www-form-urlencoded") {
oauthHeader = oauth->calculateAuthHeader(bodyParam_, "POST", url_); oauthHeader = oauth->calculateAuthHeader(bodyParam_, "POST", url_);
@@ -149,9 +148,11 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBa
} else if (error.get<int>() == 186) { } else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
} else if (error.get<int>() == 170) { } else if (error.get<int>() == 170) {
throw CocoaTweet::Exception::MissingRequiredParamException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::MissingRequiredParamException(
message.get<std::string>().c_str());
} else if (error.get<int>() == 220) { } else if (error.get<int>() == 220) {
throw CocoaTweet::Exception::CredentialNotAllowedException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::CredentialNotAllowedException(
message.get<std::string>().c_str());
} }
} }
+2 -2
View File
@@ -11,8 +11,8 @@ class HttpPost : public HttpBase {
public: public:
protected: protected:
/// @brief Send HTTP/POST using OAuth object /// @brief Send HTTP/POST using OAuth object
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object to /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// authenticate /// to OAuth object to authenticate
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback : /// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response /// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth, void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
+2 -1
View File
@@ -16,7 +16,8 @@ public:
Media() = default; Media() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
Media(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); Media(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const; CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const;
+4 -3
View File
@@ -26,11 +26,12 @@ public:
void mediaId(const std::string& _mediaId); void mediaId(const std::string& _mediaId);
/// @brief upload media /// @brief upload media
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object for /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// authenticate /// to OAuth object for authenticate
/// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post /// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post
/// tweet. /// tweet.
CocoaTweet::API::Model::MediaStore process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::MediaStore process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Medias } // namespace CocoaTweet::API::Medias
+3 -2
View File
@@ -3,7 +3,8 @@
namespace CocoaTweet::API::Model { namespace CocoaTweet::API::Model {
OAuthToken::OAuthToken() : oauthToken_(""), oauthTokenSecret_("") {} OAuthToken::OAuthToken() : oauthToken_(""), oauthTokenSecret_("") {}
OAuthToken::OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret): oauthToken_(_oauthToken), oauthTokenSecret_(_oauthTokenSecret) {} OAuthToken::OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret)
: oauthToken_(_oauthToken), oauthTokenSecret_(_oauthTokenSecret) {}
const std::string& OAuthToken::oauthToken() const { const std::string& OAuthToken::oauthToken() const {
return oauthToken_; return oauthToken_;
} }
@@ -16,4 +17,4 @@ namespace CocoaTweet::API::Model{
void OAuthToken::oauthTokenSecret(const std::string& _oauthTokenSecret) { void OAuthToken::oauthTokenSecret(const std::string& _oauthTokenSecret) {
oauthTokenSecret_ = _oauthTokenSecret; oauthTokenSecret_ = _oauthTokenSecret;
} }
} } // namespace CocoaTweet::API::Model
+1 -1
View File
@@ -15,6 +15,6 @@ namespace CocoaTweet::API::Model{
void oauthToken(const std::string& _oauthToken); void oauthToken(const std::string& _oauthToken);
void oauthTokenSecret(const std::string& _oauthTokenSecret); void oauthTokenSecret(const std::string& _oauthTokenSecret);
}; };
} } // namespace CocoaTweet::API::Model
#endif #endif
+3 -2
View File
@@ -16,7 +16,8 @@ namespace CocoaTweet::API::OAuth1{
oauthToken_ = _token; oauthToken_ = _token;
} }
const CocoaTweet::API::Model::OAuthToken AccessToken::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth){ const CocoaTweet::API::Model::OAuthToken AccessToken::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::OAuthToken oauthToken; CocoaTweet::API::Model::OAuthToken oauthToken;
HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) {
auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); auto mp = CocoaTweet::Util::parse(_rcv, '&', '=');
@@ -30,4 +31,4 @@ namespace CocoaTweet::API::OAuth1{
}); });
return oauthToken; return oauthToken;
} }
} } // namespace CocoaTweet::API::OAuth1
+4 -2
View File
@@ -7,14 +7,16 @@ namespace CocoaTweet::API::OAuth1{
class AccessToken : public CocoaTweet::API::Interface::HttpPost { class AccessToken : public CocoaTweet::API::Interface::HttpPost {
private: private:
CocoaTweet::API::Model::OAuthToken oauthToken_; CocoaTweet::API::Model::OAuthToken oauthToken_;
public: public:
AccessToken(); AccessToken();
void oauthVerifier(const std::string& _verifier); void oauthVerifier(const std::string& _verifier);
void oauthToken(const CocoaTweet::API::Model::OAuthToken _token); void oauthToken(const CocoaTweet::API::Model::OAuthToken _token);
const CocoaTweet::API::Model::OAuthToken process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) ; const CocoaTweet::API::Model::OAuthToken process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} } // namespace CocoaTweet::API::OAuth1
#endif #endif
+5 -3
View File
@@ -21,13 +21,15 @@ namespace CocoaTweet::API::OAuth1{
bodyParam_.insert_or_assign("screen_name", _screenName); bodyParam_.insert_or_assign("screen_name", _screenName);
} }
const std::string Authorize::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> __unused__) { const std::string Authorize::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> __unused__) {
std::vector<std::string> tmp; std::vector<std::string> tmp;
std::string query = ""; std::string query = "";
for (const auto& [key, value] : bodyParam_) { for (const auto& [key, value] : bodyParam_) {
tmp.push_back(key + "=" + value); tmp.push_back(key + "=" + value);
query = CocoaTweet::Util::join(tmp, "&"); query = CocoaTweet::Util::join(tmp, "&");
} }
return url_ + "?" + query;; return url_ + "?" + query;
} ;
} }
} // namespace CocoaTweet::API::OAuth1
+3 -2
View File
@@ -13,8 +13,9 @@ public:
void screenName(const std::string& _screenName); void screenName(const std::string& _screenName);
const std::string process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> __unused__) ; const std::string process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> __unused__);
}; };
} } // namespace CocoaTweet::API::OAuth1
#endif #endif
+5 -3
View File
@@ -6,7 +6,8 @@ namespace CocoaTweet::API::OAuth1 {
oauth_ = _oauth; oauth_ = _oauth;
} }
CocoaTweet::API::Model::OAuthToken OAuth::requestToken(const std::string& _oauthCallback) const{ CocoaTweet::API::Model::OAuthToken OAuth::requestToken(
const std::string& _oauthCallback) const {
auto key = oauth_.lock()->key(); auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key); auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
@@ -22,7 +23,8 @@ namespace CocoaTweet::API::OAuth1 {
return authorize.process(oauth_); return authorize.process(oauth_);
} }
const CocoaTweet::API::Model::OAuthToken OAuth::accessToken(const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const{ const CocoaTweet::API::Model::OAuthToken OAuth::accessToken(
const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const {
auto key = oauth_.lock()->key(); auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
key.accessToken(_oauthToken.oauthToken()); key.accessToken(_oauthToken.oauthToken());
@@ -32,4 +34,4 @@ namespace CocoaTweet::API::OAuth1 {
accessToken.oauthVerifier(_verifier); accessToken.oauthVerifier(_verifier);
return accessToken.process(oauth); return accessToken.process(oauth);
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::OAuth1
+5 -4
View File
@@ -17,14 +17,15 @@ public:
OAuth() = default; OAuth() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
OAuth(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); OAuth(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::OAuthToken requestToken(const std::string& _oauthCallback) const; CocoaTweet::API::Model::OAuthToken requestToken(const std::string& _oauthCallback) const;
const std::string authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) 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 #endif
+3 -2
View File
@@ -12,7 +12,8 @@ namespace CocoaTweet::API::OAuth1{
bodyParam_.insert_or_assign("oauth_callback", _oauthCallback); bodyParam_.insert_or_assign("oauth_callback", _oauthCallback);
} }
CocoaTweet::API::Model::OAuthToken RequestToken::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth){ CocoaTweet::API::Model::OAuthToken RequestToken::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::OAuthToken oauthToken; CocoaTweet::API::Model::OAuthToken oauthToken;
HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) {
auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); auto mp = CocoaTweet::Util::parse(_rcv, '&', '=');
@@ -26,4 +27,4 @@ namespace CocoaTweet::API::OAuth1{
}); });
return oauthToken; return oauthToken;
} }
} } // namespace CocoaTweet::API::OAuth1
+3 -2
View File
@@ -10,8 +10,9 @@ namespace CocoaTweet::API::OAuth1{
RequestToken(); RequestToken();
void oauthCallback(const std::string& _oauthCallback); void oauthCallback(const std::string& _oauthCallback);
CocoaTweet::API::Model::OAuthToken process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::OAuthToken process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} } // namespace CocoaTweet::API::OAuth1
#endif #endif
+1 -1
View File
@@ -13,4 +13,4 @@ namespace CocoaTweet::API::OAuth2 {
CocoaTweet::API::OAuth2::Token token; CocoaTweet::API::OAuth2::Token token;
return token.process(oauth); return token.process(oauth);
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::OAuth2
+3 -3
View File
@@ -15,12 +15,12 @@ public:
OAuth2() = default; OAuth2() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
const std::string token() const; const std::string token() const;
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::OAuth2
#endif #endif
+3 -5
View File
@@ -1,7 +1,6 @@
#include <cocoatweet/api/oauth2/token.h> #include <cocoatweet/api/oauth2/token.h>
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
namespace CocoaTweet::API::OAuth2 { namespace CocoaTweet::API::OAuth2 {
Token::Token() { Token::Token() {
contentType_ = "application/x-www-form-urlencoded"; contentType_ = "application/x-www-form-urlencoded";
@@ -10,9 +9,8 @@ namespace CocoaTweet::API::OAuth2{
bodyParam_.insert_or_assign("grant_type", "client_credentials"); bodyParam_.insert_or_assign("grant_type", "client_credentials");
} }
const std::string Token::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
const std::string Token::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto basic = std::make_shared<CocoaTweet::Authentication::Basic>(_oauth.lock()->key()); auto basic = std::make_shared<CocoaTweet::Authentication::Basic>(_oauth.lock()->key());
std::string bearer = ""; std::string bearer = "";
HttpPost::process(basic, [&bearer](const std::string& _rcv) { HttpPost::process(basic, [&bearer](const std::string& _rcv) {
@@ -22,4 +20,4 @@ const std::string Token::process(std::weak_ptr<CocoaTweet::Authentication::Authe
return bearer; return bearer;
} }
} } // namespace CocoaTweet::API::OAuth2
+3 -2
View File
@@ -8,8 +8,9 @@ namespace CocoaTweet::API::OAuth2{
class Token : public CocoaTweet::API::Interface::HttpPost { class Token : public CocoaTweet::API::Interface::HttpPost {
public: public:
Token(); Token();
const std::string process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); const std::string process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} } // namespace CocoaTweet::API::OAuth2
#endif #endif
+4 -2
View File
@@ -18,9 +18,11 @@ public:
void id(const std::string _id); void id(const std::string _id);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+2 -1
View File
@@ -11,7 +11,8 @@ public:
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+2 -1
View File
@@ -28,7 +28,8 @@ public:
Status() = default; Status() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
Status(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); Status(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
/// @brief send request to statuses/update with specified status /// @brief send request to statuses/update with specified status
+2 -1
View File
@@ -11,7 +11,8 @@ public:
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+2 -1
View File
@@ -53,7 +53,8 @@ void Update::failDMCommands(bool _fail) {
bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail)); bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail));
} }
CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) { CocoaTweet::API::Model::Tweet Update::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); tweet = CocoaTweet::API::Model::Tweet::parse(_rcv);
+4 -2
View File
@@ -40,9 +40,11 @@ public:
void failDMCommands(bool _fail); void failDMCommands(bool _fail);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); CocoaTweet::API::Model::Tweet process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
std::string status_; std::string status_;
+2 -1
View File
@@ -19,7 +19,8 @@ public:
void screenName(const std::string& _screenName); void screenName(const std::string& _screenName);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// to oauth object
/// @param[out] std::vector<CocoaTweet::API::Model::Tweet> : request result /// @param[out] std::vector<CocoaTweet::API::Model::Tweet> : request result
std::vector<CocoaTweet::API::Model::Tweet> process( std::vector<CocoaTweet::API::Model::Tweet> process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
+1 -1
View File
@@ -31,4 +31,4 @@ CocoaTweet::API::Model::User Show::process(
return user; return user;
} }
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Users
+3 -2
View File
@@ -23,7 +23,8 @@ public:
void screenName(const std::string& _screenName); void screenName(const std::string& _screenName);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer
/// to oauth object
/// @param[out] CocoaTweet::API::Model::User : request result /// @param[out] CocoaTweet::API::Model::User : request result
CocoaTweet::API::Model::User process( CocoaTweet::API::Model::User process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
@@ -31,6 +32,6 @@ public:
private: private:
std::string status_; std::string status_;
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Users
#endif #endif
+1 -1
View File
@@ -11,4 +11,4 @@ namespace CocoaTweet::API::Users{
show.screenName(_screenName); show.screenName(_screenName);
return show.process(oauth_); return show.process(oauth_);
} }
} } // namespace CocoaTweet::API::Users
+3 -3
View File
@@ -15,12 +15,12 @@ public:
User() = default; User() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to
/// OAuth object
User(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth); User(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::User show(const std::string& _screenName) const; CocoaTweet::API::Model::User show(const std::string& _screenName) const;
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Users
#endif #endif
@@ -103,7 +103,6 @@ const std::string OAuth1::version() const {
return "1.0"; return "1.0";
} }
std::map<std::string, std::string> OAuth1::oauthParam() const { std::map<std::string, std::string> OAuth1::oauthParam() const {
auto tmp = std::map<std::string, std::string>{{"oauth_nonce", nonce()}, auto tmp = std::map<std::string, std::string>{{"oauth_nonce", nonce()},
{"oauth_signature_method", method()}, {"oauth_signature_method", method()},
@@ -28,7 +28,6 @@ public:
std::map<std::string, std::string> oauthParam() const; std::map<std::string, std::string> oauthParam() const;
std::string hmacSha1(std::string _key, std::string _data); std::string hmacSha1(std::string _key, std::string _data);
const std::string base64(const std::string& _raw); const std::string base64(const std::string& _raw);
}; };
} // namespace CocoaTweet::Authentication } // namespace CocoaTweet::Authentication
@@ -5,16 +5,11 @@
namespace CocoaTweet::Authentication { namespace CocoaTweet::Authentication {
class AuthenticatorBase { class AuthenticatorBase {
public: public:
enum class AuthenticationMethod{ enum class AuthenticationMethod { OAUTH10A, OAUTH2, BASIC, PLAIN, NONE };
OAUTH10A,
OAUTH2,
BASIC,
PLAIN,
NONE
};
virtual const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, virtual const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url) = 0; const std::string& _method,
const std::string& _url) = 0;
const Key key() const { const Key key() const {
return key_; return key_;
@@ -29,6 +24,6 @@ protected:
return realsize; return realsize;
} }
}; };
} } // namespace CocoaTweet::Authentication
#endif #endif
-3
View File
@@ -34,13 +34,11 @@ Basic::Basic(const Key _key){
const std::string Basic::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string Basic::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _method,
const std::string& _url) { const std::string& _url) {
auto signature = key_.consumerKey() + ":" + key_.consumerSecret(); auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
auto k64Signature = base64(signature); auto k64Signature = base64(signature);
auto authHeader = std::string("Authorization: Basic ") + k64Signature; auto authHeader = std::string("Authorization: Basic ") + k64Signature;
return authHeader; return authHeader;
} }
const std::string Basic::base64(const std::string& _raw) { const std::string Basic::base64(const std::string& _raw) {
@@ -73,5 +71,4 @@ const std::string Basic::base64(const std::string& _raw) {
return base64; return base64;
} }
} // namespace CocoaTweet::Authentication } // namespace CocoaTweet::Authentication
-1
View File
@@ -18,7 +18,6 @@ public:
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url); const std::string& _method, const std::string& _url);
const std::string base64(const std::string& _raw); const std::string base64(const std::string& _raw);
}; };
} // namespace CocoaTweet::Authentication } // namespace CocoaTweet::Authentication
-2
View File
@@ -34,11 +34,9 @@ Bearer::Bearer(const Key _key){
const std::string Bearer::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string Bearer::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _method,
const std::string& _url) { const std::string& _url) {
auto authHeader = std::string("Authorization: Bearer ") + key_.bearerToken(); auto authHeader = std::string("Authorization: Bearer ") + key_.bearerToken();
return authHeader; return authHeader;
} }
} // namespace CocoaTweet::Authentication } // namespace CocoaTweet::Authentication
-1
View File
@@ -13,7 +13,6 @@ public:
Bearer(); Bearer();
Bearer(const Key _key); Bearer(const Key _key);
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url); const std::string& _method, const std::string& _url);
}; };
+8 -7
View File
@@ -7,10 +7,7 @@
namespace CocoaTweet::Authentication { namespace CocoaTweet::Authentication {
class Key { class Key {
public: public:
enum AUTH_TYPE{ enum AUTH_TYPE { OAUTH10A, OAUTH2 };
OAUTH10A,
OAUTH2
};
private: private:
std::string consumerKey_; std::string consumerKey_;
@@ -19,15 +16,19 @@ private:
std::string accessTokenSecret_; std::string accessTokenSecret_;
std::string bearerToken_; std::string bearerToken_;
AUTH_TYPE authType_; AUTH_TYPE authType_;
public: public:
Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {} Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret, 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), : consumerKey_(_consumerKey),
consumerSecret_(_consumerSecret), consumerSecret_(_consumerSecret),
accessToken_(_accessToken), accessToken_(_accessToken),
accessTokenSecret_(_accessTokenSecret), authType_(_authType) {} accessTokenSecret_(_accessTokenSecret),
Key(const std::string& _consumerKey, const std::string& _consumerSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2) authType_(_authType) {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret,
const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2)
: consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType) {} : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType) {}
void consumerKey(const std::string& _consumerKey) { void consumerKey(const std::string& _consumerKey) {
+2 -3
View File
@@ -4,8 +4,7 @@
#include <cocoatweet/authentication/authenticator.h> #include <cocoatweet/authentication/authenticator.h>
namespace CocoaTweet::Authentication { namespace CocoaTweet::Authentication {
class Plain: public AuthenticatorBase { class Plain : public AuthenticatorBase {};
}; } // namespace CocoaTweet::Authentication
}
#endif #endif
+1 -1
View File
@@ -17,6 +17,6 @@ authorize(const bool app_only);
private: private:
Key key_; Key key_;
} }
} } // namespace CocoaTweet::Authentication
#endif #endif
+2 -2
View File
@@ -36,8 +36,8 @@ std::string join(const std::vector<std::string> _vec, const std::string& _delim)
return str; return str;
} }
std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim,
std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim, const char _conn){ const char _conn) {
int first = 0; int first = 0;
int last = str.find_first_of(_delim); int last = str.find_first_of(_delim);
std::vector<std::string> result; std::vector<std::string> result;
+2 -1
View File
@@ -9,7 +9,8 @@
namespace CocoaTweet::Util { namespace CocoaTweet::Util {
std::string urlEncode(const std::string& _str); std::string urlEncode(const std::string& _str);
std::string join(const std::vector<std::string> _vec, const std::string& _delim); std::string join(const std::vector<std::string> _vec, const std::string& _delim);
std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim, const char _conn); std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim,
const char _conn);
} // namespace CocoaTweet::Util } // namespace CocoaTweet::Util
#endif #endif
+30 -7
View File
@@ -2,35 +2,58 @@
#include <iostream> #include <iostream>
#include <cocoatweet/exception/exception.h> #include <cocoatweet/exception/exception.h>
#include <cocoatweet/api/oauth1/requestToken.h>
#include <cocoatweet/api/oauth1/authorize.h>
#include <cocoatweet/api/oauth1/accessToken.h>
#include <cocoatweet/api/oauth2/token.h>
#include <cocoatweet/authentication/authenticate.h>
auto main() -> int { auto main() -> int {
// Generate Key object // Generate Key object
// auto consumerKey = "your consumer key"; // auto consumerKey = "your consumer key";
// auto consumerSecret = "your consumer secret"; // auto consumerSecret = "your consumer secret";
// auto accessToken = "your access token"; // auto accessToken = "your access token";
// auto accessTokenSecret = "your access token secret"; // auto accessTokenSecret = "your access token secret";
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key(consumerKey, // CocoaTweet::Authentication::Key key = CocoaTweet::Authentication::Key(consumerKey,
// consumerSecret, // consumerSecret,
// accessToken, // accessToken,
// accessTokenSecret); // accessTokenSecret);
try {
// also can generate Key object from JSON file // also can generate Key object from JSON file
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json"); CocoaTweet::Authentication::Key key =
CocoaTweet::Authentication::Key::fromJsonFile("api_key.json");
std::shared_ptr<CocoaTweet::Authentication::OAuth1> oauth =
std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
// Generate API Entry object using Key object // Generate API Entry object using Key object
// CocoaTweet::API::API api(key); CocoaTweet::API::API api(key);
auto bearerToken = api.oauth2().token();
key.bearerToken(bearerToken);
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH2);
api.swapKey(key);
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; // 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 // NOTE: call this, always authenticate with Bearer Token, which is Read Only Token
// try { // try {
// api.generateBearerToken(); // api.generateBearerToken();
// Now, you can use a twitter api // 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().create(status.id());
// api.favorite().destroy(status.id()); // api.favorite().destroy(status.id());
// api.status().destroy(status.id()); // api.status().destroy(status.id());
// auto timeline = api.status().userTimeline("milkcocoa0902"); // auto timeline = api.status().userTimeline("milkcocoa0902");
// } catch (CocoaTweet::Exception::Exception e) { } catch (CocoaTweet::Exception::Exception e) {
// std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
// } }
} }