Line data Source code
1 : #pragma once 2 : 3 : // This component provides an interface, `HTTPClient`, that represents an 4 : // asynchronous HTTP client. 5 : // 6 : // `HTTPClient` is used by `DatadogAgent` to send traces to the Datadog Agent. 7 : // 8 : // If this library was built with support for libcurl, then `Curl` implements 9 : // `HTTPClient` in terms of libcurl. See `curl.h`. 10 : 11 : #include <chrono> 12 : #include <functional> 13 : 14 : #include "error.h" 15 : #include "expected.h" 16 : #include "json_fwd.hpp" 17 : #include "optional.h" 18 : 19 : namespace datadog { 20 : namespace tracing { 21 : 22 : class DictReader; 23 : class DictWriter; 24 : 25 : class HTTPClient { 26 : public: 27 : struct URL { 28 : std::string scheme; // http, https, or unix 29 : std::string authority; // domain:port or /path/to/socket 30 : std::string path; // resource, e.g. /v0.4/traces 31 : }; 32 : 33 : using HeadersSetter = std::function<void(DictWriter& headers)>; 34 : using ResponseHandler = std::function<void( 35 : int status, const DictReader& headers, std::string body)>; 36 : // `ErrorHandler` is for errors encountered by `HTTPClient`, not for 37 : // error-indicating HTTP responses. 38 : using ErrorHandler = std::function<void(Error)>; 39 : 40 : // Send a POST request to the specified `url`. Set request headers by calling 41 : // the specified `set_headers` callback. Include the specified `body` at the 42 : // end of the request. Invoke the specified `on_response` callback if/when 43 : // a response is delivered (even if that response contains an error HTTP 44 : // response status). Invoke the specified `on_error` if an error occurs 45 : // outside of HTTP, such as a connection failure. If an error occurs while 46 : // preparing the request, return an `Error`. The behavior is undefined if 47 : // either of `on_response` or `on_error` throws an exception. 48 : virtual Expected<void> post( 49 : const URL& url, HeadersSetter set_headers, std::string body, 50 : ResponseHandler on_response, ErrorHandler on_error, 51 : std::chrono::steady_clock::time_point deadline) = 0; 52 : 53 : // Wait until there are no more outstanding requests, or until the specified 54 : // `deadline`. 55 : virtual void drain(std::chrono::steady_clock::time_point deadline) = 0; 56 : 57 : // Return a JSON representation of this object's configuration. The JSON 58 : // representation is an object with the following properties: 59 : // 60 : // - "type" is the unmangled, qualified name of the most-derived class, e.g. 61 : // "datadog::tracing::Curl". 62 : // - "config" is an object containing this object's configuration. "config" 63 : // may be omitted if the derived class has no configuration. 64 : virtual nlohmann::json config_json() const = 0; 65 : 66 639 : virtual ~HTTPClient() = default; 67 : }; 68 : 69 : } // namespace tracing 70 : } // namespace datadog