|
62 | 62 | >Java</a |
63 | 63 | > |
64 | 64 | </li> |
| 65 | + <li class="nav-item"> |
| 66 | + <a |
| 67 | + class="examples__link nav-link" |
| 68 | + data-bs-toggle="tab" |
| 69 | + href="#python" |
| 70 | + role="tab" |
| 71 | + >Python</a |
| 72 | + > |
| 73 | + </li> |
65 | 74 | <li class="nav-item"> |
66 | 75 | <a |
67 | 76 | class="examples__link nav-link" |
@@ -224,53 +233,82 @@ <h2 class="examples__title">Query documents with Java.</h2> |
224 | 233 | </div> |
225 | 234 | </div> |
226 | 235 | </div> |
227 | | - <div class="tab-pane fade" id="swift" role="tabpanel"> |
| 236 | + <div class="tab-pane fade" id="python" role="tabpanel"> |
228 | 237 | <div class="container"> |
229 | 238 | <div class="row align-items-center"> |
230 | 239 | <div class="col-md-4 mb-4"> |
231 | | - <h2 class="examples__title">Query documents with Swift.</h2> |
| 240 | + <h2 class="examples__title">Query documents with Python.</h2> |
232 | 241 | <p class="examples__desc"> |
233 | 242 | The GET request has two query parameters: |
234 | | - <code class="black">filter</code> to appy a query and |
| 243 | + <code class="black">filter</code> to apply a query and |
235 | 244 | <code class="black">pagesize</code> to limit the response to one |
236 | 245 | document. |
237 | 246 | <br /> |
238 | | - Here we use the JSONSerialization to parse the response body. |
| 247 | + This example uses the popular |
| 248 | + <a class="text-danger" href="https://requests.readthedocs.io/" target="_blank">requests</a> |
| 249 | + library. |
239 | 250 | </p> |
240 | 251 | <a href="/try" class="btn btn-o-white">More examples</a> |
241 | 252 | </div> |
242 | 253 | <div class="col-md-8"> |
243 | | - {% highlight swift %} |
244 | | -import UIKit |
245 | | -import PlaygroundSupport |
| 254 | + {% highlight python %} |
| 255 | +import requests |
| 256 | +import json |
246 | 257 |
|
247 | | -var urlComponents = URLComponents(string: "https://demo.restheart.org/messages") |
| 258 | +url = "https://demo.restheart.org/messages" |
| 259 | +params = { |
| 260 | + "filter": '{"from":"Bob"}', |
| 261 | + "pagesize": "1" |
| 262 | +} |
248 | 263 |
|
249 | | -// set the query parameters |
250 | | -var queryItems = [URLQueryItem]() |
251 | | -queryItems.append(URLQueryItem(name: "pagesize", value: "1")) |
252 | | -queryItems.append(URLQueryItem(name: "filter", value: "{\"from\":\"Bob\"}")) |
253 | | -urlComponents?.queryItems = queryItems |
| 264 | +response = requests.get(url, params=params) |
| 265 | +data = response.json() |
254 | 266 |
|
255 | | -var urlRequest = URLRequest(url: (urlComponents?.url)!) |
256 | | -// set request method |
257 | | -urlRequest.httpMethod = "GET" |
| 267 | +print(json.dumps(data, indent=2)) |
| 268 | +{% endhighlight %} |
| 269 | + </div> |
| 270 | + </div> |
| 271 | + </div> |
| 272 | + </div> |
| 273 | + <div class="tab-pane fade" id="swift" role="tabpanel"> |
| 274 | + <div class="container"> |
| 275 | + <div class="row align-items-center"> |
| 276 | + <div class="col-md-4 mb-4"> |
| 277 | + <h2 class="examples__title">Query documents with Swift.</h2> |
| 278 | + <p class="examples__desc"> |
| 279 | + The GET request has two query parameters: |
| 280 | + <code class="black">filter</code> to apply a query and |
| 281 | + <code class="black">pagesize</code> to limit the response to one |
| 282 | + document. |
| 283 | + <br /> |
| 284 | + This example uses modern Swift with async/await (Swift 5.5+). |
| 285 | + </p> |
| 286 | + <a href="/try" class="btn btn-o-white">More examples</a> |
| 287 | + </div> |
| 288 | + <div class="col-md-8"> |
| 289 | + {% highlight swift %} |
| 290 | +import Foundation |
258 | 291 |
|
259 | | -URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in |
260 | | - guard let dataResponse = data, error == nil else { |
261 | | - print(error?.localizedDescription ?? "Response Error") |
262 | | - return |
263 | | - } |
264 | | - do { |
265 | | - let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: []) |
266 | | - print(jsonResponse) |
267 | | - } catch let jsonError { |
268 | | - print(jsonError) |
| 292 | +func fetchMessages() async throws { |
| 293 | + var components = URLComponents(string: "https://demo.restheart.org/messages") |
| 294 | + components?.queryItems = [ |
| 295 | + URLQueryItem(name: "pagesize", value: "1"), |
| 296 | + URLQueryItem(name: "filter", value: "{\"from\":\"Bob\"}") |
| 297 | + ] |
| 298 | + |
| 299 | + guard let url = components?.url else { |
| 300 | + throw URLError(.badURL) |
269 | 301 | } |
270 | | -}.resume() |
| 302 | + |
| 303 | + let (data, _) = try await URLSession.shared.data(from: url) |
| 304 | + let json = try JSONSerialization.jsonObject(with: data) |
| 305 | + print(json) |
| 306 | +} |
271 | 307 |
|
272 | | -// only needed in playground |
273 | | -PlaygroundPage.current.needsIndefiniteExecution = true |
| 308 | +// Usage |
| 309 | +Task { |
| 310 | + try await fetchMessages() |
| 311 | +} |
274 | 312 | {% endhighlight %} |
275 | 313 | </div> |
276 | 314 | </div> |
|
0 commit comments