about summary refs log tree commit diff stats
path: root/src/client.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/client.h')
-rw-r--r--src/client.h77
1 files changed, 72 insertions, 5 deletions
diff --git a/src/client.h b/src/client.h index 3a133e4..ae80ed9 100644 --- a/src/client.h +++ b/src/client.h
@@ -7,6 +7,12 @@
7#include <list> 7#include <list>
8#include <curl_easy.h> 8#include <curl_easy.h>
9#include <curl_header.h> 9#include <curl_header.h>
10#include <thread>
11#include <mutex>
12#include "notification.h"
13#include <set>
14#include <ctime>
15#include <chrono>
10 16
11namespace OAuth { 17namespace OAuth {
12 class Consumer; 18 class Consumer;
@@ -18,21 +24,82 @@ namespace twitter {
18 24
19 class client { 25 class client {
20 public: 26 public:
27 class stream {
28 public:
29 typedef std::function<void(notification _notification)> notify_callback;
30
31 stream(client& _client);
32
33 void setNotifyCallback(notify_callback _n);
34
35 bool isRunning() const;
36 void start();
37 void stop();
38
39 private:
40 enum class backoff {
41 none,
42 network,
43 http,
44 rate_limit
45 };
46
47 void run();
48 int progress();
49 int write(char* ptr, size_t size, size_t nmemb);
50
51 friend int client_stream_progress_callback_wrapper(void* stream, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
52 friend size_t client_stream_write_callback_wrapper(void* ptr, size_t size, size_t nmemb, void* stream);
53
54 client& _client;
55 notify_callback _notify;
56 bool _stop = false;
57 std::thread _thread;
58 std::mutex _running_mutex;
59 std::mutex _notify_mutex;
60 std::mutex _stall_mutex;
61 std::string _buffer;
62 time_t _last_write;
63 bool _established = false;
64 backoff _backoff_type = backoff::none;
65 std::chrono::milliseconds _backoff_amount;
66 };
67
21 client(const auth& _auth); 68 client(const auth& _auth);
22 ~client(); 69 ~client();
23 70
24 response updateStatus(std::string msg, tweet& result, std::list<long> media_ids = {}); 71 response updateStatus(std::string msg, tweet& result, tweet in_response_to = tweet(), std::list<long> media_ids = {});
25 response uploadMedia(std::string media_type, const char* data, long data_length, long& media_id); 72 response uploadMedia(std::string media_type, const char* data, long data_length, long& media_id);
26 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 const user& getUser() const;
84
85 void setUserStreamNotifyCallback(stream::notify_callback callback);
86 void startUserStream();
87 void stopUserStream();
88
27 private: 89 private:
90 friend class stream;
91
28 OAuth::Consumer* _oauth_consumer; 92 OAuth::Consumer* _oauth_consumer;
29 OAuth::Token* _oauth_token; 93 OAuth::Token* _oauth_token;
30 OAuth::Client* _oauth_client; 94 OAuth::Client* _oauth_client;
31 95
32 bool performGet(std::string url, long& response_code, json& result); 96 user _current_user;
33 bool performPost(std::string url, std::string dataStr, long& response_code, json& result); 97 stream _user_stream{*this};
34 bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, json& result); 98
35 response codeForError(int httpcode, json errors) const; 99 bool performGet(std::string url, long& response_code, std::string& result);
100 bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result);
101 bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result);
102 response codeForError(int httpcode, std::string errors) const;
36 }; 103 };
37 104
38}; 105};