net-cpp  ..
C++11 library for networking purposes
client.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  * Gary Wang <gary.wang@canonical.com>
18  */
19 #ifndef CORE_NET_HTTP_CLIENT_H_
20 #define CORE_NET_HTTP_CLIENT_H_
21 
22 #include <core/net/visibility.h>
23 
24 #include <core/net/http/method.h>
25 #include <core/net/http/request.h>
26 
27 #include <chrono>
28 #include <iosfwd>
29 #include <memory>
30 
31 namespace core
32 {
33 namespace net
34 {
35 
36 struct Uri;
37 
38 namespace http
39 {
40 class ContentType;
41 class Request;
42 
44 {
45 public:
47  struct Errors
48  {
49  Errors() = delete;
50 
55  {
58  };
59  };
60 
62  struct Timings
63  {
64  typedef std::chrono::duration<double> Seconds;
65 
66  struct Statistics
67  {
69  Seconds max{Seconds::max()};
71  Seconds min{Seconds::max()};
73  Seconds mean{Seconds::max()};
75  Seconds variance{Seconds::max()};
76  };
77 
79  Statistics name_look_up{};
83  Statistics connect{};
87  Statistics app_connect{};
89  Statistics pre_transfer{};
91  Statistics start_transfer{};
93  Statistics total{};
94  };
95 
96  Client(const Client&) = delete;
97  virtual ~Client() = default;
98 
99  Client& operator=(const Client&) = delete;
100  bool operator==(const Client&) const = delete;
101 
102  virtual std::string uri_to_string (const core::net::Uri& uri) const;
103 
105  virtual std::string url_escape(const std::string& s) const = 0;
106 
108  virtual std::string base64_encode(const std::string& s) const = 0;
109 
111  virtual std::string base64_decode(const std::string& s) const = 0;
112 
114  virtual Timings timings() = 0;
115 
117  virtual void run() = 0;
118 
120  virtual void stop() = 0;
121 
128  virtual std::shared_ptr<Request> get(const Request::Configuration& configuration) = 0;
129 
136  virtual std::shared_ptr<Request> head(const Request::Configuration& configuration) = 0;
137 
146  virtual std::shared_ptr<Request> put(const Request::Configuration& configuration, std::istream& payload, std::size_t size) = 0;
147 
156  virtual std::shared_ptr<Request> post(const Request::Configuration& configuration, const std::string& payload, const std::string& type) = 0;
157 
165  virtual std::shared_ptr<Request> post_form(const Request::Configuration& configuration, const std::map<std::string, std::string>& values);
166 
175  std::shared_ptr<Request> post(const Request::Configuration& configuration, std::istream& payload, std::size_t size);
176 
183  std::shared_ptr<Request> del(const Request::Configuration& configuration);
184 
185 protected:
186  Client() = default;
187 };
188 
190 CORE_NET_DLL_PUBLIC std::shared_ptr<Client> make_client();
191 }
192 }
193 }
194 
195 #endif // CORE_NET_HTTP_CLIENT_H_
bool operator==(const Client &) const =delete
virtual std::string uri_to_string(const core::net::Uri &uri) const
virtual std::string base64_decode(const std::string &s) const =0
Base64-decodes the given string.
virtual std::shared_ptr< Request > post(const Request::Configuration &configuration, const std::string &payload, const std::string &type)=0
post is a convenience method for issuing a POST request for the given URI.
virtual std::shared_ptr< Request > get(const Request::Configuration &configuration)=0
get is a convenience method for issueing a GET request for the given URI.
virtual ~Client()=default
virtual Timings timings()=0
Queries timing statistics over all requests that have been executed by this client.
virtual std::shared_ptr< Request > post_form(const Request::Configuration &configuration, const std::map< std::string, std::string > &values)
post_form is a convenience method for issuing a POST request for the given URI, with url-encoded payl...
Client & operator=(const Client &)=delete
Client(const Client &)=delete
virtual std::string base64_encode(const std::string &s) const =0
Base64-encodes the given string.
std::shared_ptr< Request > post(const Request::Configuration &configuration, std::istream &payload, std::size_t size)
post is a convenience method for issuing a POST request for the given URI.
virtual std::shared_ptr< Request > head(const Request::Configuration &configuration)=0
head is a convenience method for issueing a HEAD request for the given URI.
std::shared_ptr< Request > del(const Request::Configuration &configuration)
del is a convenience method for issueing a DELETE request for the given URI.
virtual std::shared_ptr< Request > put(const Request::Configuration &configuration, std::istream &payload, std::size_t size)=0
put is a convenience method for issuing a PUT request for the given URI.
virtual std::string url_escape(const std::string &s) const =0
Percent-encodes the given string.
virtual void run()=0
Execute the client and any impl-specific thread-pool or runtime.
virtual void stop()=0
Stop the client and any impl-specific thread-pool or runtime.
The Request class encapsulates a request for a web resource.
Definition: request.h:42
CORE_NET_DLL_PUBLIC std::shared_ptr< Client > make_client()
Dispatches to the default implementation and returns a client instance.
Definition: location.h:24
The Uri class encapsulates the components of a URI.
Definition: uri.h:37
HttpMethodNotSupported is thrown if the underlying impl. does not support the requested HTTP method.
Definition: client.h:55
HttpMethodNotSupported(Method method, const core::Location &)
Summarizes error conditions.
Definition: client.h:48
Summarizes timing information about completed requests.
Definition: client.h:63
std::chrono::duration< double > Seconds
Definition: client.h:64
Collection of known content types.
Definition: content_type.h:33
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:163
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25