about summary refs log tree commit diff stats
path: root/src/client.h
blob: 901c7b5683ff62c36be0fcdea941d026afe08eb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef TWITTER_H_ABFF6A12
#define TWITTER_H_ABFF6A12

#include "codes.h"
#include "tweet.h"
#include "auth.h"
#include <list>
#include <thread>
#include <mutex>
#include "notification.h"
#include <set>
#include <ctime>
#include <chrono>

namespace OAuth {
  class Consumer;
  class Token;
  class Client;
};

class curl_httppost;

namespace twitter {
  
  class client {
    public:
      class stream {
        public:
          typedef std::function<void(notification _notification)> notify_callback;
          
          stream(client& _client);
          
          void setNotifyCallback(notify_callback _n);
          
          bool isRunning() const;
          void start();
          void stop();
          
          int progress();
          size_t write(char* ptr, size_t size, size_t nmemb);
          
        private:
          enum class backoff {
            none,
            network,
            http,
            rate_limit
          };
          
          void run();
          
          client& _client;
          notify_callback _notify;
          bool _stop = false;
          std::thread _thread;
          std::mutex _running_mutex;
          std::mutex _notify_mutex;
          std::mutex _stall_mutex;
          std::string _buffer;
          time_t _last_write;
          bool _established = false;
          backoff _backoff_type = backoff::none;
          std::chrono::milliseconds _backoff_amount;
      };
      
      client(const auth& _auth);
      ~client();
      
      response updateStatus(std::string msg, tweet& result, tweet in_response_to = tweet(), std::list<long> media_ids = {});
      response uploadMedia(std::string media_type, const char* data, long data_length, long& media_id);
      
      response follow(user_id toFollow);
      response follow(user toFollow);
      
      response unfollow(user_id toUnfollow);
      response unfollow(user toUnfollow);
      
      response getFriends(std::set<user_id>& result);
      response getFollowers(std::set<user_id>& result);
      
      const user& getUser() const;
      
      void setUserStreamNotifyCallback(stream::notify_callback callback);
      void startUserStream();
      void stopUserStream();
      
      std::string generateReplyPrefill(tweet t) const;
      
    private:
      friend class stream;
      
      OAuth::Consumer* _oauth_consumer;
      OAuth::Token* _oauth_token;
      OAuth::Client* _oauth_client;
      
      user _current_user;
      stream _user_stream{*this};
      
      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);
      response codeForError(int httpcode, std::string errors) const;
  };
  
};

#endif /* end of include guard: TWITTER_H_ABFF6A12 */