-
Notifications
You must be signed in to change notification settings - Fork 558
Providing an abstract HTTP interface. #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f8d5ee4
ec891f1
89c9659
2d3991a
a1b0586
b8a6222
d382644
e3baf15
c20ba79
35df55a
4c427db
9f16d11
cb3ccb5
f8b7af5
74558ca
ae0e5e1
6c13a33
11c0012
c03fef1
71e6625
6b4076e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| #pragma once | ||
|
|
||
| #include <chrono> | ||
| #include <string> | ||
| #include <vector> | ||
| #include "opentelemetry/nostd/function_ref.h" | ||
| #include "opentelemetry/nostd/string_view.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| /* | ||
| Usage Example | ||
|
|
||
| struct SimpleReponseHandler: public ResponseHandler { | ||
| void OnResponse(Response& res) noexcept override | ||
| { | ||
| if (res.IsSuccess()) { | ||
| res.GetNextHeader([](nostd::string_view name, std::string value) -> bool { | ||
| std::cout << "Header Name:" << name << " Header Value:"<< value ; | ||
| return true; | ||
| }); | ||
| .. process response body | ||
| } | ||
| } | ||
|
|
||
| void OnError(nostd::string_view err) noexcept override | ||
| { | ||
| std::cout << " Error:" << err; | ||
| } | ||
| }; | ||
|
|
||
| SessionManager sessionManager; // implementer can provide singleton implementation for it | ||
| auto session = sessionManager.createSession("localhost", 8000); | ||
| auto request = session->CreateRequest(); | ||
| request->AddHeader(..); | ||
| SimpleResponseHandler res_handler; | ||
| session->SendRequest(res_handler); | ||
| session->FinishSession() // optionally in the end | ||
| ...shutdown | ||
| sessionManager.FinishAllSessions() | ||
| */ | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace common | ||
| { | ||
| namespace http | ||
| { | ||
|
|
||
| enum class Method | ||
| { | ||
| Get, | ||
| Post, | ||
| Put, | ||
| Options, | ||
| Head, | ||
| Patch, | ||
| Delete | ||
| }; | ||
|
|
||
| enum class SessionState | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are plenty of errors, e.g., libcurl https://curl.haxx.se/libcurl/c/libcurl-errors.html - some are specific to libcurl, others are specific to non HTTP/HTTPS protocols, but many would be found in any HTTP library
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. http_client implementer for libcurl, winInet, winhttp and others need to ensure to map all those library-specific errors to http_client error states. |
||
| { | ||
| Created, // session object is created | ||
| Ongoing, // session is ongoing | ||
| Finished, // session is finished ( this needs to be the final state ) | ||
| Queued, // http request is queued | ||
| TimedOut, // Request timedout, no response received | ||
| Aborted, // http request aborted due to local error, | ||
| Cancelled, // http request cancelled, possibly due to session->CancelSession(); | ||
| SendingFailed, // http request sending failed | ||
| NetworkError, // network error | ||
| SSLHandshakeFailed, // ssl handshake failed | ||
| ReadError, // error while reading response | ||
| WriteError // error while writing rquest | ||
| }; | ||
|
|
||
| using Byte = uint8_t; | ||
| using StatusCode = uint16_t; | ||
| using Body = std::vector<Byte>; | ||
| using SSLCertificate = std::vector<Byte>; | ||
|
|
||
| class Request | ||
| { | ||
|
lalitb marked this conversation as resolved.
|
||
| public: | ||
| virtual void SetMethod(Method method) noexcept = 0; | ||
|
|
||
| virtual void SetUri(nostd::string_view uri) noexcept = 0; | ||
|
|
||
| virtual void SetBody(Body &body) noexcept = 0; | ||
|
|
||
| virtual void AddHeader(nostd::string_view name, nostd::string_view value) noexcept = 0; | ||
|
|
||
| virtual void ReplaceHeader(nostd::string_view name, nostd::string_view value) noexcept = 0; | ||
|
|
||
| virtual void SetTimeoutMs(std::chrono::milliseconds timeout_ms) noexcept = 0; | ||
|
|
||
| virtual ~Request() = default; | ||
| }; | ||
|
|
||
| class Response | ||
| { | ||
| public: | ||
| virtual const Body &GetBody() const noexcept = 0; | ||
|
|
||
| virtual bool ForEachHeader( | ||
| nostd::function_ref<bool(nostd::string_view name, std::string value)> callable) const | ||
| noexcept = 0; | ||
|
|
||
| virtual bool ForEachHeader( | ||
| const nostd::string_view &key, | ||
| nostd::function_ref<bool(nostd::string_view name, std::string value)> callable) const | ||
| noexcept = 0; | ||
|
|
||
| virtual StatusCode GetStatusCode() const noexcept = 0; | ||
|
|
||
| virtual ~Response() = default; | ||
| }; | ||
|
|
||
| class EventHandler | ||
| { | ||
| public: | ||
| virtual void OnResponse(Response &) noexcept = 0; | ||
|
lalitb marked this conversation as resolved.
|
||
|
|
||
| virtual void OnError(SessionState, nostd::string_view) noexcept = 0; | ||
|
|
||
| virtual void OnConnecting(const SSLCertificate &) noexcept {} | ||
|
|
||
| virtual ~EventHandler() = default; | ||
| }; | ||
|
|
||
| class Session | ||
| { | ||
| public: | ||
| virtual std::shared_ptr<Request> CreateRequest() noexcept = 0; | ||
|
|
||
| virtual void SendRequest(EventHandler &) noexcept = 0; | ||
|
|
||
| virtual bool IsSessionActive() noexcept = 0; | ||
|
|
||
| virtual bool CancelSession() noexcept = 0; | ||
|
|
||
| virtual bool FinishSession() noexcept = 0; | ||
|
|
||
| virtual ~Session() = default; | ||
| }; | ||
|
|
||
| class SessionManager | ||
| { | ||
| public: | ||
| virtual std::shared_ptr<Session> CreateSession(nostd::string_view host, | ||
| uint16_t port = 80) noexcept = 0; | ||
|
|
||
| virtual bool CancelAllSessions() noexcept = 0; | ||
|
|
||
| virtual bool FinishAllSessions() noexcept = 0; | ||
|
|
||
| virtual ~SessionManager() = default; | ||
| }; | ||
|
|
||
| } // namespace http | ||
| } // namespace common | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not
std:cerr?