Merge pull request #2 from milkcocoa0902/develop
merge branch develop into master
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
FROM ubuntu:20.04
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
RUN apt-get update && apt-get install -y tzdata && apt-get clean
|
||||||
|
ENV TZ=Asia/Tokyo
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y clang \
|
||||||
|
cmake \
|
||||||
|
doxygen \
|
||||||
|
git \
|
||||||
|
graphviz \
|
||||||
|
libboost-dev \
|
||||||
|
libcurl4-openssl-dev \
|
||||||
|
libssl-dev \
|
||||||
|
ninja-build \
|
||||||
|
pkg-config && \
|
||||||
|
apt-get clean
|
||||||
Vendored
+58
@@ -0,0 +1,58 @@
|
|||||||
|
pipeline {
|
||||||
|
agent {
|
||||||
|
dockerfile true
|
||||||
|
}
|
||||||
|
|
||||||
|
stages{
|
||||||
|
stage("parallel execution"){
|
||||||
|
parallel{
|
||||||
|
stage("doxygen"){
|
||||||
|
steps{
|
||||||
|
sh 'doxygen'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage("validation"){
|
||||||
|
steps{
|
||||||
|
sh 'tools/validate/includeGuard.sh'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage("build and test"){
|
||||||
|
stages{
|
||||||
|
stage("prepare"){
|
||||||
|
steps{
|
||||||
|
sh '''
|
||||||
|
mkdir -p build
|
||||||
|
cd build
|
||||||
|
cmake .. -G Ninja
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage("build"){
|
||||||
|
steps{
|
||||||
|
sh '''
|
||||||
|
cd build
|
||||||
|
ninja
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage("test"){
|
||||||
|
steps{
|
||||||
|
echo "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage("upload artifact"){
|
||||||
|
steps{
|
||||||
|
archiveArtifacts allowEmptyArchive: true, artifacts: 'help/**/*.*', onlyIfSuccessful: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,9 +6,11 @@ This is a library for using Twitter API from C++
|
|||||||
- libssl
|
- libssl
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
|
you can use these endpoint
|
||||||
- statuses/update
|
- statuses/update
|
||||||
|
- statuses/destroy/:id
|
||||||
Now, only post a tweet.
|
- favorites/create
|
||||||
|
- favorites/destroy
|
||||||
|
|
||||||
# How
|
# How
|
||||||
## API Key Registration
|
## API Key Registration
|
||||||
@@ -56,8 +58,17 @@ CocoaTweet::API::API api(key);
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Post Tweet
|
## Use API
|
||||||
post tweet
|
|
||||||
```
|
```
|
||||||
|
// Post a tweet
|
||||||
api.status().Update("Hello, World!!\nTweet from Cocoa Twitter Library");
|
api.status().Update("Hello, World!!\nTweet from Cocoa Twitter Library");
|
||||||
|
|
||||||
|
// Delete a tweet
|
||||||
|
api.status().Destroy("tweet id");
|
||||||
|
|
||||||
|
// Fav. a tweet
|
||||||
|
api.favorite().Create("tweet id");
|
||||||
|
|
||||||
|
// un Fav. a tweet
|
||||||
|
api.favorite().Destroy("tweet id");
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
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);
|
||||||
status_ = Statuses::Status(oauth_);
|
status_ = Statuses::Status(oauth_);
|
||||||
|
favorite_ = Favorites::Favorite(oauth_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Statuses::Status API::status() const {
|
Statuses::Status API::status() const {
|
||||||
return status_;
|
return status_;
|
||||||
}
|
}
|
||||||
} // namespace CocoaTweet::API
|
|
||||||
|
Favorites::Favorite API::favorite() const {
|
||||||
|
return favorite_;
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API
|
||||||
|
|||||||
@@ -1,17 +1,29 @@
|
|||||||
#ifndef COCOATWEET_API_H_
|
#ifndef COCOATWEET_API_API_H_
|
||||||
#define COCOATWEET_API_H_
|
#define COCOATWEET_API_API_H_
|
||||||
|
|
||||||
#include "cocoatweet/api/status/status.h"
|
#include <cocoatweet/api/status/status.h>
|
||||||
#include "cocoatweet/oauth/oauth.h"
|
#include <cocoatweet/api/favorite/favorite.h>
|
||||||
|
#include <cocoatweet/oauth/oauth.h>
|
||||||
|
|
||||||
namespace CocoaTweet::API {
|
namespace CocoaTweet::API {
|
||||||
|
/// @brief Twitter API Entry Point
|
||||||
class API {
|
class API {
|
||||||
public:
|
public:
|
||||||
|
/// @brief primary constructor
|
||||||
|
/// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key
|
||||||
API(CocoaTweet::OAuth::Key _key);
|
API(CocoaTweet::OAuth::Key _key);
|
||||||
|
|
||||||
|
/// @brief Getter for Grouped by Statuses/*
|
||||||
|
/// @param[out] Status object typed CocoaTweet::API::Statuses::Status
|
||||||
Statuses::Status status() const;
|
Statuses::Status status() const;
|
||||||
|
|
||||||
|
/// @brief Getter for Grouped by Favorites/*
|
||||||
|
/// @param[out] Favorite object typed CococaTweet::API::Favorites::Favorite
|
||||||
|
Favorites::Favorite favorite() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Statuses::Status status_;
|
Statuses::Status status_;
|
||||||
|
Favorites::Favorite favorite_;
|
||||||
std::shared_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
std::shared_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
||||||
};
|
};
|
||||||
} // namespace CocoaTweet::API
|
} // namespace CocoaTweet::API
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <cocoatweet/api/favorite/create.h>
|
||||||
|
#include <iostream>
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
Create::Create() {
|
||||||
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
url_ = "https://api.twitter.com/1.1/favorites/create.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Create::id(const std::string& _id) {
|
||||||
|
bodyParam_.insert_or_assign("id", _id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Create::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
HttpPost::process(_oauth, [](const unsigned int _, const std::string& _srv) {
|
||||||
|
std::cout << _srv << std::endl;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef COCOATWEET_API_FAVORITE_CREATE_H_
|
||||||
|
#define COCOATWEET_API_FAVORITE_CREATE_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
class Create : public CocoaTweet::API::Interface::HttpPost {
|
||||||
|
public:
|
||||||
|
Create();
|
||||||
|
void id(const std::string& _id);
|
||||||
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <cocoatweet/api/favorite/destroy.h>
|
||||||
|
#include <iostream>
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
Destroy::Destroy() {
|
||||||
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
url_ = "https://api.twitter.com/1.1/favorites/destroy.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Destroy::id(const std::string& _id) {
|
||||||
|
bodyParam_.insert_or_assign("id", _id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Destroy::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
HttpPost::process(_oauth, [](const unsigned int _, const std::string& _srv) {
|
||||||
|
std::cout << _srv << std::endl;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef COCOATWEET_API_FAVORITE_DESTROY_H_
|
||||||
|
#define COCOATWEET_API_FAVORITE_DESTROY_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
class Destroy : public CocoaTweet::API::Interface::HttpPost {
|
||||||
|
public:
|
||||||
|
Destroy();
|
||||||
|
void id(const std::string& _id);
|
||||||
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "cocoatweet/api/favorite/favorite.h"
|
||||||
|
#include "cocoatweet/api/favorite/create.h"
|
||||||
|
#include "cocoatweet/api/favorite/destroy.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
Favorite::Favorite(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
oauth_ = _oauth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Favorite::Create(const std::string& _id) const {
|
||||||
|
CocoaTweet::API::Favorites::Create create;
|
||||||
|
create.id(_id);
|
||||||
|
create.process(oauth_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Favorite::Destroy(const std::string& _id) const {
|
||||||
|
CocoaTweet::API::Favorites::Destroy destroy;
|
||||||
|
destroy.id(_id);
|
||||||
|
destroy.process(oauth_);
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef COCOATWEET_API_FAVORITE_FAVORITE_H_
|
||||||
|
#define COCOATWEET_API_FAVORITE_FAVORITE_H_
|
||||||
|
|
||||||
|
#include "cocoatweet/api/interface/groupInterface.h"
|
||||||
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Favorites {
|
||||||
|
class Favorite : public groupInterface {
|
||||||
|
public:
|
||||||
|
Favorite() = default;
|
||||||
|
Favorite(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
void Create(const std::string& _id) const;
|
||||||
|
void Destroy(const std::string& _id) const;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Favorites
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef COCOATWEET_API_GROUPINTERFACE_H_
|
#ifndef COCOATWEET_API_INTERFACE_GROUPINTERFACE_H_
|
||||||
#define COCOATWEET_API_GROUPINTERFACE_H_
|
#define COCOATWEET_API_INTERFACE_GROUPINTERFACE_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "cocoatweet/oauth/oauth.h"
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|||||||
+15
-18
@@ -1,4 +1,4 @@
|
|||||||
#include <cocoatweet/api/interface/postInterface.h>
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
#include "cocoatweet/util/util.h"
|
#include "cocoatweet/util/util.h"
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -10,21 +10,22 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace CocoaTweet::API::Interface {
|
namespace CocoaTweet::API::Interface {
|
||||||
size_t postInterface::curlCallback_(char* _ptr, size_t _size, size_t _nmemb,
|
size_t HttpPost::curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) {
|
||||||
std::string* _stream) {
|
|
||||||
int realsize = _size * _nmemb;
|
int realsize = _size * _nmemb;
|
||||||
_stream->append(_ptr, realsize);
|
_stream->append(_ptr, realsize);
|
||||||
return realsize;
|
return realsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||||
std::function<void(std::string)> _callback) {
|
std::function<void(const unsigned int, const std::string&)> _callback) {
|
||||||
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
|
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
|
||||||
auto oauth = _oauth.lock();
|
auto oauth = _oauth.lock();
|
||||||
auto oauthParam = oauth->oauthParam();
|
auto oauthParam = oauth->oauthParam();
|
||||||
auto sigingParam = oauthParam;
|
auto sigingParam = oauthParam;
|
||||||
for (const auto [k, v] : bodyParam_) {
|
if (contentType_ == "application/x-www-form-urlencoded") {
|
||||||
sigingParam.insert_or_assign(k, v);
|
for (const auto [k, v] : bodyParam_) {
|
||||||
|
sigingParam.insert_or_assign(k, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto signature = oauth->signature(sigingParam, "POST", url_);
|
auto signature = oauth->signature(sigingParam, "POST", url_);
|
||||||
@@ -40,10 +41,7 @@ void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
for (const auto& [key, value] : bodyParam_) {
|
for (const auto& [key, value] : bodyParam_) {
|
||||||
tmp.push_back(key + "=" + value);
|
tmp.push_back(key + "=" + value);
|
||||||
}
|
}
|
||||||
std::stringstream os;
|
requestBody = CocoaTweet::Util::join(tmp, "&");
|
||||||
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
|
|
||||||
requestBody = os.str();
|
|
||||||
requestBody.erase(requestBody.size() - std::char_traits<char>::length("&"));
|
|
||||||
}
|
}
|
||||||
std::cout << "request Body -> " << requestBody << std::endl;
|
std::cout << "request Body -> " << requestBody << std::endl;
|
||||||
|
|
||||||
@@ -54,10 +52,7 @@ void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
for (const auto& [key, value] : oauthParam) {
|
for (const auto& [key, value] : oauthParam) {
|
||||||
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||||
}
|
}
|
||||||
std::stringstream os;
|
oauthHeader += CocoaTweet::Util::join(tmp, ",");
|
||||||
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, ","));
|
|
||||||
oauthHeader += os.str();
|
|
||||||
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
|
|
||||||
}
|
}
|
||||||
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
|
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
|
||||||
|
|
||||||
@@ -65,8 +60,9 @@ void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
CURL* curl;
|
CURL* curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
std::string rcv;
|
std::string rcv;
|
||||||
|
long responseCode;
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
url_ = url_; // + "?status=" + status_;
|
url_ = url_;
|
||||||
std::cout << "URL : " << url_ << std::endl;
|
std::cout << "URL : " << url_ << std::endl;
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
|
||||||
@@ -80,18 +76,19 @@ void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
struct curl_slist* headers = NULL;
|
struct curl_slist* headers = NULL;
|
||||||
// Authorizationをヘッダに追加
|
// Authorizationをヘッダに追加
|
||||||
headers = curl_slist_append(headers, oauthHeader.c_str());
|
headers = curl_slist_append(headers, oauthHeader.c_str());
|
||||||
// headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
|
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res != CURLE_OK) {
|
if (res != CURLE_OK) {
|
||||||
std::cout << "curl error : " << res << std::endl;
|
std::cout << "curl error : " << res << std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_callback) {
|
if (_callback) {
|
||||||
_callback(rcv);
|
_callback(responseCode, rcv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace CocoaTweet::API::Interface
|
} // namespace CocoaTweet::API::Interface
|
||||||
+6
-6
@@ -1,19 +1,19 @@
|
|||||||
#ifndef COCOATWEET_API_INTERFACE_POSTINTERFACE_H_
|
#ifndef COCOATWEET_API_INTERFACE_HTTPPOST_H_
|
||||||
#define COCOATWEET_API_INTERFACE_POSTINTERFACE_H_
|
#define COCOATWEET_API_INTERFACE_HTTPPOST_H_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "cocoatweet/oauth/oauth.h"
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
namespace CocoaTweet::API::Interface {
|
namespace CocoaTweet::API::Interface {
|
||||||
class postInterface {
|
class HttpPost {
|
||||||
public:
|
public:
|
||||||
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|
||||||
std::function<void(std::string)> _callback);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
||||||
std::map<std::string, std::string> bodyParam_;
|
std::map<std::string, std::string> bodyParam_;
|
||||||
std::string url_;
|
std::string url_;
|
||||||
|
std::string contentType_;
|
||||||
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||||
|
std::function<void(const unsigned int, const std::string&)> _callback);
|
||||||
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
|
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
|
||||||
};
|
};
|
||||||
} // namespace CocoaTweet::API::Interface
|
} // namespace CocoaTweet::API::Interface
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef COCOATWEET_API_MEDIA_UPLOAD_H_
|
||||||
|
#define COCOATWEET_API_MEDIA_UPLOAD_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/api/interface/postInterface.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Medias {
|
||||||
|
class Upload : public postInterface {
|
||||||
|
public:
|
||||||
|
enum Command : class st::string {
|
||||||
|
INIT = "INIT",
|
||||||
|
APPEND = "APPEND",
|
||||||
|
FINALIZE = "FINALIZE",
|
||||||
|
};
|
||||||
|
|
||||||
|
Upload::Upload();
|
||||||
|
void media(const std::string& _media);
|
||||||
|
void mediaId(const std::string _mediaId);
|
||||||
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, Command _command);
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Medias
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <cocoatweet/api/model/tweet.h>
|
||||||
|
#include <cocoatweet/exception/tweetNotFoundException.h>
|
||||||
|
#include <cocoatweet/exception/authenticateException.h>
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Model {
|
||||||
|
Tweet Tweet::parse(const unsigned int _responseCode, const std::string& _json) {
|
||||||
|
auto j = nlohmann::json::parse(_json);
|
||||||
|
Tweet tweet;
|
||||||
|
|
||||||
|
if (_responseCode == 200) {
|
||||||
|
tweet.id(j["id_str"]);
|
||||||
|
} else {
|
||||||
|
auto error = j["errors"][0]["code"];
|
||||||
|
auto message = j["errors"][0]["message"];
|
||||||
|
if (error.get<int>() == 144) {
|
||||||
|
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
|
||||||
|
}else if(error.get<int>() == 32){
|
||||||
|
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tweet;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tweet::id(const std::string _id) {
|
||||||
|
id_ = _id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string Tweet::id() const {
|
||||||
|
return id_;
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Model
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef COCOATWEET_API_MODEL_TWEET_H_
|
||||||
|
#define COCOATWEET_API_MODEL_TWEET_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Model {
|
||||||
|
class Tweet final {
|
||||||
|
public:
|
||||||
|
Tweet() = default;
|
||||||
|
Tweet(const Tweet&) = default;
|
||||||
|
Tweet(const unsigned int _responseCode, const std::string& _json)
|
||||||
|
: Tweet(Tweet::parse(_responseCode, _json)) {}
|
||||||
|
static Tweet parse(const unsigned int _responseCode, const std::string& _json);
|
||||||
|
void id(const std::string _id);
|
||||||
|
const std::string id() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string id_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Model
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include "cocoatweet/api/status/destroy.h"
|
||||||
|
#include <cocoatweet/api/model/tweet.h>
|
||||||
|
#include <iostream>
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
Destroy::Destroy() {}
|
||||||
|
void Destroy::id(const std::string _id) {
|
||||||
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
url_ = "https://api.twitter.com/1.1/statuses/destroy/" + _id + ".json";
|
||||||
|
}
|
||||||
|
|
||||||
|
CocoaTweet::API::Model::Tweet Destroy::process(
|
||||||
|
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
CocoaTweet::API::Model::Tweet tweet;
|
||||||
|
HttpPost::process(_oauth,
|
||||||
|
[&tweet](const unsigned int _responseCode, const std::string& _rsv) {
|
||||||
|
tweet = CocoaTweet::API::Model::Tweet::parse(_responseCode, _rsv);
|
||||||
|
});
|
||||||
|
return tweet;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef COCOATWEET_API_STATUS_DESTROY_H_
|
||||||
|
#define COCOATWEET_API_STATUS_DESTROY_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
|
#include <cocoatweet/api/model/tweet.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
/// @brief class for using status/destroy:id endpoint
|
||||||
|
class Destroy : public CocoaTweet::API::Interface::HttpPost {
|
||||||
|
public:
|
||||||
|
/// @brief primary constructor
|
||||||
|
Destroy();
|
||||||
|
|
||||||
|
/// @brief set tweet id to destroy
|
||||||
|
/// @param[in] std::string _id : tweet id which should be destroy
|
||||||
|
/// @param[out] none
|
||||||
|
void id(const std::string _id);
|
||||||
|
|
||||||
|
/// @brief process request for endpoint
|
||||||
|
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object
|
||||||
|
/// @param[out] none
|
||||||
|
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,15 +2,22 @@
|
|||||||
|
|
||||||
#include "cocoatweet/api/status/status.h"
|
#include "cocoatweet/api/status/status.h"
|
||||||
#include "cocoatweet/api/status/update.h"
|
#include "cocoatweet/api/status/update.h"
|
||||||
|
#include "cocoatweet/api/status/destroy.h"
|
||||||
|
|
||||||
namespace CocoaTweet::API::Statuses {
|
namespace CocoaTweet::API::Statuses {
|
||||||
Status::Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
Status::Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
oauth_ = _oauth;
|
oauth_ = _oauth;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Status::Update(const std::string& _status) const {
|
CocoaTweet::API::Model::Tweet Status::Update(const std::string& _status) const {
|
||||||
CocoaTweet::API::Statuses::Update update;
|
CocoaTweet::API::Statuses::Update update;
|
||||||
update.status(_status);
|
update.status(_status);
|
||||||
update.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; });
|
return update.process(oauth_);
|
||||||
|
}
|
||||||
|
|
||||||
|
CocoaTweet::API::Model::Tweet Status::Destroy(const std::string& _id) const {
|
||||||
|
CocoaTweet::API::Statuses::Destroy destroy;
|
||||||
|
destroy.id(_id);
|
||||||
|
return destroy.process(oauth_);
|
||||||
}
|
}
|
||||||
} // namespace CocoaTweet::API::Statuses
|
} // namespace CocoaTweet::API::Statuses
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
#ifndef COCOATWEET_API_STATUSED_H_
|
#ifndef COCOATWEET_API_STATUS_STATUS_H_
|
||||||
#define COCOATWEET_API_STATUSED_H_
|
#define COCOATWEET_API_STATUS_STATUS_H_
|
||||||
|
|
||||||
#include "cocoatweet/api/interface/groupInterface.h"
|
#include "cocoatweet/api/interface/groupInterface.h"
|
||||||
#include "cocoatweet/oauth/oauth.h"
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
#include <cocoatweet/api/model/tweet.h>
|
||||||
|
|
||||||
namespace CocoaTweet::API::Statuses {
|
namespace CocoaTweet::API::Statuses {
|
||||||
class Status : public groupInterface {
|
class Status : public groupInterface {
|
||||||
public:
|
public:
|
||||||
Status() = default;
|
Status() = default;
|
||||||
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
void Update(const std::string& _status) const;
|
CocoaTweet::API::Model::Tweet Update(const std::string& _status) const;
|
||||||
|
CocoaTweet::API::Model::Tweet Destroy(const std::string& _id) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,12 +1,24 @@
|
|||||||
#include "cocoatweet/api/status/update.h"
|
#include "cocoatweet/api/status/update.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace CocoaTweet::API::Statuses {
|
namespace CocoaTweet::API::Statuses {
|
||||||
Update::Update() {
|
Update::Update() {
|
||||||
url_ = "https://api.twitter.com/1.1/statuses/update.json";
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
url_ = "https://api.twitter.com/1.1/statuses/update.json";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update::status(const std::string _status) {
|
void Update::status(const std::string _status) {
|
||||||
status_ = _status;
|
status_ = _status;
|
||||||
bodyParam_.insert_or_assign("status", status_);
|
bodyParam_.insert_or_assign("status", status_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
CocoaTweet::API::Model::Tweet tweet;
|
||||||
|
HttpPost::process(_oauth,
|
||||||
|
[&tweet](const unsigned int _responseCode, const std::string& _rsv) {
|
||||||
|
tweet = CocoaTweet::API::Model::Tweet::parse(_responseCode, _rsv);
|
||||||
|
});
|
||||||
|
return tweet;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace CocoaTweet::API::Statuses
|
} // namespace CocoaTweet::API::Statuses
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
#ifndef COCOATWEET_API_STATUSES_UPDATE_H_
|
#ifndef COCOATWEET_API_STATUS_UPDATE_H_
|
||||||
#define COCOATWEET_API_STATUSES_UPDATE_H_
|
#define COCOATWEET_API_STATUS_UPDATE_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
#include "cocoatweet/api/interface/postInterface.h"
|
#include <cocoatweet/api/model/tweet.h>
|
||||||
//#include "cocoatweet/oauth/oauth.h"
|
|
||||||
|
|
||||||
namespace CocoaTweet::API::Statuses {
|
namespace CocoaTweet::API::Statuses {
|
||||||
class Update : public CocoaTweet::API::Interface::postInterface {
|
class Update : public CocoaTweet::API::Interface::HttpPost {
|
||||||
public:
|
public:
|
||||||
Update();
|
Update();
|
||||||
void status(const std::string _status);
|
void status(const std::string _status);
|
||||||
|
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string status_;
|
std::string status_;
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_AUTHENTICATEEXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_AUTHENTICATEEXCEPTION_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/exception/exception.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class AuthenticateException final : Exception {
|
||||||
|
using Exception::Exception;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_EXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_EXCEPTION_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class Exception : public std::exception {
|
||||||
|
public:
|
||||||
|
Exception(const char* _msg) : msg_(std::string(_msg)) {}
|
||||||
|
const std::string& what() {
|
||||||
|
return msg_;
|
||||||
|
}
|
||||||
|
virtual ~Exception() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string msg_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_RATELIMITEXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_RATELIMITEXCEPTION_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/exception/exception.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class RateLimitException final : Exception {
|
||||||
|
using Exception::Exception;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_TWEETDUPLICATEEXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_TWEETDUPLICATEEXCEPTION_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/exception/exception.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class TweetDuplicateException final : Exception {
|
||||||
|
using Exception::Exception;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_TWEETNOTFOUNDEXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_TWEETNOTFOUNDEXCEPTION_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/exception/exception.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class TweetNotFoundException final : Exception {
|
||||||
|
using Exception::Exception;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef COCOATWEET_EXCEPTION_TWEETTOOLONGEXCEPTION_H_
|
||||||
|
#define COCOATWEET_EXCEPTION_TWEETTOOLONGEXCEPTION_H_
|
||||||
|
|
||||||
|
#include <cocoatweet/exception/exception.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::Exception {
|
||||||
|
class TweetTooLongException final : Exception {
|
||||||
|
using Exception::Exception;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::Exception
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -28,10 +28,8 @@ std::map<std::string, std::string> OAuth1::signature(
|
|||||||
tmp.push_back(key + "=" + value);
|
tmp.push_back(key + "=" + value);
|
||||||
std::cout << (key + "=" + value) << std::endl;
|
std::cout << (key + "=" + value) << std::endl;
|
||||||
}
|
}
|
||||||
std::ostringstream os;
|
|
||||||
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
|
std::string query = CocoaTweet::Util::join(tmp, "&");
|
||||||
std::string query = os.str();
|
|
||||||
query.erase(query.size() - std::char_traits<char>::length("&"));
|
|
||||||
|
|
||||||
auto significateKey = key().consumerSecret() + "&" + key().accessTokenSecret();
|
auto significateKey = key().consumerSecret() + "&" + key().accessTokenSecret();
|
||||||
auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" +
|
auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" +
|
||||||
|
|||||||
@@ -19,6 +19,17 @@ std::string urlEncode(const std::string& _str) {
|
|||||||
return out.str();
|
return out.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
std::string join(const std::vector<std::string> _vec, const std::string& _delim) {
|
||||||
std::string join(const std::vector<T> _vec) {}
|
std::string str("");
|
||||||
|
|
||||||
|
for (auto v : _vec) {
|
||||||
|
str += (v + _delim);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!str.empty()) {
|
||||||
|
str.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
} // namespace CocoaTweet::Util
|
} // namespace CocoaTweet::Util
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
namespace CocoaTweet::Util {
|
namespace CocoaTweet::Util {
|
||||||
std::string urlEncode(const std::string& _str);
|
std::string urlEncode(const std::string& _str);
|
||||||
template <typename T>
|
std::string join(const std::vector<std::string> _vec, const std::string& _delim);
|
||||||
std::string join(const std::vector<T> _vec);
|
|
||||||
} // namespace CocoaTweet::Util
|
} // namespace CocoaTweet::Util
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+17
-13
@@ -2,20 +2,24 @@
|
|||||||
#include "cocoatweet/api/api.h"
|
#include "cocoatweet/api/api.h"
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
// キーオブジェクトを作成
|
// 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, consumerSecret,
|
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key(consumerKey,
|
||||||
// accessToken, accessTokenSecret);
|
// consumerSecret,
|
||||||
|
// accessToken,
|
||||||
|
// accessTokenSecret);
|
||||||
|
|
||||||
// jsonファイルから各種キーを読み込むことも可能
|
// also can generate Key object from JSON file
|
||||||
CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("apikey.json");
|
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json");
|
||||||
|
|
||||||
// 作成したキーオブジェクトを用いてAPIを立ち上げる.
|
// Generate API Entry object using Key object
|
||||||
// 内部的にはキーオブジェクトを使用してOAuth認証機を立ち上げている.
|
// CocoaTweet::API::API api(key);
|
||||||
CocoaTweet::API::API api(key);
|
|
||||||
|
|
||||||
api.status().Update("Hello Twitter World from Cocoa Twitter Library");
|
// Now, you can use a twitter api
|
||||||
|
// api.status().Update("Hello Twitter World from Cocoa Twitter Library");
|
||||||
|
// api.favorite().Create("tweet id you want to fav.");
|
||||||
|
// api.favorite().Destroy("tweet id you want to un_fav.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user