OpenMS
Loading...
Searching...
No Matches
NetworkGetRequest Class Reference

Synchronous HTTP GET request backed by libcurl. More...

#include <OpenMS/SYSTEM/NetworkGetRequest.h>

Collaboration diagram for NetworkGetRequest:
[legend]

Public Member Functions

 NetworkGetRequest ()
 
 ~NetworkGetRequest ()
 
void setUrl (const std::string &url)
 Set the URL to request.
 
void setTimeout (int seconds)
 Set the request timeout in seconds.
 
void run ()
 Execute the GET request synchronously.
 
std::string getResponse () const
 Response body as a string.
 
const std::vector< char > & getResponseBinary () const
 Raw response body.
 
bool hasError () const
 Whether the last run produced an error.
 
std::string getErrorString () const
 Human-readable description of the last error.
 

Private Member Functions

 NetworkGetRequest (const NetworkGetRequest &)=delete
 
NetworkGetRequestoperator= (const NetworkGetRequest &)=delete
 

Private Attributes

std::vector< char > response_bytes_
 
std::string url_
 
int timeout_ = 0
 
bool has_error_ = false
 
std::string error_string_
 

Detailed Description

Synchronous HTTP GET request backed by libcurl.

Typical lifecycle: set the URL via setUrl, optionally set a timeout via setTimeout, call run to perform the request, then read the result via getResponse / getResponseBinary or check hasError / getErrorString. The instance can be reused for further requests; each call to run replaces the previously observed state. Redirects are followed automatically.

Failures (libcurl initialisation, transport errors, HTTP status >= 400) are reported through hasError and getErrorString; run does not throw on those conditions.

Instances are not copyable.

Constructor & Destructor Documentation

◆ NetworkGetRequest() [1/2]

◆ ~NetworkGetRequest()

◆ NetworkGetRequest() [2/2]

NetworkGetRequest ( const NetworkGetRequest )
privatedelete

Member Function Documentation

◆ getErrorString()

std::string getErrorString ( ) const

Human-readable description of the last error.

Returns
Error message; empty when hasError is false.

◆ getResponse()

std::string getResponse ( ) const

Response body as a string.

Returns
A copy of getResponseBinary materialised as std::string.

◆ getResponseBinary()

const std::vector< char > & getResponseBinary ( ) const

Raw response body.

Returns
Reference to the byte buffer; valid until the next run call or destruction.

◆ hasError()

bool hasError ( ) const

Whether the last run produced an error.

Returns
true if the last run call did not produce a usable response (see run for the conditions).

◆ operator=()

NetworkGetRequest & operator= ( const NetworkGetRequest )
privatedelete

◆ run()

void run ( )

Execute the GET request synchronously.

Performs the request configured by the last setUrl / setTimeout call, blocking the caller until the response is complete or libcurl reports a timeout / error. Redirects are followed. The previous response and error state are cleared at the start of the call.

The call sets hasError to true when:

  • libcurl could not be initialised (error string "Failed to initialize libcurl"),
  • a transport-level libcurl error occurred (error string from curl_easy_strerror), or
  • the server responded with HTTP status >= 400 (error string "HTTP error N", where N is the status code).

No exception is thrown for any of these.

◆ setTimeout()

void setTimeout ( int  seconds)

Set the request timeout in seconds.

Parameters
[in]secondsTimeout in seconds. 0 (the default) leaves the timeout unset, so the request blocks until the server responds or libcurl gives up on its own.

◆ setUrl()

void setUrl ( const std::string &  url)

Set the URL to request.

The URL is consumed by the next run call; no validation is performed here.

Parameters
[in]urlURL to request on the next run call.

Member Data Documentation

◆ error_string_

std::string error_string_
private

◆ has_error_

bool has_error_ = false
private

◆ response_bytes_

std::vector<char> response_bytes_
private

◆ timeout_

int timeout_ = 0
private

◆ url_

std::string url_
private