cpp Rest API master
C++ library for REST API access with Qt, Curl and cpp-httplib backends
 
Loading...
Searching...
No Matches
cpp_restapi::BaseConnection Class Referenceabstract

base class with common parts for backend specific implementations More...

#include <base_connection.hpp>

Inheritance diagram for cpp_restapi::BaseConnection:
cpp_restapi::IConnection cpp_restapi::QtBackend::Connection cpp_restapi::ThreadedConnection cpp_restapi::CppHttplibBackend::Connection cpp_restapi::CurlBackend::Connection

Public Member Functions

 BaseConnection (const std::string &address, const std::map< std::string, std::string > &headerEntries)
 
const std::string & url () const final
 return API url
 
std::expected< std::string, HttpErrorfetch (const std::string &request) final
 Perform a single HTTP request.
 
std::expected< std::string, HttpErrorfetch (const std::string &request, IPaginationStrategy &strategy) final
 Perform requests with automatic pagination.
 
std::expected< Response, HttpErrorfetchResponse (const std::string &url) final
 Perform a single HTTP request returning the full response.
 
CancellationToken fetch (const std::string &request, FetchCallback onSuccess, ErrorCallback onError={}) final
 Perform an HTTP GET request asynchronously.
 
CancellationToken fetch (const std::string &request, IPaginationStrategy &strategy, BodyCallback onSuccess, ErrorCallback onError={}) final
 Perform paginated requests asynchronously.
 
std::string get (const std::string &) final
 perform a request to api
 
virtual Response fetchPage (const std::string &request)=0
 
- Public Member Functions inherited from cpp_restapi::IConnection
virtual ~IConnection ()=default
 
virtual std::unique_ptr< ISseConnectionsubscribe (const std::string &request, EventCallback callback)=0
 Subscribe to an SSE endpoint.
 

Protected Member Functions

virtual void fetchAsync (const std::string &fullUrl, CancellationToken cancel, FetchCallback onSuccess, ErrorCallback onError)=0
 
const std::map< std::string, std::string > & getHeaderEntries () const
 
const std::string & address () const
 

Additional Inherited Members

- Public Types inherited from cpp_restapi::IConnection
using EventCallback = std::function<void(const SseEvent&)>
 
using FetchCallback = std::function<void(Response)>
 
using BodyCallback = std::function<void(std::string)>
 
using ErrorCallback = std::function<void(HttpError)>
 

Detailed Description

base class with common parts for backend specific implementations

Constructor & Destructor Documentation

◆ BaseConnection()

cpp_restapi::BaseConnection::BaseConnection ( const std::string & address,
const std::map< std::string, std::string > & headerEntries )
explicit

Member Function Documentation

◆ address()

const std::string & cpp_restapi::BaseConnection::address ( ) const
protected

◆ fetch() [1/4]

std::expected< std::string, HttpError > cpp_restapi::BaseConnection::fetch ( const std::string & request)
finalvirtual

Perform a single HTTP request.

Parameters
requestrelative API path (e.g. "api/v1/disks")
Returns
response body on success, or an HttpError describing the failure

Returns std::unexpected(HttpError) when:

  • The server returns a 4xx or 5xx status code
  • A network-level failure occurs (statusCode == 0 in the error)

Implements cpp_restapi::IConnection.

Reimplemented in cpp_restapi::QtBackend::Connection, and cpp_restapi::ThreadedConnection.

◆ fetch() [2/4]

CancellationToken cpp_restapi::BaseConnection::fetch ( const std::string & request,
FetchCallback onSuccess,
ErrorCallback onError = {} )
finalvirtual

Perform an HTTP GET request asynchronously.

Non-blocking. onSuccess is called with the full response when the request completes successfully; onError is called with an HttpError if the request fails. The callbacks may be invoked from a background thread (non-Qt backends) or from the Qt event-loop thread (Qt backend) — callers must handle thread-safety accordingly.

Parameters
requestrelative API path (e.g. "users/octocat")
onSuccesscalled with Response{body, headers, statusCode} on success
onErrorcalled with an HttpError on failure (optional)
Returns
cancellation token; store it and set to true to suppress callbacks

Implements cpp_restapi::IConnection.

Reimplemented in cpp_restapi::QtBackend::Connection, and cpp_restapi::ThreadedConnection.

◆ fetch() [3/4]

std::expected< std::string, HttpError > cpp_restapi::BaseConnection::fetch ( const std::string & request,
IPaginationStrategy & strategy )
finalvirtual

Perform requests with automatic pagination.

Parameters
requestrelative API path
strategypagination strategy defining how to discover next page and merge results
Returns
merged response body from all pages on success, or an HttpError on first failure

Implements cpp_restapi::IConnection.

Reimplemented in cpp_restapi::QtBackend::Connection, and cpp_restapi::ThreadedConnection.

◆ fetch() [4/4]

CancellationToken cpp_restapi::BaseConnection::fetch ( const std::string & request,
IPaginationStrategy & strategy,
BodyCallback onSuccess,
ErrorCallback onError = {} )
finalvirtual

Perform paginated requests asynchronously.

Non-blocking. Pages are fetched sequentially; once all pages have been collected the merged result is delivered via onSuccess. On any page failure onError is called and fetching stops.

Parameters
requestrelative API path (e.g. "repos/owner/name/issues")
strategypagination strategy (next-page discovery and merge)
onSuccesscalled with the merged body string on success
onErrorcalled with an HttpError on failure (optional)
Returns
cancellation token

Implements cpp_restapi::IConnection.

Reimplemented in cpp_restapi::QtBackend::Connection, and cpp_restapi::ThreadedConnection.

◆ fetchAsync()

virtual void cpp_restapi::BaseConnection::fetchAsync ( const std::string & fullUrl,
CancellationToken cancel,
FetchCallback onSuccess,
ErrorCallback onError )
protectedpure virtual

◆ fetchPage()

virtual Response cpp_restapi::BaseConnection::fetchPage ( const std::string & request)
pure virtual

◆ fetchResponse()

std::expected< Response, HttpError > cpp_restapi::BaseConnection::fetchResponse ( const std::string & url)
finalvirtual

Perform a single HTTP request returning the full response.

Parameters
urlfull URL to fetch
Returns
full Response (body, headers, statusCode) on success, or an HttpError on failure

Implements cpp_restapi::IConnection.

◆ get()

std::string cpp_restapi::BaseConnection::get ( const std::string & request)
finalvirtual

perform a request to api

Parameters
requestapi request. For example "users/SomeUserName/repos"
Returns
api response in json format
Deprecated
Use fetch() for single requests or fetch() with IPaginationStrategy for paginated requests. This method has hardcoded GitHub-specific pagination logic.

Implements cpp_restapi::IConnection.

◆ getHeaderEntries()

const std::map< std::string, std::string > & cpp_restapi::BaseConnection::getHeaderEntries ( ) const
protected

◆ url()

const std::string & cpp_restapi::BaseConnection::url ( ) const
finalvirtual

return API url

Implements cpp_restapi::IConnection.


The documentation for this class was generated from the following file: