mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-14 19:15:05 -05:00
add WebResponse::into
This commit is contained in:
parent
5969c905a8
commit
f909a737ec
2 changed files with 27 additions and 1 deletions
|
@ -67,6 +67,7 @@ namespace geode::utils::web {
|
|||
Result<std::string> string() const;
|
||||
Result<matjson::Value> json() const;
|
||||
ByteVector data() const;
|
||||
Result<> into(std::filesystem::path const& path) const;
|
||||
|
||||
std::vector<std::string> headers() const;
|
||||
std::optional<std::string> header(std::string_view name) const;
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
#include "Geode/utils/general.hpp"
|
||||
#include <Geode/utils/Result.hpp>
|
||||
#include <Geode/utils/general.hpp>
|
||||
#include <filesystem>
|
||||
#include <fmt/core.h>
|
||||
#include <fstream>
|
||||
#include <matjson.hpp>
|
||||
#include <system_error>
|
||||
#define CURL_STATICLIB
|
||||
#include <curl/curl.h>
|
||||
#include <ca_bundle.h>
|
||||
|
@ -65,8 +70,25 @@ public:
|
|||
int m_code;
|
||||
ByteVector m_data;
|
||||
std::unordered_map<std::string, std::string> m_headers;
|
||||
|
||||
Result<> into(std::filesystem::path const& path) const;
|
||||
};
|
||||
|
||||
Result<> WebResponse::Impl::into(std::filesystem::path const& path) const {
|
||||
// Test if there are no permission issues
|
||||
std::error_code ec;
|
||||
auto _ = std::filesystem::exists(path, ec);
|
||||
if (ec) {
|
||||
return Err(fmt::format("Couldn't write to file: {}", ec.category().message(ec.value())));
|
||||
}
|
||||
|
||||
auto stream = std::ofstream(path, std::ios::out | std::ios::binary);
|
||||
stream.write(reinterpret_cast<const char*>(m_data.data()), m_data.size());
|
||||
stream.close();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
WebResponse::WebResponse() : m_impl(std::make_shared<Impl>()) {}
|
||||
|
||||
bool WebResponse::ok() const {
|
||||
|
@ -91,6 +113,9 @@ Result<matjson::Value> WebResponse::json() const {
|
|||
ByteVector WebResponse::data() const {
|
||||
return m_impl->m_data;
|
||||
}
|
||||
Result<> WebResponse::into(std::filesystem::path const& path) const {
|
||||
return m_impl->into(path);
|
||||
}
|
||||
|
||||
std::vector<std::string> WebResponse::headers() const {
|
||||
return map::keys(m_impl->m_headers);
|
||||
|
|
Loading…
Reference in a new issue