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

◆ test_await_approach()

void test_await_approach ( )

Definition at line 164 of file NetTestReal.h.

164 {
165 FL_WARN("APPROACH 2: await_top_level pattern with explicit types");
166
167 // TUTORIAL: Create a fetch_options object to configure the HTTP request
168 // fetch_options is a data container - you can set timeout, headers, etc.
169 fl::fetch_options request_config(""); // Empty URL - will use the URL passed to fetch_get()
170 request_config.timeout(5000) // 5 second timeout
171 .header("User-Agent", "FastLED/NetTest-Tutorial"); // Custom user agent
172
173 // TUTORIAL: fetch_get() returns fl::promise<fl::response> (explicit type!)
174 // This promise represents the future HTTP response
175 fl::promise<fl::response> http_promise = fl::fetch_get("http://fastled.io", request_config);
176
177 // TUTORIAL: await_top_level() returns fl::result<fl::response>
178 // result wraps either a successful response OR an Error - never both!
179 // CRITICAL: await_top_level() blocks until completion - ONLY safe in Arduino loop()!
181
182 // TUTORIAL: Check if the result contains a successful response
183 if (result.ok()) {
184 // TUTORIAL: Extract the response from the result
185 // result.value() returns const fl::response& - the actual HTTP response
186 const fl::response& http_response = result.value();
187
188 FL_WARN("SUCCESS [Await] HTTP fetch successful! Status: "
189 << http_response.status() << " " << http_response.status_text());
190
191 // TUTORIAL: Check for optional Content-Type header
192 // get_content_type() returns fl::optional<fl::string> - may be empty!
193 fl::optional<fl::string> content_type = http_response.get_content_type();
194 if (content_type.has_value()) {
195 FL_WARN("CONTENT [Await] Content-Type: " << *content_type);
196 }
197
198 // TUTORIAL: Get the response body as fl::string
199 const fl::string& response_body = http_response.text();
200 if (response_body.length() >= 100) {
201 FL_WARN("RESPONSE [Await] First 100 characters: " << response_body.substr(0, 100));
202 } else {
203 FL_WARN("RESPONSE [Await] Full response (" << response_body.length()
204 << " chars): " << response_body);
205 }
206
207 // Visual feedback: Blue LEDs indicate await-based success (different from promise)
208 fill_solid(leds, NUM_LEDS, CRGB(0, 0, 64)); // Blue for await success
209 } else {
210 // Either HTTP error OR network error - both end up here
211 // TUTORIAL: result.error_message() is a convenience method for getting error text
212 FL_WARN("ERROR [Await] Request failed: " << result.error_message());
213
214 // Visual feedback: Red LEDs for any await error
215 fill_solid(leds, NUM_LEDS, CRGB(64, 0, 0)); // Red for any error
216 }
217}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
bool has_value() const
Definition optional.h:30
StrN substr(fl::size start, fl::size length) const
Definition str.h:543
fl::size length() const
Definition str.h:325
Fetch options builder (fluent interface)
Definition fetch.h:200
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:72
fl::optional< fl::string > get_content_type() const
Get content type convenience method.
Definition fetch.h:112
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
const fl::string & text() const
Response body as text (like JavaScript response.text())
Definition fetch.h:100
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
Optional< T > optional
Definition optional.h:14
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FL_WARN
Definition warn.h:12

References fl::await_top_level(), fl::result< T >::error_message(), fl::fetch_get(), fl::fill_solid(), FL_WARN, fl::response::get_content_type(), fl::Optional< T >::has_value(), fl::fetch_options::header(), leds, fl::StrN< SIZE >::length(), NUM_LEDS, fl::result< T >::ok(), fl::response::status(), fl::response::status_text(), fl::StrN< SIZE >::substr(), fl::response::text(), fl::fetch_options::timeout(), and fl::result< T >::value().

Referenced by loop().

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