Skip to content

Commit add121d

Browse files
committed
sync vs async note
1 parent 4251ac7 commit add121d

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,97 @@ int main() {
106106
2. **401 "missing-api-key" error**: Make sure you call `config->setApiKey(utility::conversions::to_string_t("api_key"), utility::conversions::to_string_t("YOUR_KEY"))` before creating the DefaultAPI instance.
107107
3. **Wrong API class**: Use `DefaultAPI` for server-side authenticated requests, `PublicAPI` for client-side/public requests.
108108

109+
## Making API Calls: Synchronous vs Asynchronous
110+
111+
All API methods in this SDK return `pplx::task<std::shared_ptr<ResponseType>>` from the C++ REST SDK. This gives you flexibility in how you handle API responses.
112+
113+
### Synchronous Calls with `.get()`
114+
115+
Use `.get()` to block the calling thread until the request completes and retrieve the result synchronously:
116+
117+
```cpp
118+
auto config = std::make_shared<org::openapitools::client::api::ApiConfiguration>();
119+
config->setBaseUrl(utility::conversions::to_string_t("https://fastcomments.com"));
120+
config->setApiKey(utility::conversions::to_string_t("api_key"),
121+
utility::conversions::to_string_t("YOUR_API_KEY"));
122+
123+
auto apiClient = std::make_shared<org::openapitools::client::api::ApiClient>(config);
124+
org::openapitools::client::api::DefaultApi api(apiClient);
125+
126+
// Call .get() to block and get the result synchronously
127+
auto response = api.getComments(
128+
utility::conversions::to_string_t("your-tenant-id"),
129+
boost::none, // page
130+
boost::none, // limit
131+
boost::none, // skip
132+
boost::none, // asTree
133+
boost::none, // skipChildren
134+
boost::none, // limitChildren
135+
boost::none, // maxTreeDepth
136+
utility::conversions::to_string_t("your-url-id"), // urlId
137+
boost::none, // userId
138+
boost::none, // anonUserId
139+
boost::none, // contextUserId
140+
boost::none, // hashTag
141+
boost::none, // parentId
142+
boost::none // direction
143+
).get(); // Blocks until the HTTP request completes
144+
145+
if (response && response->comments) {
146+
std::cout << "Found " << response->comments->size() << " comments" << std::endl;
147+
}
148+
```
149+
150+
### Asynchronous Calls with `.then()`
151+
152+
Use `.then()` for non-blocking asynchronous execution with callbacks:
153+
154+
```cpp
155+
auto config = std::make_shared<org::openapitools::client::api::ApiConfiguration>();
156+
config->setBaseUrl(utility::conversions::to_string_t("https://fastcomments.com"));
157+
config->setApiKey(utility::conversions::to_string_t("api_key"),
158+
utility::conversions::to_string_t("YOUR_API_KEY"));
159+
160+
auto apiClient = std::make_shared<org::openapitools::client::api::ApiClient>(config);
161+
org::openapitools::client::api::DefaultApi api(apiClient);
162+
163+
// Use .then() for asynchronous callback-based execution
164+
api.getComments(
165+
utility::conversions::to_string_t("your-tenant-id"),
166+
boost::none, boost::none, boost::none, boost::none, boost::none,
167+
boost::none, boost::none,
168+
utility::conversions::to_string_t("your-url-id"),
169+
boost::none, boost::none, boost::none, boost::none, boost::none, boost::none
170+
).then([](std::shared_ptr<GetComments_200_response> response) {
171+
// This runs asynchronously when the request completes
172+
if (response && response->comments) {
173+
std::cout << "Found " << response->comments->size() << " comments" << std::endl;
174+
}
175+
});
176+
177+
// Execution continues immediately without blocking
178+
std::cout << "Request sent, continuing..." << std::endl;
179+
```
180+
181+
### Choosing Between Synchronous and Asynchronous
182+
183+
The choice depends on your runtime environment and application architecture:
184+
185+
**`.get()` (Synchronous blocking)**
186+
- Blocks the calling thread until the HTTP request completes
187+
- Simpler code flow, easier to reason about
188+
- Suitable for dedicated worker threads, batch processing, or command-line tools
189+
- **Not suitable** for event loops, GUI threads, or single-threaded servers
190+
191+
**`.then()` (Asynchronous non-blocking)**
192+
- Returns immediately, callback executes when request completes
193+
- Does not block the calling thread
194+
- Required for event-driven architectures, GUI applications, or single-threaded event loops
195+
- Allows chaining multiple operations
196+
- More complex control flow
197+
198+
The SDK's test suite uses `.get()` exclusively, but this is appropriate for the test environment where blocking is acceptable.
199+
109200
## Notes
110201
111202
### Broadcast Ids

0 commit comments

Comments
 (0)