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_
method.h
core::net::http::Client::base64_encode
virtual std::string base64_encode(const std::string &s) const =0
Base64-encodes the given string.
core::Location
Definition: location.h:26
visibility.h
core::net::http::Client::Client
Client(const Client &)=delete
core::net::http::Client::post
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.
core::net::http::Error
Definition: http/error.h:30
core::net::http::Client::get
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.
core::net::http::Client::uri_to_string
virtual std::string uri_to_string(const core::net::Uri &uri) const
core::net::http::Client::base64_decode
virtual std::string base64_decode(const std::string &s) const =0
Base64-decodes the given string.
core::net::http::Client::Timings::Statistics
Definition: client.h:67
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:163
core::net::http::Client::url_escape
virtual std::string url_escape(const std::string &s) const =0
Percent-encodes the given string.
core::net::http::Client
Definition: client.h:44
core::net::http::Client::Client
Client()=default
core::net::http::Client::Errors::HttpMethodNotSupported
HttpMethodNotSupported is thrown if the underlying impl. does not support the requested HTTP method.
Definition: client.h:55
core::net::http::Client::Errors::Errors
Errors()=delete
core::net::http::Client::run
virtual void run()=0
Execute the client and any impl-specific thread-pool or runtime.
core::net::http::ContentType
Collection of known content types.
Definition: content_type.h:33
core::net::http::Client::Errors::HttpMethodNotSupported::HttpMethodNotSupported
HttpMethodNotSupported(Method method, const core::Location &)
core::net::http::Client::Timings
Summarizes timing information about completed requests.
Definition: client.h:63
core::net::http::make_client
CORE_NET_DLL_PUBLIC std::shared_ptr< Client > make_client()
Dispatches to the default implementation and returns a client instance.
core::net::http::Method
Method
Definition: method.h:33
core
Definition: location.h:24
core::net::http::Client::stop
virtual void stop()=0
Stop the client and any impl-specific thread-pool or runtime.
request.h
core::net::http::Client::put
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.
core::net::http::Client::post
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.
core::net::http::Client::Timings::Seconds
std::chrono::duration< double > Seconds
Definition: client.h:64
CORE_NET_DLL_PUBLIC
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25
core::net::Uri
The Uri class encapsulates the components of a URI.
Definition: uri.h:37
core::net::http::Client::Errors::HttpMethodNotSupported::method
Method method
Definition: client.h:57
core::net::http::Client::timings
virtual Timings timings()=0
Queries timing statistics over all requests that have been executed by this client.
core::net::http::Client::del
std::shared_ptr< Request > del(const Request::Configuration &configuration)
del is a convenience method for issueing a DELETE request for the given URI.
core::net::http::Client::~Client
virtual ~Client()=default
core::net::http::Client::post_form
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...
core::net::http::Client::head
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.
core::net::http::Client::operator=
Client & operator=(const Client &)=delete
core::net::http::Request
The Request class encapsulates a request for a web resource.
Definition: request.h:42
core::net::http::Client::Errors
Summarizes error conditions.
Definition: client.h:48
core::net::http::Client::operator==
bool operator==(const Client &) const =delete