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

◆ test_promise_approach()

void test_promise_approach ( )

Definition at line 109 of file ClientReal.h.

109 {
110 FL_WARN("APPROACH 1: Promise-based pattern with explicit types");
111
112 // TUTORIAL: fetch_get() returns fl::task::Promise<fl::net::http::Response> (not auto!)
113 // The promise represents a future HTTP response that may succeed or fail
114 // Chain .then() for success handling and the lambda receives a
115 // const fl::net::http::Response& when the fetch succeeds. error_ will handle network device
116 // failures (no connection, DNS failure, etc, but not HTTP errors like 404, 500, etc.)
117 fl::net::http::fetch_get("http://fastled.io").then([](const fl::net::http::Response& response) {
118 // TUTORIAL: Check if HTTP request was successful
119 if (response.ok()) {
120 FL_WARN("SUCCESS [Promise] HTTP fetch successful! Status: "
121 << response.status() << " " << response.status_text());
122
123 // TUTORIAL: get_content_type() returns fl::optional<fl::string>
124 // Optional types may or may not contain a value - always check!
125 fl::optional<fl::string> content_type = response.get_content_type();
126 if (content_type.has_value()) {
127 FL_WARN("CONTENT [Promise] Content-Type: " << *content_type);
128 }
129
130 // TUTORIAL: response.text() returns fl::string with response body
131 const fl::string& response_body = response.text();
132 if (response_body.length() >= 100) {
133 FL_WARN("RESPONSE [Promise] First 100 characters: " << response_body.substr(0, 100));
134 } else {
135 FL_WARN("RESPONSE [Promise] Full response (" << response_body.length()
136 << " chars): " << response_body);
137 }
138
139 // Visual feedback: Green LEDs indicate promise-based success
140 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 64, 0)); // Green for promise success
141 } else {
142 // HTTP error (like 404, 500, etc.) - still a valid response, just an error status
143 FL_WARN("ERROR [Promise] HTTP Error! Status: "
144 << response.status() << " " << response.status_text());
145 FL_WARN("CONTENT [Promise] Error content: " << response.text());
146
147 // Visual feedback: Orange LEDs indicate HTTP error
148 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 32, 0)); // Orange for HTTP error
149 }
150 })
151 // TUTORIAL: Chain .catch_() for network/connection error handling
152 // The lambda receives a const fl::task::Error& when the fetch fails completely
153 .catch_([](const fl::task::Error& network_error) {
154 // Network error (no connection, DNS failure, etc.)
155 FL_WARN("ERROR [Promise] Network Error: " << network_error.message);
156
157 // Visual feedback: Red LEDs indicate network failure
158 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 0, 0)); // Red for network error
159 });
160}
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
bool has_value() const FL_NOEXCEPT
Definition optional.h:42
fl::size length() const FL_NOEXCEPT
HTTP response class (unified interface)
Definition fetch.h:78
string substr(fl::size start, fl::size length) const FL_NOEXCEPT
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.
Optional< T > optional
Definition optional.h:16
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
fl::string message
Definition promise.h:40
Error type for promises.
Definition promise.h:39

References fl::net::http::fetch_get(), fill_solid(), FL_WARN, fl::Optional< T >::has_value(), leds, fl::basic_string::length(), fl::task::Error::message, NUM_LEDS, and fl::string::substr().

Referenced by loop().

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