aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-15 18:53:38 +0200
committerGitHub <[email protected]>2023-05-15 18:53:38 +0200
commit3fb79e25865c3bafa9156c2db767ddc16f5019f3 (patch)
treed476cf8c3b05793642b1a029171d76dfcf9f092d /src/zenhttp
parentadded trace::DescribeSession to TraceInit (diff)
downloadzen-3fb79e25865c3bafa9156c2db767ddc16f5019f3.tar.xz
zen-3fb79e25865c3bafa9156c2db767ddc16f5019f3.zip
Better defaults for zen cli (#302)
added ZenCmdBase::ResolveTargetHostSpec - this is a helper which provides better default behaviour for commands which interact with a local zen server instance. more specifically it picks a default based on which processes are actually running on the local machine this change also wires up the Scrub command along with some required HttpClient improvements
Diffstat (limited to 'src/zenhttp')
-rw-r--r--src/zenhttp/httpclient.cpp46
-rw-r--r--src/zenhttp/include/zenhttp/httpclient.h9
2 files changed, 53 insertions, 2 deletions
diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp
index 891ada83e..fa290ef52 100644
--- a/src/zenhttp/httpclient.cpp
+++ b/src/zenhttp/httpclient.cpp
@@ -83,6 +83,15 @@ CommonResponse(cpr::Response&& HttpResponse)
{
const HttpResponseCode WorkResponseCode = HttpResponseCode(HttpResponse.status_code);
+ if (HttpResponse.status_code == 0)
+ {
+ // Client side failure code
+
+ return HttpClient::Response{
+ .StatusCode = WorkResponseCode,
+ .ResponsePayload = IoBufferBuilder::MakeCloneFromMemory(HttpResponse.error.message.data(), HttpResponse.error.message.size())};
+ }
+
if (WorkResponseCode == HttpResponseCode::NoContent || HttpResponse.text.empty())
{
return HttpClient::Response{.StatusCode = WorkResponseCode};
@@ -310,6 +319,13 @@ HttpClient::Delete(std::string_view Url)
}
HttpClient::Response
+HttpClient::Post(std::string_view Url)
+{
+ Impl::Session Sess = m_Impl->AllocSession(m_BaseUri, Url);
+ return CommonResponse(Sess->Post());
+}
+
+HttpClient::Response
HttpClient::Post(std::string_view Url, const IoBuffer& Payload)
{
Impl::Session Sess = m_Impl->AllocSession(m_BaseUri, Url);
@@ -381,6 +397,36 @@ HttpClient::Response::AsText()
return {};
}
+std::string
+HttpClient::Response::ToText()
+{
+ if (!ResponsePayload)
+ return {};
+
+ switch (ResponsePayload.GetContentType())
+ {
+ case ZenContentType::kCbObject:
+ {
+ zen::ExtendableStringBuilder<1024> ObjStr;
+ zen::CbObject Object{SharedBuffer(ResponsePayload)};
+ zen::CompactBinaryToJson(Object, ObjStr);
+ return ObjStr.ToString();
+ }
+ break;
+
+ case ZenContentType::kCSS:
+ case ZenContentType::kHTML:
+ case ZenContentType::kJavaScript:
+ case ZenContentType::kJSON:
+ case ZenContentType::kText:
+ case ZenContentType::kYAML:
+ return std::string{AsText()};
+
+ default:
+ return "<unhandled content format>";
+ }
+}
+
bool
HttpClient::Response::IsSuccess() const noexcept
{
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h
index edf3bf773..9f08835d3 100644
--- a/src/zenhttp/include/zenhttp/httpclient.h
+++ b/src/zenhttp/include/zenhttp/httpclient.h
@@ -35,13 +35,18 @@ public:
// validate that the content type or content itself makes sense as a string.
std::string_view AsText();
- bool IsSuccess() const noexcept;
- inline explicit operator bool() const noexcept { return IsSuccess(); }
+ // Return text representation of the payload. Formats into JSON for structured
+ // 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(); }
};
[[nodiscard]] Response Put(std::string_view Url, const IoBuffer& Payload);
[[nodiscard]] Response Get(std::string_view Url);
[[nodiscard]] Response Delete(std::string_view Url);
+ [[nodiscard]] Response Post(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);