cpp Rest API master
C++ library for REST API access with Qt, Curl and cpp-httplib backends
 
Loading...
Searching...
No Matches
base_connection.hpp
Go to the documentation of this file.
1
2#ifndef BASE_CONNECTION_HPP_INCLUDED
3#define BASE_CONNECTION_HPP_INCLUDED
4
5#include <expected>
6#include <map>
7
9#include "cpp_restapi_export.h"
10
11
12namespace cpp_restapi
13{
17 class CPP_RESTAPI_EXPORT BaseConnection: public cpp_restapi::IConnection
18 {
19 public:
20 explicit BaseConnection(const std::string& address, const std::map<std::string, std::string>& headerEntries);
21
22 // -- connection info --
23 const std::string& url() const final;
24
25 // -- synchronous fetch --
26 std::expected<std::string, HttpError> fetch(const std::string& request) final;
27 std::expected<std::string, HttpError> fetch(const std::string& request, IPaginationStrategy& strategy) final;
28 std::expected<Response, HttpError> fetchResponse(const std::string& url) final;
29
30 // -- asynchronous fetch --
31 CancellationToken fetch(const std::string& request, FetchCallback onSuccess, ErrorCallback onError = {}) final;
32 CancellationToken fetch(const std::string& request, IPaginationStrategy& strategy, BodyCallback onSuccess, ErrorCallback onError = {}) final;
33
34 // -- deprecated --
35 std::string get(const std::string &) final;
36
37 // -- backend interface --
38 virtual Response fetchPage(const std::string& request) = 0;
39
40 protected:
41 virtual void fetchAsync(const std::string& fullUrl,
42 CancellationToken cancel,
43 FetchCallback onSuccess,
44 ErrorCallback onError) = 0;
45
46 const std::map<std::string, std::string>& getHeaderEntries() const;
47
48 const std::string& address() const;
49
50 private:
51 const std::string m_address;
52 std::map<std::string, std::string> m_headerEntries;
53 };
54}
55
56#endif
base class with common parts for backend specific implementations
Definition base_connection.hpp:18
std::string get(const std::string &) final
perform a request to api
virtual void fetchAsync(const std::string &fullUrl, CancellationToken cancel, FetchCallback onSuccess, ErrorCallback onError)=0
BaseConnection(const std::string &address, const std::map< std::string, std::string > &headerEntries)
CancellationToken fetch(const std::string &request, IPaginationStrategy &strategy, BodyCallback onSuccess, ErrorCallback onError={}) final
Perform paginated requests asynchronously.
const std::string & url() const final
return API url
const std::map< std::string, std::string > & getHeaderEntries() const
const std::string & address() const
virtual Response fetchPage(const std::string &request)=0
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
std::function< void(HttpError)> ErrorCallback
Definition iconnection.hpp:44
std::function< void(std::string)> BodyCallback
Definition iconnection.hpp:43
Interface for pagination strategies.
Definition ipagination_strategy.hpp:17
HTTP response containing body, raw headers and the HTTP status code.
Definition iconnection.hpp:26