From aceb3cc33ee78cce1077252aff8a8808a3195ff1 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 31 May 2016 09:55:58 -0400 Subject: Added ability to get configuration --- src/client.cpp | 16 ++++++++ src/client.h | 6 +++ src/configuration.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/configuration.h | 52 ++++++++++++++++++++++++ src/twitter.h | 1 + 5 files changed, 184 insertions(+) create mode 100644 src/configuration.cpp create mode 100644 src/configuration.h (limited to 'src') diff --git a/src/client.cpp b/src/client.cpp index b7d1624..cf2b5c4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -321,6 +321,22 @@ namespace twitter { return _current_user; } + configuration client::getConfiguration() + { + if (!_configuration || (difftime(time(NULL), _last_configuration_update) > 60*60*24)) + { + long response_code; + std::string response_data; + if (performGet("https://api.twitter.com/1.1/help/configuration.json", response_code, response_data)) + { + _configuration = configuration(response_data); + _last_configuration_update = time(NULL); + } + } + + return _configuration; + } + response client::getFriends(std::set& _ret) { if (!_current_user) diff --git a/src/client.h b/src/client.h index c1c6344..f96022d 100644 --- a/src/client.h +++ b/src/client.h @@ -11,6 +11,7 @@ #include #include #include +#include "configuration.h" namespace OAuth { class Consumer; @@ -81,6 +82,8 @@ namespace twitter { const user& getUser() const; + configuration getConfiguration(); + // NOTE: stream setting function calls will fail silently when stream is running void setUserStreamNotifyCallback(stream::notify_callback callback); void setUserStreamReceiveAllReplies(bool _arg); @@ -99,6 +102,9 @@ namespace twitter { user _current_user; stream _user_stream{*this}; + configuration _configuration; + time_t _last_configuration_update; + bool performGet(std::string url, long& response_code, std::string& result); bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result); bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result); diff --git a/src/configuration.cpp b/src/configuration.cpp new file mode 100644 index 0000000..63464ed --- /dev/null +++ b/src/configuration.cpp @@ -0,0 +1,109 @@ +#include "configuration.h" +#include +#include + +using nlohmann::json; + +namespace twitter { + + configuration::configuration() + { + _valid = false; + } + + configuration::configuration(std::string data) + { + _valid = true; + + auto _data = json::parse(data); + + _characters_reserved_per_media = _data.at("characters_reserved_per_media"); + _dm_text_character_limit = _data.at("dm_text_character_limit"); + _max_media_per_upload = _data.at("max_media_per_upload"); + _photo_size_limit = _data.at("photo_size_limit"); + _short_url_length = _data.at("short_url_length"); + _short_https_url_length = _data.at("short_url_length_https"); + + for (json::iterator sizedata = _data.at("photo_sizes").begin(); sizedata != _data.at("photo_sizes").end(); ++sizedata) + { + photosize size; + size.height = sizedata.value().at("h"); + size.width = sizedata.value().at("w"); + if (sizedata.value().at("resize") == "fit") + { + size.resize = resizetype::fit; + } else { + size.resize = resizetype::crop; + } + + _photo_sizes[sizedata.key()] = size; + } + + for (auto path : _data.at("non_username_paths")) + { + _non_username_paths.insert(path.get()); + } + } + + size_t configuration::getCharactersReservedPerMedia() const + { + assert(_valid); + + return _characters_reserved_per_media; + } + + size_t configuration::getDirectMessageCharacterLimit() const + { + assert(_valid); + + return _dm_text_character_limit; + } + + size_t configuration::getMaxMediaPerUpload() const + { + assert(_valid); + + return _max_media_per_upload; + } + + size_t configuration::getPhotoSizeLimit() const + { + assert(_valid); + + return _photo_size_limit; + } + + std::map configuration::getPhotoSizes() const + { + assert(_valid); + + return _photo_sizes; + } + + size_t configuration::getShortUrlLength() const + { + assert(_valid); + + return _short_url_length; + } + + size_t configuration::getShortHttpsUrlLength() const + { + assert(_valid); + + return _short_https_url_length; + } + + std::set configuration::getNonUsernamePaths() const + { + assert(_valid); + + return _non_username_paths; + } + + configuration::operator bool() const + { + return _valid; + } + +}; diff --git a/src/configuration.h b/src/configuration.h new file mode 100644 index 0000000..a15be3d --- /dev/null +++ b/src/configuration.h @@ -0,0 +1,52 @@ +#ifndef CONFIGURATION_H_A7164D18 +#define CONFIGURATION_H_A7164D18 + +#include +#include +#include + +namespace twitter { + + class configuration { + public: + enum class resizetype { + fit, + crop + }; + + struct photosize { + size_t height; + size_t width; + resizetype resize; + }; + + configuration(); + configuration(std::string data); + + size_t getCharactersReservedPerMedia() const; + size_t getDirectMessageCharacterLimit() const; + size_t getMaxMediaPerUpload() const; + size_t getPhotoSizeLimit() const; + std::map getPhotoSizes() const; + size_t getShortUrlLength() const; + size_t getShortHttpsUrlLength() const; + std::set getNonUsernamePaths() const; + + operator bool() const; + + private: + bool _valid = false; + + size_t _characters_reserved_per_media; + size_t _dm_text_character_limit; + size_t _max_media_per_upload; + size_t _photo_size_limit; + std::map _photo_sizes; + size_t _short_url_length; + size_t _short_https_url_length; + std::set _non_username_paths; + }; + +}; + +#endif /* end of include guard: CONFIGURATION_H_A7164D18 */ diff --git a/src/twitter.h b/src/twitter.h index d0b469e..90925df 100644 --- a/src/twitter.h +++ b/src/twitter.h @@ -16,5 +16,6 @@ namespace twitter { #include "notification.h" #include "list.h" #include "direct_message.h" +#include "configuration.h" #endif /* end of include guard: TWITTER_H_AC7A7666 */ -- cgit 1.4.1