FastLED 3.9.15
Loading...
Searching...
No Matches

◆ test_json_await()

void test_json_await ( )

APPROACH 4: JSON Response with await pattern
Same JSON handling but using await_top_level for synchronous-style code.

Definition at line 286 of file ClientReal.h.

286 {
287 FL_WARN("APPROACH 4: JSON Response with await pattern");
288
289 // TUTORIAL: Using await pattern with JSON responses
290 // fl::net::http::fetch_get() returns fl::task::Promise<fl::net::http::Response>
291 fl::task::Promise<fl::net::http::Response> json_promise = fl::net::http::fetch_get("https://httpbin.org/get");
292
293 // TUTORIAL: await_top_level() converts promise to result
294 // fl::task::PromiseResult<fl::net::http::Response> contains either response or error
296
297 if (result.ok()) {
298 // TUTORIAL: Extract the response from the result
300
301 FL_WARN("SUCCESS [JSON Await] HTTP fetch successful! Status: "
302 << http_response.status() << " " << http_response.status_text());
303
304 // TUTORIAL: Check for JSON content and parse if available
305 if (http_response.is_json()) {
306 FL_WARN("DETECTED [JSON Await] Response contains JSON data");
307
308 // TUTORIAL: Parse JSON with automatic caching
309 fl::json data = http_response.json();
310
311 // TUTORIAL: httpbin.org/get returns information about the request
312 // Extract data with safe defaults using FastLED's ideal JSON API
313 fl::string origin_ip = data["origin"] | fl::string("unknown");
314 fl::string request_url = data["url"] | fl::string("unknown");
315
316 FL_WARN("JSON [Await] Request Origin IP: " << origin_ip);
317 FL_WARN("JSON [Await] Request URL: " << request_url);
318
319 // TUTORIAL: Access nested headers object safely
320 if (data.contains("headers")) {
321 fl::json headers = data["headers"];
322 fl::string user_agent = headers["User-Agent"] | fl::string("unknown");
323 fl::string accept = headers["Accept"] | fl::string("unknown");
324
325 FL_WARN("JSON [Await] User-Agent: " << user_agent);
326 FL_WARN("JSON [Await] Accept: " << accept);
327 }
328
329 // TUTORIAL: Access query parameters (if any)
330 if (data.contains("args")) {
331 fl::json args = data["args"];
332 if (args.size() > 0) {
333 FL_WARN("JSON [Await] Query parameters found: " << args.size());
334 } else {
335 FL_WARN("JSON [Await] No query parameters in request");
336 }
337 }
338
339 // Visual feedback: Cyan LEDs for successful await JSON processing
340 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 128, 128)); // Cyan for await JSON success
341
342 } else {
343 FL_WARN("INFO [JSON Await] Response is not JSON format");
344 // Visual feedback: Orange for non-JSON with await
345 fill_solid(leds, NUM_LEDS, fl::CRGB(128, 32, 0)); // Orange for non-JSON await
346 }
347
348 } else {
349 // TUTORIAL: Handle request failures (network or HTTP errors)
350 FL_WARN("ERROR [JSON Await] Request failed: " << result.error_message());
351 // Visual feedback: Red LEDs for any await error
352 fill_solid(leds, NUM_LEDS, fl::CRGB(128, 0, 0)); // Red for await error
353 }
354
355 FastLED.show();
356}
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Response & json(const class json &data)
Set JSON response body with automatic Content-Type header.
Response & status(int code)
Set HTTP status code.
bool contains(size_t idx) const FL_NOEXCEPT
Definition json.h:625
HTTP response class (unified interface)
Definition fetch.h:78
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:58
Result type for promise operations.
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
#define FL_WARN(X)
Definition log.h:276
fl::task::Promise< Response > fetch_get(const fl::string &url, const FetchOptions &request)
HTTP GET request.
PromiseResult< T > await_top_level(Promise< T > p)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition executor.h:186
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
asio::http::Response http_response
Definition server.h:281
corkscrew_args args
Definition old.h:149
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38

References args, fl::task::await_top_level(), fl::json::contains(), FastLED, fl::net::http::fetch_get(), fill_solid(), FL_WARN, fl::asio::http::Response::json(), leds, NUM_LEDS, and fl::asio::http::Response::status().

Referenced by loop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: