diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 680f167a2d..6a27cca475 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -20,24 +20,23 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace http { - -enum class HttpResult: uint8_t +enum class HttpResult : uint8_t { - // Response has been received successfully from target server. - HttpResult_OK = 0, + // Response has been received successfully from target server. + HttpResult_OK = 0, - // Request has been aborted by the caller. The server might or - // might not have already received or processed the request. - HttpResult_Aborted = 1, + // Request has been aborted by the caller. The server might or + // might not have already received or processed the request. + HttpResult_Aborted = 1, - // Local conditions have prevented the request from being sent - // (invalid request parameters, out of memory, internal error etc.) - HttpResult_LocalFailure = 2, + // Local conditions have prevented the request from being sent + // (invalid request parameters, out of memory, internal error etc.) + HttpResult_LocalFailure = 2, - // Network conditions somewhere between the local machine and - // the target server have caused the request to fail - // (connection failed, connection dropped abruptly etc.). - HttpResult_NetworkFailure = 3 + // Network conditions somewhere between the local machine and + // the target server have caused the request to fail + // (connection failed, connection dropped abruptly etc.). + HttpResult_NetworkFailure = 3 }; @@ -45,20 +44,20 @@ enum class HttpResult: uint8_t class HttpHeaders { public: - // Inserts a name/value pair, and removes elements with the same name. - virtual void set(nostd::string_view const& name, nostd::string_view const& value) = 0; + // Inserts a name/value pair, and removes elements with the same name. + virtual void set(nostd::string_view const &name, nostd::string_view const &value) = 0; - // Inserts a name/value pair - virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; + // Inserts a name/value pair + virtual void add(nostd::string_view const &name, nostd::string_view const &value) = 0; - // Gets a string value given a name - // Returns: - // If there are multiple headers with same name, it returns any one of the value (implementation defined) - // Empty string if there is no header with speicfied name.. - virtual nostd::string_view const& get(nostd::string_view const& name) const = 0; + // Gets a string value given a name + // Returns: + // If there are multiple headers with same name, it returns any one of the value + // (implementation defined) Empty string if there is no header with speicfied name.. + virtual nostd::string_view const &get(nostd::string_view const &name) const = 0; - // Tests whether the headers contain the specified name. - virtual bool has(nostd::string_view const& name) const = 0; + // Tests whether the headers contain the specified name. + virtual bool has(nostd::string_view const &name) const = 0; }; // The HttpRequest class represents a Request object. @@ -69,23 +68,23 @@ class HttpHeaders class HttpRequest { public: - // Gets the request ID. - virtual const nostd::string_view& GetId() const = 0; + // Gets the request ID. + virtual const nostd::string_view &GetId() const = 0; - // Sets the request Method - virtual void SetMethod(nostd::string_view const& method) = 0; + // Sets the request Method + virtual void SetMethod(nostd::string_view const &method) = 0; - // Gets the request URI - virtual void SetUrl(nostd::string_view const& url) = 0; + // Gets the request URI + virtual void SetUrl(nostd::string_view const &url) = 0; - // Gets the request Headers - virtual HttpHeaders* GetHeaders() const = 0; + // Gets the request Headers + virtual HttpHeaders *GetHeaders() const = 0; - // Sets the request body - virtual void SetBody(const uint8_t* const body, const size_t len) = 0; + // Sets the request body + virtual void SetBody(const uint8_t *const body, const size_t len) = 0; - // Gets the request body - virtual void GetBody(uint8_t* body, size_t& len) = 0; + // Gets the request body + virtual void GetBody(uint8_t *body, size_t &len) = 0; }; // The HttpResponse class represents a Response object. @@ -97,69 +96,66 @@ class HttpRequest class HttpResponse { public: - // Gets the response ID. - virtual const nostd::string_view& GetId() = 0; + // Gets the response ID. + virtual const nostd::string_view &GetId() = 0; - // Get the response result code. - virtual HttpResult GetResult() = 0; + // Get the response result code. + virtual HttpResult GetResult() = 0; - // Gets the response status code. - virtual unsigned GetStatusCode() = 0; + // Gets the response status code. + virtual unsigned GetStatusCode() = 0; - // Gets the response headers. - virtual HttpHeaders* GetHeaders() = 0; + // Gets the response headers. + virtual HttpHeaders *GetHeaders() = 0; - // Gets the response body. - virtual void GetBody(uint8_t* body, size_t& len) = 0; + // Gets the response body. + virtual void GetBody(uint8_t *body, size_t &len) = 0; }; // The HttpResponseCallback class receives HTTP client responses class HttpResponseCallback { public: - // Called when an HTTP request completes. - // The passed response object contains details about the exact way the - // request finished (HTTP status code, headers, content, error codes - // etc.). The ownership of the response object is transferred to the - // callback object. It can store it for later if necessary. Finally, it - // must be deleted using its virtual destructor. - virtual void OnHttpResponse(HttpResponse* response) = 0; + // Called when an HTTP request completes. + // The passed response object contains details about the exact way the + // request finished (HTTP status code, headers, content, error codes + // etc.). The ownership of the response object is transferred to the + // callback object. It can store it for later if necessary. Finally, it + // must be deleted using its virtual destructor. + virtual void OnHttpResponse(HttpResponse *response) = 0; }; // The HttpClient class is the interface for HTTP client implementations. class HttpClient { public: - - // Creates an empty HTTP request object. - // The created request object has only its ID prepopulated. Other fields - // must be set by the caller. The request object can then be sent - // using SendRequestAsync(). If you are not going to use the request object - // then you can delete it safely using its virtual destructor. - virtual HttpRequest* CreateRequest() = 0; - - // Begins an HTTP request. - // The method takes ownership of the passed request, and can destroy it before - // returning to the caller. Do not access the request object in any - // way after this invocation, and do not delete it. - // The callback object is always called, even if the request is - // cancelled, or if an error occurs immediately during sending. In the - // latter case, the OnHttpResponse() callback is called before this - // method returns. You must keep the callback object alive until its - // OnHttpResponse() callback is called. - virtual void SendRequestAsync(HttpRequest* request, HttpResponseCallback* callback) = 0; - - // Cancels an HTTP request. - // The caller must provide a string ID returned earlier by request->GetId(). - // The request is cancelled asynchronously. The caller must still - // wait for the relevant OnHttpResponse() callback (it can just come - // earlier with some "aborted" error status). - virtual void CancelRequestAsync(nostd::string_view const& id) = 0; - - virtual void CancelAllRequests() - { - } + // Creates an empty HTTP request object. + // The created request object has only its ID prepopulated. Other fields + // must be set by the caller. The request object can then be sent + // using SendRequestAsync(). If you are not going to use the request object + // then you can delete it safely using its virtual destructor. + virtual HttpRequest *CreateRequest() = 0; + + // Begins an HTTP request. + // The method takes ownership of the passed request, and can destroy it before + // returning to the caller. Do not access the request object in any + // way after this invocation, and do not delete it. + // The callback object is always called, even if the request is + // cancelled, or if an error occurs immediately during sending. In the + // latter case, the OnHttpResponse() callback is called before this + // method returns. You must keep the callback object alive until its + // OnHttpResponse() callback is called. + virtual void SendRequestAsync(HttpRequest *request, HttpResponseCallback *callback) = 0; + + // Cancels an HTTP request. + // The caller must provide a string ID returned earlier by request->GetId(). + // The request is cancelled asynchronously. The caller must still + // wait for the relevant OnHttpResponse() callback (it can just come + // earlier with some "aborted" error status). + virtual void CancelRequestAsync(nostd::string_view const &id) = 0; + + virtual void CancelAllRequests() {} }; -} // namespace nostd +} // namespace http OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index 282c373729..e0ef35c18e 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -27,142 +27,113 @@ namespace http namespace http_api = opentelemetry::http; // HttpHeaders implementation -class HttpHeaders: http_api::HttpHeaders, std::multimap +class HttpHeaders : http_api::HttpHeaders, std::multimap { public: - virtual void set(nostd::string_view const& name, nostd::string_view const& value) override - { - auto range = equal_range(std::string(name.data())); - auto hint = erase(range.first, range.second); - insert(hint, std::make_pair(name, value)); - } - - virtual void add(nostd::string_view const& name, nostd::string_view const& value) override - { - insert(std::make_pair(name, value)); - } - - virtual nostd::string_view const& get(nostd::string_view const& name) const override - { - auto it = find(std::string(name.data())); - return (it != end()) ? it->second : m_empty; - } - - virtual bool has(nostd::string_view const& name) const override - { - auto it = find(std::string(name.data())); - return (it != end()); - } + virtual void set(nostd::string_view const &name, nostd::string_view const &value) override + { + auto range = equal_range(std::string(name.data())); + auto hint = erase(range.first, range.second); + insert(hint, std::make_pair(name, value)); + } + + virtual void add(nostd::string_view const &name, nostd::string_view const &value) override + { + insert(std::make_pair(name, value)); + } + + virtual nostd::string_view const &get(nostd::string_view const &name) const override + { + auto it = find(std::string(name.data())); + return (it != end()) ? it->second : m_empty; + } + + virtual bool has(nostd::string_view const &name) const override + { + auto it = find(std::string(name.data())); + return (it != end()); + } private: - nostd::string_view m_empty{}; + nostd::string_view m_empty{}; }; -class SimpleHttpRequest: public http_api::HttpRequest +class SimpleHttpRequest : public http_api::HttpRequest { public: + SimpleHttpRequest(nostd::string_view const &id) : id_(id), method_("GET") {} - SimpleHttpRequest(nostd::string_view const& id): id_(id), method_("GET") - { - } - - // Gets the HTTP request ID. - virtual const nostd::string_view& GetId() const override - { - return id_; - } + // Gets the HTTP request ID. + virtual const nostd::string_view &GetId() const override { return id_; } - // Sets the request method - virtual void SetMethod(nostd::string_view const& method) override - { - method_ = method; - } + // Sets the request method + virtual void SetMethod(nostd::string_view const &method) override { method_ = method; } - // Sets the HTTP request URI. - virtual void SetUrl(nostd::string_view const& url) override - { - url_ = url; - } + // Sets the HTTP request URI. + virtual void SetUrl(nostd::string_view const &url) override { url_ = url; } - // Gets the HTTP request headers. - virtual http_api::HttpHeaders* GetHeaders() const override - { - return headers_; - } + // Gets the HTTP request headers. + virtual http_api::HttpHeaders *GetHeaders() const override { return headers_; } - // Sets the request body. - virtual void SetBody(const uint8_t* const body, const size_t len) override + // Sets the request body. + virtual void SetBody(const uint8_t *const body, const size_t len) override + { + for (int i = 0; i < len; i++) { - for (int i = 0; i < len; i++) - { - body_.push_back(body[i]); - } + body_.push_back(body[i]); } + } - virtual void GetBody(uint8_t* body, size_t& len) override + virtual void GetBody(uint8_t *body, size_t &len) override + { + len = 0; + for (auto &e : body_) { - len = 0; - for (auto &e: body_) - { - body[len++] = e; - } - len = (len > 0 ) ? len - 1 : len ; + body[len++] = e; } + len = (len > 0) ? len - 1 : len; + } private: - http_api::HttpHeaders* headers_; - std::vector body_; - nostd::string_view method_; - nostd::string_view id_; - nostd::string_view url_; - - + http_api::HttpHeaders *headers_; + std::vector body_; + nostd::string_view method_; + nostd::string_view id_; + nostd::string_view url_; }; -class SimpleHttpResponse: public http_api::HttpResponse +class SimpleHttpResponse : public http_api::HttpResponse { public: - SimpleHttpResponse(nostd::string_view const& id): - id_(id), - result_(http_api::HttpResult::HttpResult_LocalFailure), - statusCode_(0) - { - } + SimpleHttpResponse(nostd::string_view const &id) + : id_(id), result_(http_api::HttpResult::HttpResult_LocalFailure), statusCode_(0) + {} - virtual http_api::HttpResult GetResult() override - { - return result_; - } + virtual http_api::HttpResult GetResult() override { return result_; } - virtual unsigned GetStatusCode() override - { - return statusCode_; - } + virtual unsigned GetStatusCode() override { return statusCode_; } - virtual http_api::HttpHeaders* GetHeaders() override - { - return headers_; - } + virtual http_api::HttpHeaders *GetHeaders() override { return headers_; } - virtual void GetBody(uint8_t* body, size_t& len) override + virtual void GetBody(uint8_t *body, size_t &len) override + { + int i = 0; + for (auto &e : body_) { - int i = 0; - for (auto &e: body_) - { - body_[i++] = e; - } + body_[i++] = e; } + } private: - nostd::string_view id_; - unsigned statusCode_; - http_api::HttpHeaders* headers_; - http_api::HttpResult result_; - std::vector body_; + nostd::string_view id_; + unsigned statusCode_; + http_api::HttpHeaders *headers_; + http_api::HttpResult result_; + std::vector body_; }; -} //http -} //sdk +} // namespace http +} // namespace sdk OPENTELEMETRY_END_NAMESPACE