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 NetTestReal.h.

286 {
287 FL_WARN("APPROACH 4: JSON Response with await pattern");
288
289 // TUTORIAL: Using await pattern with JSON responses
290 // fl::fetch_get() returns fl::promise<fl::response>
291 fl::promise<fl::response> json_promise = fl::fetch_get("https://httpbin.org/get");
292
293 // TUTORIAL: await_top_level() converts promise to result
294 // fl::result<fl::response> contains either response or error
296
297 if (result.ok()) {
298 // TUTORIAL: Extract the response from the result
299 const fl::response& http_response = result.value();
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, 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, 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, CRGB(128, 0, 0)); // Red for await error
353 }
354
355 FastLED.show();
356}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
bool contains(size_t idx) const
Definition json.h:2078
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:72
fl::Json json() const
Response body parsed as JSON (JavaScript-like API)
Definition fetch.cpp:319
const fl::string & status_text() const
HTTP status text (like JavaScript response.statusText)
Definition fetch.h:94
int status() const
HTTP status code (like JavaScript response.status)
Definition fetch.h:91
bool is_json() const
Check if response appears to contain JSON content.
Definition fetch.h:127
HTTP response class (unified interface)
Definition fetch.h:83
const T & value() const
Get the success value (const)
bool ok() const
Check if the result is successful.
fl::string error_message() const
Get the error message as a convenience.
Result type for promise operations.
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp:9
fl::result< T > await_top_level(fl::promise< T > promise)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition async.h:175
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180
corkscrew_args args
Definition old.h:150
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FL_WARN
Definition warn.h:12

References args, fl::await_top_level(), fl::Json::contains(), fl::result< T >::error_message(), FastLED, fl::fetch_get(), fl::fill_solid(), FL_WARN, fl::response::is_json(), fl::response::json(), leds, NUM_LEDS, fl::result< T >::ok(), fl::response::status(), fl::response::status_text(), and fl::result< T >::value().

Referenced by loop().

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