2022-10-05 08:41:05 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../DefaultInclude.hpp"
|
2023-02-08 10:25:07 -05:00
|
|
|
#include "MiniFunction.hpp"
|
2023-12-30 12:42:53 -05:00
|
|
|
#include <matjson.hpp>
|
2022-12-04 11:39:40 -05:00
|
|
|
#include "Result.hpp"
|
2022-11-28 12:09:39 -05:00
|
|
|
#include "general.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2024-06-02 21:35:05 -04:00
|
|
|
#include <filesystem>
|
2024-01-03 21:12:23 -05:00
|
|
|
#include <chrono>
|
2022-10-05 08:41:05 -04:00
|
|
|
|
|
|
|
namespace geode::utils::web {
|
2022-11-28 12:09:39 -05:00
|
|
|
GEODE_DLL void openLinkInBrowser(std::string const& url);
|
2022-12-04 11:39:40 -05:00
|
|
|
|
2023-02-08 10:25:07 -05:00
|
|
|
using FileProgressCallback = utils::MiniFunction<bool(double, double)>;
|
2022-10-05 08:41:05 -04:00
|
|
|
|
2022-10-12 17:22:43 -04:00
|
|
|
/**
|
|
|
|
* Synchronously fetch data from the internet
|
|
|
|
* @param url URL to fetch
|
|
|
|
* @returns Returned data as bytes, or error on error
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
[[deprecated("Use the WebRequest class from the web2.hpp header instead")]]
|
2022-12-14 06:11:19 -05:00
|
|
|
GEODE_DLL Result<ByteVector> fetchBytes(std::string const& url);
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-05 08:41:05 -04:00
|
|
|
/**
|
|
|
|
* Synchronously fetch data from the internet
|
|
|
|
* @param url URL to fetch
|
|
|
|
* @returns Returned data as string, or error on error
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
[[deprecated("Use the WebRequest class from the web2.hpp header instead")]]
|
2022-10-05 08:41:05 -04:00
|
|
|
GEODE_DLL Result<std::string> fetch(std::string const& url);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Syncronously download a file from the internet
|
|
|
|
* @param url URL to fetch
|
|
|
|
* @param into Path to download file into
|
2022-10-30 14:59:20 -04:00
|
|
|
* @param prog Progress function; first parameter is bytes downloaded so
|
|
|
|
* far, and second is total bytes to download. Return true to continue
|
|
|
|
* downloading, and false to interrupt. Note that interrupting does not
|
2022-10-05 08:41:05 -04:00
|
|
|
* automatically remove the file that was being downloaded
|
|
|
|
* @returns Returned data as JSON, or error on error
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
[[deprecated("Use the WebRequest class from the web2.hpp header instead")]]
|
2022-10-05 08:41:05 -04:00
|
|
|
GEODE_DLL Result<> fetchFile(
|
2024-06-02 21:35:05 -04:00
|
|
|
std::string const& url, std::filesystem::path const& into, FileProgressCallback prog = nullptr
|
2022-10-05 08:41:05 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously fetch data from the internet and parse it as JSON
|
|
|
|
* @param url URL to fetch
|
|
|
|
* @returns Returned data as JSON, or error on error
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
[[deprecated("Use the WebRequest class from the web2.hpp header instead")]]
|
2024-02-11 22:04:56 -05:00
|
|
|
GEODE_DLL Result<matjson::Value> fetchJSON(std::string const& url);
|
2022-10-12 17:22:43 -04:00
|
|
|
|
|
|
|
class SentAsyncWebRequest;
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-10-12 17:22:43 -04:00
|
|
|
class AsyncWebResult;
|
|
|
|
class AsyncWebResponse;
|
|
|
|
class AsyncWebRequest;
|
|
|
|
|
2023-02-08 10:25:07 -05:00
|
|
|
using AsyncProgress = utils::MiniFunction<void(SentAsyncWebRequest&, double, double)>;
|
|
|
|
using AsyncExpect = utils::MiniFunction<void(std::string const&)>;
|
|
|
|
using AsyncExpectCode = utils::MiniFunction<void(std::string const&, int)>;
|
|
|
|
using AsyncThen = utils::MiniFunction<void(SentAsyncWebRequest&, ByteVector const&)>;
|
|
|
|
using AsyncCancelled = utils::MiniFunction<void(SentAsyncWebRequest&)>;
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* A handle to an in-progress sent asynchronous web request. Use this to
|
2022-10-13 15:30:57 -04:00
|
|
|
* cancel the request / query information about it
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
class GEODE_DLL [[deprecated("Use the WebRequest class from the web2.hpp header instead")]] SentAsyncWebRequest {
|
2022-10-12 17:22:43 -04:00
|
|
|
private:
|
2022-12-12 07:40:05 -05:00
|
|
|
class Impl;
|
|
|
|
std::shared_ptr<Impl> m_impl;
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-10-12 17:22:43 -04:00
|
|
|
friend class AsyncWebResult;
|
|
|
|
friend class AsyncWebRequest;
|
|
|
|
|
|
|
|
void pause();
|
|
|
|
void resume();
|
2023-01-24 11:17:03 -05:00
|
|
|
void error(std::string const& error, int code);
|
2022-10-12 17:22:43 -04:00
|
|
|
void doCancel();
|
|
|
|
|
|
|
|
public:
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-12-12 07:40:05 -05:00
|
|
|
* Do not call these manually.
|
2022-10-13 15:30:57 -04:00
|
|
|
*/
|
2022-12-12 07:40:05 -05:00
|
|
|
SentAsyncWebRequest();
|
|
|
|
~SentAsyncWebRequest();
|
|
|
|
static std::shared_ptr<SentAsyncWebRequest> create(AsyncWebRequest const&, std::string const& id);
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Cancel the request. Cleans up any downloaded files, but if you run
|
|
|
|
* extra code in `then`, you will have to clean it up manually in
|
2022-10-13 15:30:57 -04:00
|
|
|
* `cancelled`
|
|
|
|
*/
|
2022-10-12 17:22:43 -04:00
|
|
|
void cancel();
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
|
|
|
* Check if the request is finished
|
|
|
|
*/
|
2022-10-12 17:22:43 -04:00
|
|
|
bool finished() const;
|
2024-01-24 11:31:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the response header of the request. Only valid after the request
|
|
|
|
*/
|
|
|
|
std::string getResponseHeader(std::string_view header) const;
|
2022-10-12 17:22:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
using SentAsyncWebRequestHandle = std::shared_ptr<SentAsyncWebRequest>;
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-12-14 06:11:19 -05:00
|
|
|
using DataConverter = Result<T> (*)(ByteVector const&);
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* An asynchronous, thread-safe web request. Downloads data from the
|
|
|
|
* internet without slowing the main thread. All callbacks are run in the
|
2022-10-13 15:30:57 -04:00
|
|
|
* GD thread, so interacting with the Cocos2d UI is perfectly safe
|
|
|
|
*/
|
2024-02-26 11:26:34 -05:00
|
|
|
class GEODE_DLL [[deprecated("Use the WebRequest class from the web2.hpp header instead")]] AsyncWebRequest {
|
2022-10-12 21:50:41 -04:00
|
|
|
private:
|
2024-01-03 20:17:27 -05:00
|
|
|
class Impl;
|
|
|
|
std::unique_ptr<Impl> m_impl;
|
2023-09-08 10:17:45 -04:00
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-10-12 21:50:41 -04:00
|
|
|
friend class AsyncWebResult;
|
|
|
|
friend class SentAsyncWebRequest;
|
|
|
|
friend class AsyncWebResponse;
|
2024-01-03 20:17:27 -05:00
|
|
|
friend class SentAsyncWebRequest::Impl;
|
2022-10-12 21:50:41 -04:00
|
|
|
|
2024-01-03 20:17:27 -05:00
|
|
|
AsyncWebRequest& setThen(AsyncThen);
|
2022-10-12 21:50:41 -04:00
|
|
|
public:
|
2022-10-14 14:04:59 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* An asynchronous, thread-safe web request. Downloads data from the
|
|
|
|
* internet without slowing the main thread. All callbacks are run in the
|
2022-10-14 14:04:59 -04:00
|
|
|
* GD thread, so interacting with the Cocos2d UI is perfectly safe
|
|
|
|
*/
|
2024-01-03 20:17:27 -05:00
|
|
|
AsyncWebRequest();
|
|
|
|
~AsyncWebRequest();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL to fetch from the internet asynchronously
|
|
|
|
* @param url URL of the data to download. Redirects will be
|
|
|
|
* automatically followed
|
|
|
|
* @returns An AsyncWebResponse object
|
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebResponse fetch(std::string_view const url);
|
2024-01-03 20:17:27 -05:00
|
|
|
/**
|
|
|
|
* Begin the web request. It's not always necessary to call this as the
|
|
|
|
* destructor calls it automatically, but if you need access to the
|
|
|
|
* handle of the sent request, use this
|
|
|
|
* @returns Handle to the sent web request
|
|
|
|
*/
|
|
|
|
SentAsyncWebRequestHandle send();
|
2022-10-14 14:04:59 -04:00
|
|
|
|
2024-01-09 18:10:58 -05:00
|
|
|
/**
|
|
|
|
* Shorthand for a GET request to the given url.
|
|
|
|
* Same return value as fetch.
|
|
|
|
*/
|
|
|
|
AsyncWebResponse get(std::string_view const url);
|
|
|
|
/**
|
|
|
|
* Shorthand for a POST request to the given url.
|
|
|
|
* Same return value as fetch.
|
|
|
|
*/
|
|
|
|
AsyncWebResponse post(std::string_view const url);
|
|
|
|
/**
|
|
|
|
* Shorthand for a PUT request to the given url.
|
|
|
|
* Same return value as fetch.
|
|
|
|
*/
|
|
|
|
AsyncWebResponse put(std::string_view const url);
|
|
|
|
/**
|
|
|
|
* Shorthand for a PATCH request to the given url.
|
|
|
|
* Same return value as fetch.
|
|
|
|
*/
|
|
|
|
AsyncWebResponse patch(std::string_view const url);
|
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* If you only want one instance of this web request to run (for example,
|
|
|
|
* you're downloading some global data for a manager), then use this
|
|
|
|
* to specify a Join ID. If another request with the same ID is
|
|
|
|
* already running, this request's callbacks will be appended to the
|
2022-10-13 15:30:57 -04:00
|
|
|
* existing one instead of creating a new request
|
2022-10-30 14:59:20 -04:00
|
|
|
* @param requestID The Join ID of the request. Can be anything,
|
2022-10-13 15:30:57 -04:00
|
|
|
* recommended to be something unique
|
|
|
|
* @returns Same AsyncWebRequest
|
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& join(std::string_view const requestID);
|
|
|
|
|
|
|
|
// Request parameters
|
2022-11-03 16:46:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* In order to specify a http header to the request, give it here.
|
|
|
|
* Can be called more than once.
|
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& header(std::string_view const header);
|
|
|
|
/**
|
|
|
|
* In order to specify a http header to the request, give it here.
|
|
|
|
* Can be called more than once.
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& header(std::string_view const headerName, std::string_view const headerValue);
|
2023-09-08 10:17:45 -04:00
|
|
|
/**
|
|
|
|
* In order to specify an user agent to the request, give it here.
|
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& userAgent(std::string_view const userAgent);
|
|
|
|
/**
|
|
|
|
* Sets the Content-Type header to the specified value.
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& contentType(std::string_view const contentType);
|
2023-09-08 10:17:45 -04:00
|
|
|
/**
|
|
|
|
* Specify that the request is a POST request.
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& postRequest();
|
|
|
|
/**
|
2024-01-09 18:10:58 -05:00
|
|
|
* Specify the HTTP method for the request, like PUT and DELETE.
|
2023-09-08 10:17:45 -04:00
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& method(std::string_view const method);
|
2023-09-08 10:17:45 -04:00
|
|
|
/**
|
2024-01-09 18:10:58 -05:00
|
|
|
* Specify the raw request body to send with the request.
|
|
|
|
* Content type is unchanged.
|
2023-09-08 10:17:45 -04:00
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& bodyRaw(std::string_view const content);
|
2023-09-08 10:17:45 -04:00
|
|
|
/**
|
2024-01-09 18:10:58 -05:00
|
|
|
* Specify a json request body. Additionally sets the content type to
|
|
|
|
* application/json.
|
2023-09-08 10:17:45 -04:00
|
|
|
*/
|
2024-01-09 18:10:58 -05:00
|
|
|
AsyncWebRequest& body(matjson::Value const& value);
|
2024-01-03 21:12:23 -05:00
|
|
|
/**
|
|
|
|
* Specify a timeout, in seconds, in which the request will fail.
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& timeout(std::chrono::seconds seconds);
|
2024-01-09 18:10:58 -05:00
|
|
|
|
|
|
|
// Callbacks
|
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2023-01-24 11:17:03 -05:00
|
|
|
* Specify a callback to run if the download fails. The callback is
|
|
|
|
* always ran in the GD thread, so interacting with UI is safe
|
2022-10-13 15:30:57 -04:00
|
|
|
* @param handler Callback to run if the download fails
|
|
|
|
* @returns Same AsyncWebRequest
|
|
|
|
*/
|
2022-10-12 21:50:41 -04:00
|
|
|
AsyncWebRequest& expect(AsyncExpect handler);
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2023-01-24 11:17:03 -05:00
|
|
|
* Specify a callback to run if the download fails. The callback is
|
|
|
|
* always ran in the GD thread, so interacting with UI is safe
|
|
|
|
* @param handler Callback to run if the download fails
|
|
|
|
* @returns Same AsyncWebRequest
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& expect(AsyncExpectCode handler);
|
|
|
|
/**
|
|
|
|
* Specify a callback to run when the download progresses. The callback is
|
|
|
|
* always ran in the GD thread, so interacting with UI is safe
|
2022-10-13 15:30:57 -04:00
|
|
|
* @param handler Callback to run when the download progresses
|
|
|
|
* @returns Same AsyncWebRequest
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& progress(AsyncProgress handler);
|
|
|
|
/**
|
2023-01-24 11:17:03 -05:00
|
|
|
* Specify a callback to run if the download is cancelled. The callback is
|
|
|
|
* always ran in the GD thread, so interacting with UI is safe. Web
|
|
|
|
* requests may be cancelled after they are finished (for example, if
|
|
|
|
* downloading files in bulk and one fails). In that case, handle
|
|
|
|
* freeing up the results of `then` in this handler
|
2022-10-13 15:30:57 -04:00
|
|
|
* @param handler Callback to run if the download is cancelled
|
|
|
|
* @returns Same AsyncWebRequest
|
|
|
|
*/
|
|
|
|
AsyncWebRequest& cancelled(AsyncCancelled handler);
|
2022-10-12 21:50:41 -04:00
|
|
|
};
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2024-02-26 11:26:34 -05:00
|
|
|
class [[deprecated("Use the WebRequest class from the web2.hpp header instead")]] AsyncWebResult {
|
2022-10-12 17:22:43 -04:00
|
|
|
private:
|
|
|
|
AsyncWebRequest& m_request;
|
|
|
|
DataConverter<T> m_converter;
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
AsyncWebResult(AsyncWebRequest& request, DataConverter<T> converter) :
|
|
|
|
m_request(request), m_converter(converter) {}
|
|
|
|
|
2022-10-12 17:22:43 -04:00
|
|
|
friend class AsyncWebResponse;
|
|
|
|
|
|
|
|
public:
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Specify a callback to run after a download is finished. Runs in the
|
2022-10-13 15:30:57 -04:00
|
|
|
* GD thread, so interacting with UI is safe
|
|
|
|
* @param handle Callback to run
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns The original AsyncWebRequest, where you can specify more
|
2022-10-13 15:30:57 -04:00
|
|
|
* aspects about the request like failure and progress callbacks
|
|
|
|
*/
|
2023-02-08 10:25:07 -05:00
|
|
|
AsyncWebRequest& then(utils::MiniFunction<void(T)> handle);
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Specify a callback to run after a download is finished. Runs in the
|
2022-10-13 15:30:57 -04:00
|
|
|
* GD thread, so interacting with UI is safe
|
|
|
|
* @param handle Callback to run
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns The original AsyncWebRequest, where you can specify more
|
2022-10-13 15:30:57 -04:00
|
|
|
* aspects about the request like failure and progress callbacks
|
|
|
|
*/
|
2023-02-08 10:25:07 -05:00
|
|
|
AsyncWebRequest& then(utils::MiniFunction<void(SentAsyncWebRequest&, T)> handle);
|
2022-10-12 17:22:43 -04:00
|
|
|
};
|
|
|
|
|
2024-02-26 11:26:34 -05:00
|
|
|
class GEODE_DLL [[deprecated("Use the WebRequest class from the web2.hpp header instead")]] AsyncWebResponse {
|
2022-10-12 17:22:43 -04:00
|
|
|
private:
|
|
|
|
AsyncWebRequest& m_request;
|
|
|
|
|
|
|
|
inline AsyncWebResponse(AsyncWebRequest& request) : m_request(request) {}
|
|
|
|
|
|
|
|
friend class AsyncWebRequest;
|
|
|
|
|
|
|
|
public:
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Download into a stream. Make sure the stream lives for the entire
|
|
|
|
* duration of the request. If you want to download a file, use the
|
2024-06-02 21:35:05 -04:00
|
|
|
* `std::filesystem::path` overload of `into` instead
|
2022-10-30 14:59:20 -04:00
|
|
|
* @param stream Stream to download into. Make sure it lives long
|
2022-10-13 15:30:57 -04:00
|
|
|
* enough, otherwise the web request will crash
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
|
|
|
* after the download is finished. The result has a `std::monostate`
|
|
|
|
* template parameter, as it can be assumed you know what you passed
|
2022-10-13 15:30:57 -04:00
|
|
|
* into `into`
|
|
|
|
*/
|
2022-10-12 17:22:43 -04:00
|
|
|
AsyncWebResult<std::monostate> into(std::ostream& stream);
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
|
|
|
* Download into a file
|
2022-10-30 14:59:20 -04:00
|
|
|
* @param path File to download into. If it already exists, it will
|
2022-10-13 15:30:57 -04:00
|
|
|
* be overwritten.
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
|
|
|
* after the download is finished. The result has a `std::monostate`
|
|
|
|
* template parameter, as it can be assumed you know what you passed
|
2022-10-13 15:30:57 -04:00
|
|
|
* into `into`
|
|
|
|
*/
|
2024-06-02 21:35:05 -04:00
|
|
|
AsyncWebResult<std::monostate> into(std::filesystem::path const& path);
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
|
|
|
* Download into memory as a string
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
2022-10-13 15:30:57 -04:00
|
|
|
* after the download is finished
|
|
|
|
*/
|
2022-10-12 17:22:43 -04:00
|
|
|
AsyncWebResult<std::string> text();
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
|
|
|
* Download into memory as a byte array
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
2022-10-13 15:30:57 -04:00
|
|
|
* after the download is finished
|
|
|
|
*/
|
2022-12-14 06:11:19 -05:00
|
|
|
AsyncWebResult<ByteVector> bytes();
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
|
|
|
* Download into memory as JSON
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
2022-10-13 15:30:57 -04:00
|
|
|
* after the download is finished
|
|
|
|
*/
|
2023-12-30 12:42:53 -05:00
|
|
|
AsyncWebResult<matjson::Value> json();
|
2022-10-12 17:22:43 -04:00
|
|
|
|
2022-10-13 15:30:57 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Download into memory as a custom type. The data will first be
|
|
|
|
* downloaded into memory as a byte array, and then converted using
|
2022-10-13 15:30:57 -04:00
|
|
|
* the specified converter function
|
2022-10-30 14:59:20 -04:00
|
|
|
* @param converter Function that converts the data from a byte array
|
2022-10-13 15:30:57 -04:00
|
|
|
* to the desired type
|
2022-10-30 14:59:20 -04:00
|
|
|
* @returns AsyncWebResult, where you can specify the `then` action for
|
2022-10-13 15:30:57 -04:00
|
|
|
* after the download is finished
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-10-12 17:22:43 -04:00
|
|
|
AsyncWebResult<T> as(DataConverter<T> converter) {
|
|
|
|
return AsyncWebResult(m_request, converter);
|
|
|
|
}
|
|
|
|
};
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
AsyncWebRequest& AsyncWebResult<T>::then(utils::MiniFunction<void(T)> handle) {
|
2024-01-03 20:17:27 -05:00
|
|
|
return m_request.setThen([converter = m_converter,
|
2022-12-14 06:11:19 -05:00
|
|
|
handle](SentAsyncWebRequest& req, ByteVector const& arr) {
|
2022-10-13 09:36:36 -04:00
|
|
|
auto conv = converter(arr);
|
|
|
|
if (conv) {
|
2022-11-28 10:42:19 -05:00
|
|
|
handle(conv.unwrap());
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-01-24 11:17:03 -05:00
|
|
|
req.error("Unable to convert value: " + conv.unwrapErr(), -1);
|
2022-10-13 09:36:36 -04:00
|
|
|
}
|
2024-01-03 20:17:27 -05:00
|
|
|
});
|
2022-10-13 09:36:36 -04:00
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2023-02-08 10:25:07 -05:00
|
|
|
AsyncWebRequest& AsyncWebResult<T>::then(utils::MiniFunction<void(SentAsyncWebRequest&, T)> handle) {
|
2024-01-03 20:17:27 -05:00
|
|
|
return m_request.setThen([converter = m_converter,
|
2022-12-14 06:11:19 -05:00
|
|
|
handle](SentAsyncWebRequest& req, ByteVector const& arr) {
|
2022-10-13 09:36:36 -04:00
|
|
|
auto conv = converter(arr);
|
|
|
|
if (conv) {
|
|
|
|
handle(req, conv.value());
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-01-24 11:17:03 -05:00
|
|
|
req.error("Unable to convert value: " + conv.error(), -1);
|
2022-10-13 09:36:36 -04:00
|
|
|
}
|
2024-01-03 20:17:27 -05:00
|
|
|
});
|
2022-10-13 09:36:36 -04:00
|
|
|
}
|
2022-10-05 08:41:05 -04:00
|
|
|
}
|