diff options
| author | Stefan Boberg <[email protected]> | 2023-05-15 20:41:57 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2023-05-15 20:41:57 +0200 |
| commit | c0eabf9ce24274336d402737b2f8ea3d6772d33b (patch) | |
| tree | 853dcee9fa1d4542fb5d18b17638a1cf0395024f /src | |
| parent | added some top-level trace scopes to httpsys impl (diff) | |
| download | zen-c0eabf9ce24274336d402737b2f8ea3d6772d33b.tar.xz zen-c0eabf9ce24274336d402737b2f8ea3d6772d33b.zip | |
some HttpClient changes eliminating some cpr helpers
Diffstat (limited to 'src')
| -rw-r--r-- | src/zenhttp/httpclient.cpp | 24 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclient.h | 30 |
2 files changed, 33 insertions, 21 deletions
diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp index fa290ef52..761b4b08e 100644 --- a/src/zenhttp/httpclient.cpp +++ b/src/zenhttp/httpclient.cpp @@ -23,11 +23,9 @@ namespace zen { using namespace std::literals; -HttpClient::Response -FromCprResponse(cpr::Response& InResponse) -{ - return {.StatusCode = HttpResponseCode(InResponse.status_code)}; -} +////////////////////////////////////////////////////////////////////////// +// +// CPR helpers cpr::Body AsCprBody(const CbObject& Obj) @@ -51,14 +49,6 @@ AsCprBody(const CompositeBuffer& Buffers) return cpr::Body{std::move(String)}; } -CbObject -AsCbObject(cpr::Response& Response) -{ - const std::string& Data = Response.text; - - return LoadCompactBinaryObject(IoBufferBuilder::MakeCloneFromMemory(Data.data(), Data.size())); -} - ////////////////////////////////////////////////////////////////////////// HttpClient::Response @@ -213,9 +203,7 @@ HttpClient::TransactPackage(std::string_view Url, CbPackage Package) for (const CbAttachment& Attachment : Attachments) { - IoHash Hash = Attachment.GetHash(); - - Writer.AddHash(Hash); + Writer.AddHash(Attachment.GetHash()); } Writer.EndArray(); @@ -233,7 +221,7 @@ HttpClient::TransactPackage(std::string_view Url, CbPackage Package) IoBuffer ResponseBuffer(IoBuffer::Wrap, FilterResponse.text.data(), FilterResponse.text.size()); CbObject ResponseObject = LoadCompactBinaryObject(ResponseBuffer); - for (auto& Entry : ResponseObject["need"]) + for (CbFieldView& Entry : ResponseObject["need"]) { ZEN_ASSERT(Entry.IsHash()); AttachmentsToSend.push_back(Entry.AsHash()); @@ -272,7 +260,7 @@ HttpClient::TransactPackage(std::string_view Url, CbPackage Package) if (!IsHttpSuccessCode(FilterResponse.status_code)) { - return FromCprResponse(FilterResponse); + return {.StatusCode = HttpResponseCode(FilterResponse.status_code)}; } IoBuffer ResponseBuffer(IoBuffer::Clone, FilterResponse.text.data(), FilterResponse.text.size()); diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h index 9f08835d3..559d7e719 100644 --- a/src/zenhttp/include/zenhttp/httpclient.h +++ b/src/zenhttp/include/zenhttp/httpclient.h @@ -8,6 +8,8 @@ #include <zencore/uid.h> #include <zenhttp/httpcommon.h> +#include <optional> + namespace zen { class CbPackage; @@ -15,6 +17,11 @@ class CbPackage; /** HTTP client implementation for Zen use cases Currently simple and synchronous, should become lean and asynchronous + + This does not attempt to provide an interface which can be used to interface + with arbitrary services, it's primarily intended for interfacing with UE + development ecosystem HTTP services like Zen store or Jupiter + */ class HttpClient @@ -23,12 +30,27 @@ public: HttpClient(std::string_view BaseUri); ~HttpClient(); + struct ErrorContext + { + std::string ErrorMessage; + }; + struct Response { HttpResponseCode StatusCode = HttpResponseCode::ImATeapot; IoBuffer ResponsePayload; // Note: this also includes the content type - CbObject AsObject(); + // This contains any errors from the HTTP stack. It won't contain information on + // why the server responded with a non-success HTTP status, that may be gleaned + // from the response payload itself depending on what the server provides. + std::optional<ErrorContext> Error; + + // Return the response payload as a CbObject. Note that this does not attempt to + // validate that the content type or content itself makes sense as a CbObject + CbObject AsObject(); + + // Return the response payload as a CbPackage. Note that this does not attempt to + // validate that the content type or content itself makes sense as a CbPackage CbPackage AsPackage(); // Return the response payload as a string. Note that this does not attempt to @@ -39,8 +61,10 @@ public: // objects, returns text as-is for text types like Text, JSON, HTML etc std::string ToText(); - bool IsSuccess() const noexcept; - inline operator bool() const noexcept { return IsSuccess(); } + // Returns whether the HTTP status code is considered successful (i.e in the + // 2xx range) + bool IsSuccess() const noexcept; + inline explicit operator bool() const noexcept { return IsSuccess(); } }; [[nodiscard]] Response Put(std::string_view Url, const IoBuffer& Payload); |