cpp Rest API master
C++ library for REST API access with Qt, Curl and cpp-httplib backends
 
Loading...
Searching...
No Matches
iconnection.hpp
Go to the documentation of this file.
1
2#ifndef ACONNECTION_HPP
3#define ACONNECTION_HPP
4
5#include <atomic>
6#include <expected>
7#include <functional>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "http_error.hpp"
13
14namespace cpp_restapi
15{
16 // Forward declarations – include the corresponding header when
17 // you need the full definition (e.g. to access SseEvent members).
18 struct SseEvent;
19 struct ISseConnection;
20 struct IPaginationStrategy;
21
25 struct Response
26 {
27 std::string body;
28 std::string headers;
29 int statusCode = 0;
30 };
31
33 using CancellationToken = std::shared_ptr<std::atomic<bool>>;
34
39 {
40 public:
41 using EventCallback = std::function<void(const SseEvent&)>;
42 using FetchCallback = std::function<void(Response)>;
43 using BodyCallback = std::function<void(std::string)>;
44 using ErrorCallback = std::function<void(HttpError)>;
45
46 virtual ~IConnection() = default;
47
48 // -- connection info --
49
53 virtual const std::string& url() const = 0;
54
55 // -- synchronous fetch --
56
66 virtual std::expected<std::string, HttpError> fetch(const std::string& request) = 0;
67
74 virtual std::expected<std::string, HttpError> fetch(const std::string& request, IPaginationStrategy& strategy) = 0;
75
81 virtual std::expected<Response, HttpError> fetchResponse(const std::string& url) = 0;
82
83 // -- asynchronous fetch --
84
99 virtual CancellationToken fetch(const std::string& request,
100 FetchCallback onSuccess,
101 ErrorCallback onError = {}) = 0;
102
116 virtual CancellationToken fetch(const std::string& request,
117 IPaginationStrategy& strategy,
118 BodyCallback onSuccess,
119 ErrorCallback onError = {}) = 0;
120
121 // -- Server-Sent Events --
122
129 virtual std::unique_ptr<ISseConnection> subscribe(const std::string& request, EventCallback callback) = 0;
130
131 // -- deprecated --
132
140 [[deprecated("Use fetch() or fetch() with IPaginationStrategy instead")]]
141 virtual std::string get(const std::string& request) = 0;
142 };
143}
144
145#endif
Definition base_connection.hpp:13
std::shared_ptr< std::atomic< bool > > CancellationToken
Token returned by asynchronous fetch(); set it to true to cancel the request.
Definition iconnection.hpp:33
Represents an HTTP or network-level error.
Definition http_error.hpp:28
Interface representing connection with rest api server.
Definition iconnection.hpp:39
std::function< void(Response)> FetchCallback
Definition iconnection.hpp:42
virtual std::expected< Response, HttpError > fetchResponse(const std::string &url)=0
Perform a single HTTP request returning the full response.
virtual const std::string & url() const =0
return API url
virtual std::expected< std::string, HttpError > fetch(const std::string &request, IPaginationStrategy &strategy)=0
Perform requests with automatic pagination.
virtual CancellationToken fetch(const std::string &request, IPaginationStrategy &strategy, BodyCallback onSuccess, ErrorCallback onError={})=0
Perform paginated requests asynchronously.
virtual std::string get(const std::string &request)=0
perform a request to api
virtual ~IConnection()=default
std::function< void(HttpError)> ErrorCallback
Definition iconnection.hpp:44
std::function< void(std::string)> BodyCallback
Definition iconnection.hpp:43
virtual std::unique_ptr< ISseConnection > subscribe(const std::string &request, EventCallback callback)=0
Subscribe to an SSE endpoint.
std::function< void(const SseEvent &)> EventCallback
Definition iconnection.hpp:41
virtual std::expected< std::string, HttpError > fetch(const std::string &request)=0
Perform a single HTTP request.
virtual CancellationToken fetch(const std::string &request, FetchCallback onSuccess, ErrorCallback onError={})=0
Perform an HTTP GET request asynchronously.
Interface for pagination strategies.
Definition ipagination_strategy.hpp:17
HTTP response containing body, raw headers and the HTTP status code.
Definition iconnection.hpp:26
std::string headers
Definition iconnection.hpp:28
std::string body
Definition iconnection.hpp:27
int statusCode
HTTP status code (e.g. 200, 404). 0 means no response was received (network error).
Definition iconnection.hpp:29
Represents a single Server-Sent Event.
Definition sse_event.hpp:15