diff options
| author | Stefan Boberg <[email protected]> | 2023-05-05 11:09:45 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-05 11:09:45 +0200 |
| commit | 5fee41301ff4488503e64f8d79c1a07508dd27be (patch) | |
| tree | 0fffdaa8993c78b518118dfa2a65ea43e90487f3 /src/zenhttp/include | |
| parent | Update README.md (diff) | |
| download | zen-5fee41301ff4488503e64f8d79c1a07508dd27be.tar.xz zen-5fee41301ff4488503e64f8d79c1a07508dd27be.zip | |
247 complete httpclient implementation (#269)
* implemented HttpClient connection pooling
* implemented missing verbs
* added response helpers (CbObject/CbPackage/text)
* added RwLock::WithSharedLock and RwLock::WithExclusiveLock
* added some noexcept annotations on RwLock
* removed CPR dependency in httpclient.h
Diffstat (limited to 'src/zenhttp/include')
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclient.h | 36 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpcommon.h | 18 |
2 files changed, 38 insertions, 16 deletions
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h index 8316a9b9f..8ec29d548 100644 --- a/src/zenhttp/include/zenhttp/httpclient.h +++ b/src/zenhttp/include/zenhttp/httpclient.h @@ -8,10 +8,6 @@ #include <zencore/uid.h> #include <zenhttp/httpcommon.h> -ZEN_THIRD_PARTY_INCLUDES_START -#include <cpr/cpr.h> -ZEN_THIRD_PARTY_INCLUDES_END - namespace zen { class CbPackage; @@ -20,6 +16,7 @@ class CbPackage; Currently simple and synchronous, should become lean and asynchronous */ + class HttpClient { public: @@ -28,20 +25,39 @@ public: struct Response { - int StatusCode = 0; - IoBuffer ResponsePayload; // Note: this also includes the content type + HttpResponseCode StatusCode = HttpResponseCode::ImATeapot; + IoBuffer ResponsePayload; // Note: this also includes the content type + + CbObject AsObject(); + CbPackage AsPackage(); + + // Return the response payload as a string. Note that this does not attempt to + // validate that the content type or content itself makes sense as a string. + std::string_view AsText(); + + bool IsSuccess() const noexcept; + inline operator bool() const noexcept { return IsSuccess(); } }; - [[nodiscard]] Response Put(std::string_view Url, IoBuffer Payload); + [[nodiscard]] Response Put(std::string_view Url, const IoBuffer& Payload); [[nodiscard]] Response Get(std::string_view Url); - [[nodiscard]] Response TransactPackage(std::string_view Url, CbPackage Package); [[nodiscard]] Response Delete(std::string_view Url); + [[nodiscard]] Response Post(std::string_view Url, const IoBuffer& Payload); + [[nodiscard]] Response Post(std::string_view Url, CbObject Payload); + [[nodiscard]] Response Post(std::string_view Url, CbPackage Payload); + + [[nodiscard]] Response TransactPackage(std::string_view Url, CbPackage Package); + + inline std::string GetBaseUri() const { return m_BaseUri; } private: + struct Impl; + std::string m_BaseUri; std::string m_SessionId; + Ref<Impl> m_Impl; }; -} // namespace zen - void httpclient_forcelink(); // internal + +} // namespace zen diff --git a/src/zenhttp/include/zenhttp/httpcommon.h b/src/zenhttp/include/zenhttp/httpcommon.h index 19fda8db4..fb0d128a2 100644 --- a/src/zenhttp/include/zenhttp/httpcommon.h +++ b/src/zenhttp/include/zenhttp/httpcommon.h @@ -30,12 +30,6 @@ extern HttpContentType (*ParseContentType)(const std::string_view& ContentTypeSt std::string_view ReasonStringForHttpResultCode(int HttpCode); bool TryParseHttpRangeHeader(std::string_view RangeHeader, HttpRanges& Ranges); -[[nodiscard]] inline bool -IsHttpSuccessCode(int HttpCode) -{ - return (HttpCode >= 200) && (HttpCode < 300); -} - enum class HttpVerb : uint8_t { kGet = 1 << 0, @@ -178,4 +172,16 @@ enum class HttpResponseCode NetworkAuthenticationRequired = 511, //!< Indicates that the client needs to authenticate to gain network access. }; +[[nodiscard]] inline bool +IsHttpSuccessCode(int HttpCode) noexcept +{ + return (HttpCode >= 200) && (HttpCode < 300); +} + +[[nodiscard]] inline bool +IsHttpSuccessCode(HttpResponseCode HttpCode) +{ + return IsHttpSuccessCode(int(HttpCode)); +} + } // namespace zen |