about summary refs log tree commit diff stats
path: root/src/client.h
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-08-20 13:56:23 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-08-20 13:56:23 -0400
commit69fc8d805396b889b5e8c1c88e8129d93db77d29 (patch)
tree6b807bd9332c65b65066e247d4d00fd5e4118d2e /src/client.h
parent442f1ee071152be04c4184473ddfee5040795b76 (diff)
downloadlibtwittercpp-69fc8d805396b889b5e8c1c88e8129d93db77d29.tar.gz
libtwittercpp-69fc8d805396b889b5e8c1c88e8129d93db77d29.tar.bz2
libtwittercpp-69fc8d805396b889b5e8c1c88e8129d93db77d29.zip
Updated API to use exceptions and make tweet/user objects more helpful
Diffstat (limited to 'src/client.h')
-rw-r--r--src/client.h129
1 files changed, 35 insertions, 94 deletions
diff --git a/src/client.h b/src/client.h index 6963412..37081ff 100644 --- a/src/client.h +++ b/src/client.h
@@ -1,17 +1,15 @@
1#ifndef TWITTER_H_ABFF6A12 1#ifndef TWITTER_H_ABFF6A12
2#define TWITTER_H_ABFF6A12 2#define TWITTER_H_ABFF6A12
3 3
4#include "codes.h"
5#include "tweet.h"
6#include "auth.h"
7#include <list> 4#include <list>
8#include <thread>
9#include <mutex>
10#include "notification.h"
11#include <set> 5#include <set>
12#include <ctime> 6#include <ctime>
13#include <chrono> 7#include <memory>
8#include "codes.h"
9#include "tweet.h"
10#include "auth.h"
14#include "configuration.h" 11#include "configuration.h"
12#include "util.h"
15 13
16namespace OAuth { 14namespace OAuth {
17 class Consumer; 15 class Consumer;
@@ -19,96 +17,39 @@ namespace OAuth {
19 class Client; 17 class Client;
20}; 18};
21 19
22class curl_httppost;
23
24namespace twitter { 20namespace twitter {
25 21
26 class client { 22 class client {
27 public: 23 public:
28 class stream { 24
29 public: 25 client(const auth& _auth);
30 typedef std::function<void(notification _notification)> notify_callback; 26 ~client();
31 27
32 stream(client& _client); 28 tweet updateStatus(std::string msg, std::list<long> media_ids = {}) const;
33 29 long uploadMedia(std::string media_type, const char* data, long data_length) const;
34 void setNotifyCallback(notify_callback _n); 30
35 void setReceiveAllReplies(bool _arg); 31 tweet replyToTweet(std::string msg, tweet_id in_response_to, std::list<long> media_ids = {}) const;
36 32 std::set<user_id> getFriends(user_id id) const;
37 bool isRunning() const; 33 std::set<user_id> getFollowers(user_id id) const;
38 void start(); 34 void follow(user_id toFollow) const;
39 void stop(); 35 void unfollow(user_id toUnfollow) const;
40 36
41 int progress(); 37 const user& getUser() const;
42 size_t write(char* ptr, size_t size, size_t nmemb); 38
43 39 const configuration& getConfiguration() const;
44 private: 40
45 enum class backoff { 41 private:
46 none, 42
47 network, 43 friend class stream;
48 http, 44
49 rate_limit 45 std::unique_ptr<OAuth::Consumer> _oauth_consumer;
50 }; 46 std::unique_ptr<OAuth::Token> _oauth_token;
51 47 std::unique_ptr<OAuth::Client> _oauth_client;
52 void run(); 48
53 49 std::unique_ptr<user> _current_user;
54 client& _client; 50
55 notify_callback _notify; 51 mutable std::unique_ptr<configuration> _configuration;
56 bool _stop = false; 52 mutable time_t _last_configuration_update;
57 std::thread _thread;
58 std::mutex _running_mutex;
59 std::mutex _stall_mutex;
60 std::string _buffer;
61 time_t _last_write;
62 bool _established = false;
63 backoff _backoff_type = backoff::none;
64 std::chrono::milliseconds _backoff_amount;
65 bool _receive_all_replies = false;
66 };
67
68 client(const auth& _auth);
69 ~client();
70
71 response updateStatus(std::string msg, tweet& result, tweet in_response_to = tweet(), std::list<long> media_ids = {});
72 response uploadMedia(std::string media_type, const char* data, long data_length, long& media_id);
73
74 response follow(user_id toFollow);
75 response follow(user toFollow);
76
77 response unfollow(user_id toUnfollow);
78 response unfollow(user toUnfollow);
79
80 response getFriends(std::set<user_id>& result);
81 response getFollowers(std::set<user_id>& result);
82
83 response getUser(user& result);
84
85 configuration getConfiguration();
86
87 // NOTE: stream setting function calls will fail silently when stream is running
88 void setUserStreamNotifyCallback(stream::notify_callback callback);
89 void setUserStreamReceiveAllReplies(bool _arg);
90 void startUserStream();
91 void stopUserStream();
92
93 std::string generateReplyPrefill(tweet t) const;
94
95 private:
96 friend class stream;
97
98 OAuth::Consumer* _oauth_consumer;
99 OAuth::Token* _oauth_token;
100 OAuth::Client* _oauth_client;
101
102 user _current_user;
103 stream _user_stream{*this};
104
105 configuration _configuration;
106 time_t _last_configuration_update;
107
108 bool performGet(std::string url, long& response_code, std::string& result);
109 bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result);
110 bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result);
111 response codeForError(int httpcode, std::string errors) const;
112 }; 53 };
113 54
114}; 55};