Operator
C++ library for plugging into matrix.org
http_client.hh
Go to the documentation of this file.
1 
20 #ifndef __OPERATOR_HTTPCLIENT_HH_
21 #define __OPERATOR_HTTPCLIENT_HH_
22 
23 #include "client.hh"
24 #include "structs.hh"
25 #include "http.hh"
26 
27 #include <memory>
28 #include <string>
29 #include <vector>
30 #include <map>
31 #include <istream>
32 
33 namespace Operator {
34  namespace Client {
55  class HTTPClientAPI {
56  public:
57  HTTPClientAPI(HTTP::Adapter &, const std::string &server);
58 
63  void versions(const callback_t<const std::vector<std::string> &> &callback);
64 
72  void login(const std::string &username, const std::string &password, const callback_t<const std::string&,const std::string&> &callback);
73 
77  void loginWith3PID(const std::string &medium, const std::string &address, const std::string &password, const callback_t<const std::string&,const std::string&> &);
78 
81  void loginWithToken(const std::string &token);
82 
86  void registerAccount(const std::string &username, const std::string &password, const callback_t<std::string,std::string> &);
87 
91  void logout(const callback_t<> &);
92 
95  bool isLoggedIn() const {
96  return !token.empty();
97  }
98 
101  const std::string &getToken() const {
102  return token;
103  }
104 
108  void sync(const std::string &since, const std::string &filter, bool full_state, std::string set_presence, size_t timeout, const callback_t<const std::string &, nlohmann::json, nlohmann::json> &);
109 
113  void roomMessages(const std::string &roomId, const std::string &from, const std::string &to, const std::string &dir, int limit, const callback_t<const std::string &, const std::string &, const std::vector<nlohmann::json> &> &);
114 
118  void sendMessage(const std::string &roomId, const std::string &eventType, const std::string &txnId, const nlohmann::json &content, const callback_t<const std::string &> &);
119  protected:
120  HTTP::Adapter &adapter;
121  std::string baseurl;
122  private:
123  mutable std::string token;
124 
125  void handleLogin(std::unique_ptr<Error::Error>, short code, const HTTP::headers_t &, std::unique_ptr<std::istream>, callback_t<const std::string&,const std::string&>) throw ();
126  void handleLogout(std::unique_ptr<Error::Error>, short code, const HTTP::headers_t &, std::unique_ptr<std::istream>, callback_t<>) throw ();
127  };
128 
131  class HTTPClient : public Client {
136  public:
137  HTTPClient(HTTP::Adapter &adapter, const std::string &server);
138 
139  void login(const std::string &username, const std::string &password, const callback_t<const std::string &,const std::string &> &callback = noop_callback<const std::string &,const std::string &>);
140  void loginWith3PID(const std::string &medium, const std::string &address, const std::string &password, const callback_t<const std::string &,const std::string &> &callback = noop_callback<const std::string &,const std::string &>);
141  void loginWithToken(const std::string &token) {
142  client.loginWithToken(token);
143  trigger(Event::Login("", ""));
144  }
145 
146  void logout(const callback_t<> &callback = noop_callback<>);
147 
148  bool isLoggedIn() const { return client.isLoggedIn(); }
149 
150  void startClient();
151  void stopClient();
152 
153  void sendMessage(const Event::MessageEvent &, const callback_t<const std::string &> & = noop_callback<const std::string &>);
154  protected:
155  HTTPClientAPI client;
156  private:
157  void handleSync(std::shared_ptr<Error::Error>, const std::string &, nlohmann::json, nlohmann::json) throw ();
159  std::function<void (const Event::Event &)> trigger_ptr;
160  std::function<void (const std::string &, std::shared_ptr<Error::Error>, const std::string &)> triggerError_ptr;
161 
162  bool sync_pending;
163  bool sync_active;
164  std::string next_batch;
165  unsigned int txn_counter;
166 
167  };
168  }
169 }
170 
171 #endif // __OPERATOR_HTTPCLIENT_HH_
client functionality
void loginWithToken(const std::string &token)
log in with an access token that has previously been obtained.
Definition: http_client.cc:202
general "message" events (a.k.a. timeline events)
Definition: event.hh:115
void loginWith3PID(const std::string &medium, const std::string &address, const std::string &password, const callback_t< const std::string &, const std::string &> &)
log in using third party ID and pasword.
Definition: http_client.cc:129
Definition: client.hh:32
void registerAccount(const std::string &username, const std::string &password, const callback_t< std::string, std::string > &)
register a new account.
void logout(const callback_t<> &)
log out.
Definition: http_client.cc:206
the client has logged in
Definition: event.hh:236
Matrix client interface for high-level API.This interface is implemented by the high-level API classe...
Definition: client.hh:41
void sync(const std::string &since, const std::string &filter, bool full_state, std::string set_presence, size_t timeout, const callback_t< const std::string &, nlohmann::json, nlohmann::json > &)
get events from the server.
Definition: http_client.cc:313
void roomMessages(const std::string &roomId, const std::string &from, const std::string &to, const std::string &dir, int limit, const callback_t< const std::string &, const std::string &, const std::vector< nlohmann::json > &> &)
get messages/state events from a room.
Definition: http_client.cc:398
HTTP adapter interface.
Definition: http.hh:48
bool isLoggedIn() const
determine if the client is currently logged in.
Definition: http_client.hh:148
bool isLoggedIn() const
determine if the client is currently logged in.
Definition: http_client.hh:95
std::vector< header_t > headers_t
list of HTTP headers.
Definition: http.hh:41
low-level Client-Server API over HTTP.
Definition: http_client.hh:55
void login(const std::string &username, const std::string &password, const callback_t< const std::string &, const std::string &> &callback)
log in using username and password
Definition: http_client.cc:116
high-level Client-Server API over HTTP.
Definition: http_client.hh:131
void sendMessage(const std::string &roomId, const std::string &eventType, const std::string &txnId, const nlohmann::json &content, const callback_t< const std::string &> &)
send a message event to a room.
Definition: http_client.cc:470
Common HTTP definitions.
const std::string & getToken() const
return the access token for the client session.
Definition: http_client.hh:101
void versions(const callback_t< const std::vector< std::string > &> &callback)
get the versions of the specification supported by the server.
Definition: http_client.cc:111
typename std::function< void(std::shared_ptr< Error::Error > err, T...)> callback_t
general type for callbacks
Definition: structs.hh:43
data types for Matrix