Skip to content

Commit b86afe6

Browse files
committed
Add Python code example to examples page
1 parent 2c43b13 commit b86afe6

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

_includes/examples.html

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
>Java</a
6363
>
6464
</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>
6574
<li class="nav-item">
6675
<a
6776
class="examples__link nav-link"
@@ -224,53 +233,82 @@ <h2 class="examples__title">Query documents with Java.</h2>
224233
</div>
225234
</div>
226235
</div>
227-
<div class="tab-pane fade" id="swift" role="tabpanel">
236+
<div class="tab-pane fade" id="python" role="tabpanel">
228237
<div class="container">
229238
<div class="row align-items-center">
230239
<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>
232241
<p class="examples__desc">
233242
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
235244
<code class="black">pagesize</code> to limit the response to one
236245
document.
237246
<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.
239250
</p>
240251
<a href="/try" class="btn btn-o-white">More examples</a>
241252
</div>
242253
<div class="col-md-8">
243-
{% highlight swift %}
244-
import UIKit
245-
import PlaygroundSupport
254+
{% highlight python %}
255+
import requests
256+
import json
246257

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+
}
248263

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()
254266

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
258291

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)
269301
}
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+
}
271307

272-
// only needed in playground
273-
PlaygroundPage.current.needsIndefiniteExecution = true
308+
// Usage
309+
Task {
310+
try await fetchMessages()
311+
}
274312
{% endhighlight %}
275313
</div>
276314
</div>

0 commit comments

Comments
 (0)