#pragma once #include #include namespace hex { template std::future> HttpRequest::downloadFile(const std::fs::path &path) { return std::async(std::launch::async, [this, path] { std::vector response; // Execute the request auto result = this->executeImpl(response); // Write the result to the file wolv::io::File file(path, wolv::io::File::Mode::Create); file.writeBuffer(reinterpret_cast(result.getData().data()), result.getData().size()); return result; }); } template std::future> HttpRequest::uploadFile(const std::fs::path &path, const std::string &mimeName) { hex::unused(path, mimeName); throw std::logic_error("Not implemented"); } template std::future> HttpRequest::uploadFile(std::vector data, const std::string &mimeName, const std::fs::path &fileName) { hex::unused(data, mimeName, fileName); throw std::logic_error("Not implemented"); } template std::future> HttpRequest::execute() { return std::async(std::launch::async, [this] { std::vector responseData; return this->executeImpl(responseData); }); } template HttpRequest::Result HttpRequest::executeImpl(std::vector &data) { strcpy(m_attr.requestMethod, m_method.c_str()); m_attr.attributes = EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; if (!m_body.empty()) { m_attr.requestData = m_body.c_str(); m_attr.requestDataSize = m_body.size(); } std::vector headers; for (auto it = m_headers.begin(); it != m_headers.end(); it++) { headers.push_back(it->first.c_str()); headers.push_back(it->second.c_str()); } headers.push_back(nullptr); m_attr.requestHeaders = headers.data(); // Send request emscripten_fetch_t* fetch = emscripten_fetch(&m_attr, m_url.c_str()); data.resize(fetch->numBytes); std::copy(fetch->data, fetch->data + fetch->numBytes, data.begin()); return Result(fetch->status, { data.begin(), data.end() }); } }