This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.h
90 lines (81 loc) · 2.84 KB
/
client.h
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
/* Copyright (c) 2017, EPFL/Blue Brain Project
*
* This file is part of Rockets <https://github.com/BlueBrain/Rockets>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library 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 library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef ROCKETS_HTTP_CLIENT_H
#define ROCKETS_HTTP_CLIENT_H
#include <rockets/http/request.h>
#include <rockets/http/response.h>
#include <rockets/socketBasedInterface.h>
namespace rockets
{
namespace http
{
/**
* Client for making asynchronous HTTP requests.
*
* Example usage: @include apps/httpRequest.cpp
*/
class Client : public SocketBasedInterface
{
public:
/** @name Setup */
//@{
/** Construct a new client. */
ROCKETS_API Client();
/** Close the client. */
ROCKETS_API ~Client();
//@}
/**
* Make an http request.
*
* @param uri to address the request.
* @param method http method to use.
* @param body optional payload to send.
* @throw std::invalid_argument if the uri is too long (>4000 char) or
* some parameter is invalid or not supported.
* @return future http response - can be a std::runtime_error if the
* request fails.
*/
ROCKETS_API std::future<http::Response> request(
const std::string& uri, http::Method method = http::Method::GET,
std::string body = std::string());
/**
* Make an http request.
*
* @param uri to address the request.
* @param method http method to use.
* @param body optional payload to send.
* @param callback for the http response.
* @param errorCallback used to report request failure (optional).
* @throw std::invalid_argument if the uri is too long (>4000 char) or
* some parameter is invalid or not supported.
*/
ROCKETS_API void request(
const std::string& uri, http::Method method, std::string body,
std::function<void(http::Response)> callback,
std::function<void(std::string)> errorCallback = {});
class Impl; // must be public for static_cast from C callback
private:
std::unique_ptr<Impl> _impl;
void _setSocketListener(SocketListener* listener) final;
void _processSocket(SocketDescriptor fd, int events) final;
void _process(int timeout_ms) final;
};
}
}
#endif