10#include <emscripten.h>
11#include <emscripten/val.h>
15#include "platforms/wasm/js_fetch.h"
29 wasm_fetch.get(url).
response(callback);
41 fl::string fetch_url = request.url().empty() ? url : request.url();
44 auto wasm_request = WasmFetchRequest(fetch_url);
66 response resp(501,
"Not Implemented");
67 resp.
set_text(
"HTTP fetch not supported on this platform");
74 FL_WARN(
"HTTP fetch is not supported on non-WASM platforms. URL: " << url);
77 response error_response(501,
"Not Implemented");
78 error_response.
set_body(
"HTTP fetch is only available in WASM/browser builds. This platform does not support network requests.");
185 const auto& opts = request.
options();
186 get_request.
timeout(opts.timeout_ms);
187 for (
const auto& header : opts.headers) {
188 get_request.
header(header.first, header.second);
190 if (!opts.body.empty()) {
191 get_request.
body(opts.body);
202 const auto& opts = request.
options();
203 post_request.
timeout(opts.timeout_ms);
204 for (
const auto& header : opts.headers) {
205 post_request.
header(header.first, header.second);
207 if (!opts.body.empty()) {
208 post_request.
body(opts.body);
219 const auto& opts = request.
options();
220 put_request.
timeout(opts.timeout_ms);
221 for (
const auto& header : opts.headers) {
222 put_request.
header(header.first, header.second);
224 if (!opts.body.empty()) {
225 put_request.
body(opts.body);
236 const auto& opts = request.
options();
237 delete_request.
timeout(opts.timeout_ms);
238 for (
const auto& header : opts.headers) {
239 delete_request.
header(header.first, header.second);
241 if (!opts.body.empty()) {
242 delete_request.
body(opts.body);
253 const auto& opts = request.
options();
254 head_request.
timeout(opts.timeout_ms);
255 for (
const auto& header : opts.headers) {
256 head_request.
header(header.first, header.second);
258 if (!opts.body.empty()) {
259 head_request.
body(opts.body);
270 const auto& opts = request.
options();
271 options_request.
timeout(opts.timeout_ms);
272 for (
const auto& header : opts.headers) {
273 options_request.
header(header.first, header.second);
275 if (!opts.body.empty()) {
276 options_request.
body(opts.body);
287 const auto& opts = request.
options();
288 patch_request.
timeout(opts.timeout_ms);
289 for (
const auto& header : opts.headers) {
290 patch_request.
header(header.first, header.second);
292 if (!opts.body.empty()) {
293 patch_request.
body(opts.body);
static AsyncManager & instance()
void register_runner(async_runner *runner)
Register an async runner.
void unregister_runner(async_runner *runner)
Unregister an async runner.
static void addListener(Listener *listener, int priority=0)
static void removeListener(Listener *listener)
~FetchEngineListener() override
void onEndFrame() override
fl::size active_requests() const
fl::unique_ptr< FetchEngineListener > mEngineListener
static FetchManager & instance()
void update() override
Update this async runner (called during async pumping)
void cleanup_completed_promises()
fl::vector< fl::promise< response > > mActivePromises
void register_promise(const fl::promise< response > &promise)
bool has_active_tasks() const override
Check if this runner has active tasks.
size_t active_task_count() const override
Get number of active tasks (for debugging/monitoring)
Internal fetch manager for promise tracking.
void push_back(const T &value)
fetch_options & timeout(int timeout_ms)
Set timeout in milliseconds.
fetch_options & body(const fl::string &data)
Set request body.
fetch_options & header(const fl::string &name, const fl::string &value)
Add header.
const RequestOptions & options() const
Get the options for this request.
Fetch options builder (fluent interface)
static promise< T > create()
Create a pending promise.
void update()
Update promise state in main loop - should be called periodically This processes pending callbacks wh...
bool complete_with_value(const T &value)
Complete the promise with a result (used by networking library)
static promise< T > resolve(const T &value)
Create a resolved promise with value.
bool valid() const
Check if promise is valid.
bool is_completed() const
Check if promise is completed (resolved or rejected)
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
fl::Json json() const
Response body parsed as JSON (JavaScript-like API)
void set_body(const fl::string &body)
void set_text(const fl::string &body)
fl::Json parse_json_body() const
Parse JSON from response body with error handling.
fl::optional< fl::Json > mCachedJson
bool is_json() const
Check if response appears to contain JSON content.
HTTP response class (unified interface)
static const fl::size npos
Unified HTTP fetch API for FastLED (cross-platform)
fl::promise< response > fetch_head(const fl::string &url, const fetch_options &request)
HTTP HEAD request.
constexpr remove_reference< T >::type && move(T &&t) noexcept
fl::promise< response > fetch_delete(const fl::string &url, const fetch_options &request)
HTTP DELETE request.
fl::promise< response > fetch_http_options(const fl::string &url, const fetch_options &request)
HTTP OPTIONS request.
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args)
void fetch_update()
Legacy manual update for fetch promises (use fl::async_run() for new code)
fl::promise< response > fetch_request(const fl::string &url, const RequestOptions &options)
Generic request with options (like fetch(url, options))
fl::promise< response > fetch_put(const fl::string &url, const fetch_options &request)
HTTP PUT request.
fl::size fetch_active_requests()
Get number of active requests.
fl::promise< response > execute_fetch_request(const fl::string &url, const fetch_options &request)
Internal helper to execute a fetch request and return a promise.
fl::promise< response > fetch_patch(const fl::string &url, const fetch_options &request)
HTTP PATCH request.
void async_run()
Run all registered async tasks once.
fl::function< void(const response &)> FetchCallback
Callback type for simple fetch responses (backward compatible)
fl::promise< response > fetch_post(const fl::string &url, const fetch_options &request)
HTTP POST request.
HeapVector< T, Allocator > vector
void fetch(const fl::string &url, const FetchCallback &callback)
Make an HTTP GET request (cross-platform, backward compatible)
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Generic asynchronous task management for FastLED.
Request options (matches JavaScript fetch RequestInit)