client.h
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
* Gary Wang <gary.wang@canonical.com>
*/
#ifndef CORE_NET_HTTP_CLIENT_H_
#define CORE_NET_HTTP_CLIENT_H_
#include <core/net/visibility.h>
#include <core/net/http/method.h>
#include <core/net/http/request.h>
#include <chrono>
#include <iosfwd>
#include <memory>
namespace core
{
namespace net
{
struct Uri;
namespace http
{
class ContentType;
class Request;
class CORE_NET_DLL_PUBLIC Client
{
public:
struct Errors
{
Errors() = delete;
struct HttpMethodNotSupported : public http::Error
{
HttpMethodNotSupported(Method method, const core::Location&);
Method method;
};
};
struct Timings
{
typedef std::chrono::duration<double> Seconds;
struct Statistics
{
Seconds max{Seconds::max()};
Seconds min{Seconds::max()};
Seconds mean{Seconds::max()};
Seconds variance{Seconds::max()};
};
Statistics name_look_up{};
Statistics connect{};
Statistics app_connect{};
Statistics pre_transfer{};
Statistics start_transfer{};
Statistics total{};
};
Client(const Client&) = delete;
virtual ~Client() = default;
Client& operator=(const Client&) = delete;
bool operator==(const Client&) const = delete;
virtual std::string uri_to_string (const core::net::Uri& uri) const;
virtual std::string url_escape(const std::string& s) const = 0;
virtual std::string base64_encode(const std::string& s) const = 0;
virtual std::string base64_decode(const std::string& s) const = 0;
virtual Timings timings() = 0;
virtual void run() = 0;
virtual void stop() = 0;
virtual std::shared_ptr<Request> get(const Request::Configuration& configuration) = 0;
virtual std::shared_ptr<Request> head(const Request::Configuration& configuration) = 0;
virtual std::shared_ptr<Request> put(const Request::Configuration& configuration, std::istream& payload, std::size_t size) = 0;
virtual std::shared_ptr<Request> post(const Request::Configuration& configuration, const std::string& payload, const std::string& type) = 0;
virtual std::shared_ptr<Request> post_form(const Request::Configuration& configuration, const std::map<std::string, std::string>& values);
std::shared_ptr<Request> post(const Request::Configuration& configuration, std::istream& payload, std::size_t size);
std::shared_ptr<Request> del(const Request::Configuration& configuration);
protected:
Client() = default;
};
CORE_NET_DLL_PUBLIC std::shared_ptr<Client> make_client();
}
}
}
#endif // CORE_NET_HTTP_CLIENT_H_
core::net::http::Client::operator==
bool operator==(const Client &) const =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::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::uri_to_string
virtual std::string uri_to_string(const core::net::Uri &uri) const
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::base64_decode
virtual std::string base64_decode(const std::string &s) const =0
Base64-decodes the given string.
core::net::http::Client::Client
Client()=default
core::net::http::Client::~Client
virtual ~Client()=default
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::Client
Client(const Client &)=delete
core::net::http::Client::base64_encode
virtual std::string base64_encode(const std::string &s) const =0
Base64-encodes the given string.
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::operator=
Client & operator=(const Client &)=delete
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::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::run
virtual void run()=0
Execute the client and any impl-specific thread-pool or runtime.
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::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::stop
virtual void stop()=0
Stop the client and any impl-specific thread-pool or runtime.
core::net::http::Error
Definition http/error.h:30
core::net::http::Request
The Request class encapsulates a request for a web resource.
Definition request.h:42
method.h
core::net::http
Definition client.h:39
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::net
Definition error.h:28
core
Definition location.h:24
request.h
core::Location
Definition location.h:26
core::net::Uri
The Uri class encapsulates the components of a URI.
Definition uri.h:37
core::net::http::Client::Errors::HttpMethodNotSupported::HttpMethodNotSupported
HttpMethodNotSupported(Method method, const core::Location &)
core::net::http::Client::Errors::HttpMethodNotSupported::method
Method method
Definition client.h:57
core::net::http::Client::Errors::Errors
Errors()=delete
core::net::http::Client::Timings::Statistics
Definition client.h:67
core::net::http::Client::Timings::Statistics::max
Seconds max
Definition client.h:69
core::net::http::Client::Timings::Statistics::mean
Seconds mean
Definition client.h:73
core::net::http::Client::Timings::Statistics::min
Seconds min
Definition client.h:71
core::net::http::Client::Timings::Statistics::variance
Seconds variance
Definition client.h:75
core::net::http::Client::Timings
Summarizes timing information about completed requests.
Definition client.h:63
core::net::http::Client::Timings::pre_transfer
Statistics pre_transfer
Definition client.h:89
core::net::http::Client::Timings::start_transfer
Statistics start_transfer
Definition client.h:91
core::net::http::Client::Timings::name_look_up
Statistics name_look_up
Definition client.h:79
core::net::http::Client::Timings::Seconds
std::chrono::duration< double > Seconds
Definition client.h:64
core::net::http::Client::Timings::total
Statistics total
Definition client.h:93
core::net::http::Client::Timings::app_connect
Statistics app_connect
Definition client.h:87
core::net::http::Client::Timings::connect
Statistics connect
Definition client.h:83
core::net::http::ContentType
Collection of known content types.
Definition content_type.h:33
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition request.h:163
visibility.h
CORE_NET_DLL_PUBLIC
#define CORE_NET_DLL_PUBLIC
Definition visibility.h:25