diff --git a/src/cocoatweet/api/api.cc b/src/cocoatweet/api/api.cc index 04ca82d..fedaabf 100644 --- a/src/cocoatweet/api/api.cc +++ b/src/cocoatweet/api/api.cc @@ -3,6 +3,7 @@ namespace CocoaTweet::API { API::API(CocoaTweet::OAuth::Key _key) { oauth_ = std::make_shared(_key); + user_ = Users::User(oauth_); status_ = Statuses::Status(oauth_); favorite_ = Favorites::Favorite(oauth_); media_ = Medias::Media(oauth_); @@ -13,6 +14,10 @@ const std::string& API::generateBearerToken() const { return oauth_->generateBearerToken(); } +Users::User API::user() const { + return user_; +} + Statuses::Status API::status() const { return status_; } diff --git a/src/cocoatweet/api/api.h b/src/cocoatweet/api/api.h index 62ab2ff..4a41b4f 100644 --- a/src/cocoatweet/api/api.h +++ b/src/cocoatweet/api/api.h @@ -1,6 +1,7 @@ #ifndef COCOATWEET_API_API_H_ #define COCOATWEET_API_API_H_ +#include #include #include #include @@ -15,6 +16,8 @@ public: /// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key API(CocoaTweet::OAuth::Key _key); + Users::User user() const; + /// @brief Getter for Grouped by Statuses/* /// @param[out] Status object typed CocoaTweet::API::Statuses::Status Statuses::Status status() const; @@ -29,6 +32,7 @@ public: const std::string& generateBearerToken() const; private: + Users::User user_; Statuses::Status status_; Favorites::Favorite favorite_; Medias::Media media_; diff --git a/src/cocoatweet/api/interface/httpGet.cc b/src/cocoatweet/api/interface/httpGet.cc index 9699e2c..af5b43e 100644 --- a/src/cocoatweet/api/interface/httpGet.cc +++ b/src/cocoatweet/api/interface/httpGet.cc @@ -108,12 +108,14 @@ void HttpGet::process(std::weak_ptr _oauth, #endif if ((responseCode / 100) == 4) { auto j = nlohmann::json::parse(rcv); - auto error = j["errors"][0]["code"]; - auto message = j["errors"][0]["message"]; if (j.count("error") != 0) { // この形式はエラーコードを持たないのでエラー種別が特定できない throw new CocoaTweet::Exception::Exception(j["error"]); } + + auto error = j["errors"][0]["code"]; + auto message = j["errors"][0]["message"]; + if (error.get() == 144) { throw CocoaTweet::Exception::TweetNotFoundException(message.get().c_str()); } else if (error.get() == 32) { diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index e318245..84f20dc 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -119,12 +119,13 @@ void HttpPost::process(std::weak_ptr _oauth, #endif if ((responseCode / 100) == 4) { auto j = nlohmann::json::parse(rcv); - auto error = j["errors"][0]["code"]; - auto message = j["errors"][0]["message"]; if (j.count("error") != 0) { // この形式はエラーコードを持たないのでエラー種別が特定できない throw new CocoaTweet::Exception::Exception(j["error"]); } + + auto error = j["errors"][0]["code"]; + auto message = j["errors"][0]["message"]; if (error.get() == 144) { throw CocoaTweet::Exception::TweetNotFoundException(message.get().c_str()); } else if (error.get() == 32) { diff --git a/src/cocoatweet/api/model/tweet.cc b/src/cocoatweet/api/model/tweet.cc index 22f971e..ab978ba 100644 --- a/src/cocoatweet/api/model/tweet.cc +++ b/src/cocoatweet/api/model/tweet.cc @@ -10,11 +10,10 @@ Tweet Tweet::parse(const std::string& _json) { tweet.createdAt(j["created_at"]); tweet.text(j["text"]); tweet.source(j["source"]); - if(j.contains("user")){ + if (j.contains("user")) { tweet.user(CocoaTweet::API::Model::User(j["user"].dump())); } - return tweet; } diff --git a/src/cocoatweet/api/user/show.cc b/src/cocoatweet/api/user/show.cc new file mode 100644 index 0000000..eb53776 --- /dev/null +++ b/src/cocoatweet/api/user/show.cc @@ -0,0 +1,34 @@ +#include "cocoatweet/api/user/show.h" +#include +#include "nlohmann/json.hpp" + +namespace CocoaTweet::API::Users { +Show::Show() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/1.1/users/show.json"; +} + +void Show::screenName(const std::string& _screenName) { + 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){ + bodyParam_.erase("screen_name"); + } + bodyParam_.insert_or_assign("id", _id); +} + +CocoaTweet::API::Model::User Show::process( + std::weak_ptr _oauth) { + CocoaTweet::API::Model::User user; + HttpGet::process(_oauth, [&user](const std::string& _rcv) { + user = CocoaTweet::API::Model::User::parse(_rcv); + }); + return user; +} + +} // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/user/show.h b/src/cocoatweet/api/user/show.h new file mode 100644 index 0000000..f8848ee --- /dev/null +++ b/src/cocoatweet/api/user/show.h @@ -0,0 +1,36 @@ +#ifndef COCOATWEET_API_USER_SHOW_H_ +#define COCOATWEET_API_USER_SHOW_H_ + +#include +#include +#include +#include +#include + +namespace CocoaTweet::API::Users { +/// @brief class for using users/show endpoint +class Show : public CocoaTweet::API::Interface::HttpGet { +public: + /// @brief primary constructor + Show(); + + /// @brief set screen name to get user information + /// @param[in] const std::string& _id : screen name for getting information + void id(const std::string& _id); + + /// @brief set screen name to get user information + /// @param[in] const std::string& _screenName : screen name for getting information + void screenName(const std::string& _screenName); + + /// @brief process request for endpoint + /// @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); + +private: + std::string status_; +}; +} // namespace CocoaTweet::API::Statuses + +#endif diff --git a/src/cocoatweet/api/user/user.cc b/src/cocoatweet/api/user/user.cc new file mode 100644 index 0000000..298b3b1 --- /dev/null +++ b/src/cocoatweet/api/user/user.cc @@ -0,0 +1,14 @@ +#include +#include + +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 diff --git a/src/cocoatweet/api/user/user.h b/src/cocoatweet/api/user/user.h new file mode 100644 index 0000000..f6bfea6 --- /dev/null +++ b/src/cocoatweet/api/user/user.h @@ -0,0 +1,26 @@ +#ifndef COCOATWEET_API_USER_USER_H_ +#define COCOATWEET_API_USER_USER_H_ + +#include "cocoatweet/api/interface/groupInterface.h" +#include "cocoatweet/oauth/oauth.h" +#include +#include +#include + +namespace CocoaTweet::API::Users { +/// @brief class for using users/show endpoint +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 + User(std::shared_ptr _oauth); + + CocoaTweet::API::Model::User show(const std::string& _screenName) const; + +}; +} // namespace CocoaTweet::API::Statuses + +#endif diff --git a/src/cocoatweet/oauth/authorize.cc b/src/cocoatweet/oauth/authorize.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/authorize.h b/src/cocoatweet/oauth/authorize.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/oauth2/invalidateToken.cc b/src/cocoatweet/oauth/oauth2/invalidateToken.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/oauth2/invalidateToken.h b/src/cocoatweet/oauth/oauth2/invalidateToken.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/oauth2/token.cc b/src/cocoatweet/oauth/oauth2/token.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/oauth2/token.h b/src/cocoatweet/oauth/oauth2/token.h new file mode 100644 index 0000000..e69de29