This commit is contained in:
keita
2022-04-21 10:03:33 +09:00
parent 9dc7f35365
commit d43e04506b
15 changed files with 127 additions and 6 deletions
+5
View File
@@ -3,6 +3,7 @@
namespace CocoaTweet::API { namespace CocoaTweet::API {
API::API(CocoaTweet::OAuth::Key _key) { API::API(CocoaTweet::OAuth::Key _key) {
oauth_ = std::make_shared<CocoaTweet::OAuth::OAuth1>(_key); oauth_ = std::make_shared<CocoaTweet::OAuth::OAuth1>(_key);
user_ = Users::User(oauth_);
status_ = Statuses::Status(oauth_); status_ = Statuses::Status(oauth_);
favorite_ = Favorites::Favorite(oauth_); favorite_ = Favorites::Favorite(oauth_);
media_ = Medias::Media(oauth_); media_ = Medias::Media(oauth_);
@@ -13,6 +14,10 @@ const std::string& API::generateBearerToken() const {
return oauth_->generateBearerToken(); return oauth_->generateBearerToken();
} }
Users::User API::user() const {
return user_;
}
Statuses::Status API::status() const { Statuses::Status API::status() const {
return status_; return status_;
} }
+4
View File
@@ -1,6 +1,7 @@
#ifndef COCOATWEET_API_API_H_ #ifndef COCOATWEET_API_API_H_
#define COCOATWEET_API_API_H_ #define COCOATWEET_API_API_H_
#include <cocoatweet/api/user/user.h>
#include <cocoatweet/api/status/status.h> #include <cocoatweet/api/status/status.h>
#include <cocoatweet/api/favorite/favorite.h> #include <cocoatweet/api/favorite/favorite.h>
#include <cocoatweet/api/media/media.h> #include <cocoatweet/api/media/media.h>
@@ -15,6 +16,8 @@ public:
/// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key /// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key
API(CocoaTweet::OAuth::Key _key); API(CocoaTweet::OAuth::Key _key);
Users::User user() const;
/// @brief Getter for Grouped by Statuses/* /// @brief Getter for Grouped by Statuses/*
/// @param[out] Status object typed CocoaTweet::API::Statuses::Status /// @param[out] Status object typed CocoaTweet::API::Statuses::Status
Statuses::Status status() const; Statuses::Status status() const;
@@ -29,6 +32,7 @@ public:
const std::string& generateBearerToken() const; const std::string& generateBearerToken() const;
private: private:
Users::User user_;
Statuses::Status status_; Statuses::Status status_;
Favorites::Favorite favorite_; Favorites::Favorite favorite_;
Medias::Media media_; Medias::Media media_;
+4 -2
View File
@@ -108,12 +108,14 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
#endif #endif
if ((responseCode / 100) == 4) { if ((responseCode / 100) == 4) {
auto j = nlohmann::json::parse(rcv); auto j = nlohmann::json::parse(rcv);
auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"];
if (j.count("error") != 0) { if (j.count("error") != 0) {
// この形式はエラーコードを持たないのでエラー種別が特定できない // この形式はエラーコードを持たないのでエラー種別が特定できない
throw new CocoaTweet::Exception::Exception(j["error"]); throw new CocoaTweet::Exception::Exception(j["error"]);
} }
auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"];
if (error.get<int>() == 144) { if (error.get<int>() == 144) {
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) { } else if (error.get<int>() == 32) {
+3 -2
View File
@@ -119,12 +119,13 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
#endif #endif
if ((responseCode / 100) == 4) { if ((responseCode / 100) == 4) {
auto j = nlohmann::json::parse(rcv); auto j = nlohmann::json::parse(rcv);
auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"];
if (j.count("error") != 0) { if (j.count("error") != 0) {
// この形式はエラーコードを持たないのでエラー種別が特定できない // この形式はエラーコードを持たないのでエラー種別が特定できない
throw new CocoaTweet::Exception::Exception(j["error"]); throw new CocoaTweet::Exception::Exception(j["error"]);
} }
auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"];
if (error.get<int>() == 144) { if (error.get<int>() == 144) {
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) { } else if (error.get<int>() == 32) {
-1
View File
@@ -14,7 +14,6 @@ Tweet Tweet::parse(const std::string& _json) {
tweet.user(CocoaTweet::API::Model::User(j["user"].dump())); tweet.user(CocoaTweet::API::Model::User(j["user"].dump()));
} }
return tweet; return tweet;
} }
+34
View File
@@ -0,0 +1,34 @@
#include "cocoatweet/api/user/show.h"
#include <cocoatweet/util/util.h>
#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<CocoaTweet::OAuth::OAuth1> _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
+36
View File
@@ -0,0 +1,36 @@
#ifndef COCOATWEET_API_USER_SHOW_H_
#define COCOATWEET_API_USER_SHOW_H_
#include <cocoatweet/api/interface/httpGet.h>
#include <cocoatweet/api/model/user.h>
#include <vector>
#include <utility>
#include <memory>
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<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::User : request result
CocoaTweet::API::Model::User process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
private:
std::string status_;
};
} // namespace CocoaTweet::API::Statuses
#endif
+14
View File
@@ -0,0 +1,14 @@
#include <cocoatweet/api/user/user.h>
#include <cocoatweet/api/user/show.h>
namespace CocoaTweet::API::Users{
User::User(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _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_);
}
}
+26
View File
@@ -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 <cocoatweet/api/model/user.h>
#include <vector>
#include <utility>
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<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object
User(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
CocoaTweet::API::Model::User show(const std::string& _screenName) const;
};
} // namespace CocoaTweet::API::Statuses
#endif
View File
View File
View File