|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# Requests installation and sample application" |
| 7 | + "# httpx installation and sample application" |
8 | 8 | ] |
9 | 9 | }, |
10 | 10 | { |
|
13 | 13 | "source": [ |
14 | 14 | "## Installation\n", |
15 | 15 | "\n", |
16 | | - "The requests library is useful for communicating with REST APIs. With [Spack](../../productive/envs/spack/index.rst) you can provide requests in your kernel:\n", |
| 16 | + "The httpx library is useful for communicating with REST APIs. With [Spack](../../productive/envs/spack/index.rst) you can provide httpx in your kernel:\n", |
17 | 17 | "\n", |
18 | 18 | "``` bash\n", |
19 | 19 | "$ spack env activate python-311\n", |
20 | | - "$ spack install py-requests\n", |
| 20 | + "$ spack install py-httpx\n", |
21 | 21 | "```\n", |
22 | 22 | "\n", |
23 | | - "Alternatively, you can install requests with other package managers, for example\n", |
| 23 | + "Alternatively, you can install httpx with other package managers, for example\n", |
24 | 24 | "\n", |
25 | 25 | "``` bash\n", |
26 | | - "$ pipenv install requests\n", |
| 26 | + "$ pipenv install httpx\n", |
27 | 27 | "```" |
28 | 28 | ] |
29 | 29 | }, |
|
35 | 35 | "\n", |
36 | 36 | "In this example we get our data from the [OpenStreetMap Nominatim API](https://nominatim.org/release-docs/develop/api/Overview/#nominatim-api). This can be reached via the URL `https://nominatim.openstreetmap.org/search?`. To e.g. receive information about the Berlin Congress Center in Berlin in JSON format, the URL `https://nominatim.openstreetmap.org/search.php?q=Alexanderplatz+Berlin&format=json` should be given, and if you want to display the corresponding map section you just have to leave out `&format=json`.\n", |
37 | 37 | "\n", |
38 | | - "Then we define the base URL and the parameters. Nominatim expects at least the following two parameters\n", |
| 38 | + "Then we define the search URL and the parameters. Nominatim expects at least the following two parameters\n", |
39 | 39 | "\n", |
40 | 40 | "| Key | Value |\n", |
41 | 41 | "| --------- | ------------------------------------ |\n", |
|
51 | 51 | "metadata": {}, |
52 | 52 | "outputs": [], |
53 | 53 | "source": [ |
54 | | - "import requests\n", |
| 54 | + "import httpx\n", |
55 | 55 | "\n", |
56 | 56 | "\n", |
57 | | - "base_url = \"https://nominatim.openstreetmap.org/search?\"\n", |
| 57 | + "search_url = \"https://nominatim.openstreetmap.org/search?\"\n", |
58 | 58 | "params = {\n", |
59 | 59 | " \"q\": \"Alexanderplatz, Berlin\",\n", |
60 | 60 | " \"format\": \"json\",\n", |
61 | 61 | "}\n", |
62 | | - "r = requests.get(base_url, params=params)" |
| 62 | + "r = httpx.get(search_url, params=params)" |
63 | 63 | ] |
64 | 64 | }, |
65 | 65 | { |
|
92 | 92 | { |
93 | 93 | "data": { |
94 | 94 | "text/plain": [ |
95 | | - "[{'place_id': 261767431,\n", |
96 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 95 | + "[{'place_id': 128497332,\n", |
| 96 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
97 | 97 | " 'osm_type': 'way',\n", |
98 | 98 | " 'osm_id': 783052052,\n", |
99 | | - " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801'],\n", |
100 | 99 | " 'lat': '52.5219814',\n", |
101 | 100 | " 'lon': '13.413635717448294',\n", |
102 | | - " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
103 | 101 | " 'class': 'place',\n", |
104 | 102 | " 'type': 'square',\n", |
105 | | - " 'importance': 0.6914982526373583},\n", |
106 | | - " {'place_id': 45802928,\n", |
107 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 103 | + " 'place_rank': 25,\n", |
| 104 | + " 'importance': 0.47149825263735834,\n", |
| 105 | + " 'addresstype': 'square',\n", |
| 106 | + " 'name': 'Alexanderplatz',\n", |
| 107 | + " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
| 108 | + " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801']},\n", |
| 109 | + " {'place_id': 128243381,\n", |
| 110 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
108 | 111 | " 'osm_type': 'node',\n", |
109 | 112 | " 'osm_id': 3908141014,\n", |
110 | | - " 'boundingbox': ['52.5165661', '52.5265661', '13.4062804', '13.4162804'],\n", |
111 | 113 | " 'lat': '52.5215661',\n", |
112 | 114 | " 'lon': '13.4112804',\n", |
113 | | - " 'display_name': 'Alexanderplatz, Dircksenstraße, Mitte, Berlin, 10179, Deutschland',\n", |
114 | 115 | " 'class': 'railway',\n", |
115 | 116 | " 'type': 'station',\n", |
116 | | - " 'importance': 0.6560990777880803,\n", |
117 | | - " 'icon': 'https://nominatim.openstreetmap.org/ui/mapicons/transport_train_station2.p.20.png'},\n", |
118 | | - " {'place_id': 190976103,\n", |
119 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 117 | + " 'place_rank': 30,\n", |
| 118 | + " 'importance': 0.43609907778808027,\n", |
| 119 | + " 'addresstype': 'railway',\n", |
| 120 | + " 'name': 'Alexanderplatz',\n", |
| 121 | + " 'display_name': 'Alexanderplatz, Dircksenstraße, Mitte, Berlin, 10179, Deutschland',\n", |
| 122 | + " 'boundingbox': ['52.5165661', '52.5265661', '13.4062804', '13.4162804']},\n", |
| 123 | + " {'place_id': 128416772,\n", |
| 124 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
120 | 125 | " 'osm_type': 'way',\n", |
121 | 126 | " 'osm_id': 346206374,\n", |
122 | | - " 'boundingbox': ['52.5216214', '52.5216661', '13.4131913', '13.4131914'],\n", |
123 | 127 | " 'lat': '52.5216214',\n", |
124 | 128 | " 'lon': '13.4131913',\n", |
125 | | - " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
126 | 129 | " 'class': 'highway',\n", |
127 | 130 | " 'type': 'pedestrian',\n", |
128 | | - " 'importance': 0.32000999999999996},\n", |
129 | | - " {'place_id': 51760167,\n", |
130 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
131 | | - " 'osm_type': 'node',\n", |
132 | | - " 'osm_id': 4598414751,\n", |
133 | | - " 'boundingbox': ['52.5165849', '52.5265849', '13.4089082', '13.4189082'],\n", |
134 | | - " 'lat': '52.5215849',\n", |
135 | | - " 'lon': '13.4139082',\n", |
| 131 | + " 'place_rank': 26,\n", |
| 132 | + " 'importance': 0.10000999999999993,\n", |
| 133 | + " 'addresstype': 'road',\n", |
| 134 | + " 'name': 'Alexanderplatz',\n", |
136 | 135 | " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
137 | | - " 'class': 'railway',\n", |
138 | | - " 'type': 'station',\n", |
139 | | - " 'importance': 0.22000999999999998,\n", |
140 | | - " 'icon': 'https://nominatim.openstreetmap.org/ui/mapicons/transport_train_station2.p.20.png'},\n", |
141 | | - " {'place_id': 49544606,\n", |
142 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
143 | | - " 'osm_type': 'node',\n", |
144 | | - " 'osm_id': 4389211800,\n", |
145 | | - " 'boundingbox': ['52.5231653', '52.5232653', '13.414475', '13.414575'],\n", |
146 | | - " 'lat': '52.5232153',\n", |
147 | | - " 'lon': '13.414525',\n", |
148 | | - " 'display_name': 'Alexanderplatz, Alexanderstraße, Mitte, Berlin, 10178, Deutschland',\n", |
149 | | - " 'class': 'highway',\n", |
150 | | - " 'type': 'bus_stop',\n", |
151 | | - " 'importance': 0.22000999999999998,\n", |
152 | | - " 'icon': 'https://nominatim.openstreetmap.org/ui/mapicons/transport_bus_stop2.p.20.png'},\n", |
153 | | - " {'place_id': 181190866,\n", |
154 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 136 | + " 'boundingbox': ['52.5216214', '52.5216661', '13.4131913', '13.4131914']},\n", |
| 137 | + " {'place_id': 127680907,\n", |
| 138 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
155 | 139 | " 'osm_type': 'way',\n", |
156 | | - " 'osm_id': 301241483,\n", |
157 | | - " 'boundingbox': ['52.5226699', '52.5227698', '13.4152008', '13.4154146'],\n", |
158 | | - " 'lat': '52.5227318',\n", |
159 | | - " 'lon': '13.4152821',\n", |
160 | | - " 'display_name': 'Alexanderstraße, Mitte, Berlin, 10178, Deutschland',\n", |
| 140 | + " 'osm_id': 301733776,\n", |
| 141 | + " 'lat': '52.5222454',\n", |
| 142 | + " 'lon': '13.4158136',\n", |
161 | 143 | " 'class': 'highway',\n", |
162 | 144 | " 'type': 'primary',\n", |
163 | | - " 'importance': 0.21000999999999995}]" |
| 145 | + " 'place_rank': 26,\n", |
| 146 | + " 'importance': 0.10000999999999993,\n", |
| 147 | + " 'addresstype': 'road',\n", |
| 148 | + " 'name': 'Alexanderstraße',\n", |
| 149 | + " 'display_name': 'Alexanderstraße, Mitte, Berlin, 10178, Deutschland',\n", |
| 150 | + " 'boundingbox': ['52.5222454', '52.5224356', '13.4153983', '13.4158136']}]" |
164 | 151 | ] |
165 | 152 | }, |
166 | 153 | "execution_count": 3, |
|
187 | 174 | { |
188 | 175 | "data": { |
189 | 176 | "text/plain": [ |
190 | | - "[{'place_id': 261767431,\n", |
191 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 177 | + "[{'place_id': 128497332,\n", |
| 178 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
192 | 179 | " 'osm_type': 'way',\n", |
193 | 180 | " 'osm_id': 783052052,\n", |
194 | | - " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801'],\n", |
195 | 181 | " 'lat': '52.5219814',\n", |
196 | 182 | " 'lon': '13.413635717448294',\n", |
197 | | - " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
198 | 183 | " 'class': 'place',\n", |
199 | 184 | " 'type': 'square',\n", |
200 | | - " 'importance': 0.6914982526373583}]" |
| 185 | + " 'place_rank': 25,\n", |
| 186 | + " 'importance': 0.47149825263735834,\n", |
| 187 | + " 'addresstype': 'square',\n", |
| 188 | + " 'name': 'Alexanderplatz',\n", |
| 189 | + " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
| 190 | + " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801']}]" |
201 | 191 | ] |
202 | 192 | }, |
203 | 193 | "execution_count": 4, |
|
207 | 197 | ], |
208 | 198 | "source": [ |
209 | 199 | "params = {\"q\": \"Alexanderplatz, Berlin\", \"format\": \"json\", \"limit\": \"1\"}\n", |
210 | | - "r = requests.get(base_url, params=params)\n", |
| 200 | + "r = httpx.get(search_url, params=params)\n", |
211 | 201 | "r.json()" |
212 | 202 | ] |
213 | 203 | }, |
|
229 | 219 | "cell_type": "markdown", |
230 | 220 | "metadata": {}, |
231 | 221 | "source": [ |
232 | | - "To ensure that the interaction was successful, we use the `raise_for_status` method of `requests`, which throws an exception if the HTTP status code isn’t `200 OK`:" |
| 222 | + "To ensure that the interaction was successful, we use the `raise_for_status` method of `httpx`, which throws an exception if the HTTP status code isn’t `200 OK`:" |
233 | 223 | ] |
234 | 224 | }, |
235 | 225 | { |
236 | 226 | "cell_type": "code", |
237 | 227 | "execution_count": 5, |
238 | 228 | "metadata": {}, |
239 | | - "outputs": [], |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "data": { |
| 232 | + "text/plain": [ |
| 233 | + "<Response [200 OK]>" |
| 234 | + ] |
| 235 | + }, |
| 236 | + "execution_count": 5, |
| 237 | + "metadata": {}, |
| 238 | + "output_type": "execute_result" |
| 239 | + } |
| 240 | + ], |
240 | 241 | "source": [ |
241 | 242 | "r.raise_for_status()" |
242 | 243 | ] |
|
245 | 246 | "cell_type": "markdown", |
246 | 247 | "metadata": {}, |
247 | 248 | "source": [ |
248 | | - "Since we don't want to exceed the load limits of the Nominatim API, we will delay our requests with the `time.sleep` function:" |
| 249 | + "Since we don't want to exceed the load limits of the Nominatim API, we will delay our httpx with the `time.sleep` function:" |
249 | 250 | ] |
250 | 251 | }, |
251 | 252 | { |
|
256 | 257 | { |
257 | 258 | "data": { |
258 | 259 | "text/plain": [ |
259 | | - "[{'place_id': 261767431,\n", |
260 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 260 | + "[{'place_id': 128497332,\n", |
| 261 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
261 | 262 | " 'osm_type': 'way',\n", |
262 | 263 | " 'osm_id': 783052052,\n", |
263 | | - " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801'],\n", |
264 | 264 | " 'lat': '52.5219814',\n", |
265 | 265 | " 'lon': '13.413635717448294',\n", |
266 | | - " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
267 | 266 | " 'class': 'place',\n", |
268 | 267 | " 'type': 'square',\n", |
269 | | - " 'importance': 0.6914982526373583}]" |
| 268 | + " 'place_rank': 25,\n", |
| 269 | + " 'importance': 0.47149825263735834,\n", |
| 270 | + " 'addresstype': 'square',\n", |
| 271 | + " 'name': 'Alexanderplatz',\n", |
| 272 | + " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
| 273 | + " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801']}]" |
270 | 274 | ] |
271 | 275 | }, |
272 | 276 | "execution_count": 6, |
|
302 | 306 | " \"\"\"\n", |
303 | 307 | " search_url = \"https://nominatim.openstreetmap.org/search?\"\n", |
304 | 308 | " params = {\"q\": address, \"format\": format, \"limit\": limit, **kwargs}\n", |
305 | | - " r = requests.get(search_url, params=params)\n", |
| 309 | + " r = httpx.get(search_url, params=params)\n", |
306 | 310 | " # Raise an exception if the status is unsuccessful\n", |
307 | 311 | " r.raise_for_status()\n", |
308 | 312 | "\n", |
|
325 | 329 | { |
326 | 330 | "data": { |
327 | 331 | "text/plain": [ |
328 | | - "[{'place_id': 261767431,\n", |
329 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
| 332 | + "[{'place_id': 128497332,\n", |
| 333 | + " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',\n", |
330 | 334 | " 'osm_type': 'way',\n", |
331 | 335 | " 'osm_id': 783052052,\n", |
332 | | - " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801'],\n", |
333 | 336 | " 'lat': '52.5219814',\n", |
334 | 337 | " 'lon': '13.413635717448294',\n", |
335 | | - " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
336 | 338 | " 'class': 'place',\n", |
337 | 339 | " 'type': 'square',\n", |
338 | | - " 'importance': 0.6914982526373583}]" |
| 340 | + " 'place_rank': 25,\n", |
| 341 | + " 'importance': 0.47149825263735834,\n", |
| 342 | + " 'addresstype': 'square',\n", |
| 343 | + " 'name': 'Alexanderplatz',\n", |
| 344 | + " 'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',\n", |
| 345 | + " 'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801']}]" |
339 | 346 | ] |
340 | 347 | }, |
341 | 348 | "execution_count": 8, |
|
347 | 354 | "nominatim_search(\"Alexanderplatz, Berlin\")" |
348 | 355 | ] |
349 | 356 | }, |
350 | | - { |
351 | | - "cell_type": "markdown", |
352 | | - "metadata": {}, |
353 | | - "source": [ |
354 | | - "However, you can use other parameters besides `address`. You can get an overview in the [Nominatim Docs](https://nominatim.org/release-docs/develop/api/Search/#parameters)." |
355 | | - ] |
356 | | - }, |
357 | | - { |
358 | | - "cell_type": "code", |
359 | | - "execution_count": 9, |
360 | | - "metadata": {}, |
361 | | - "outputs": [ |
362 | | - { |
363 | | - "data": { |
364 | | - "text/plain": [ |
365 | | - "[{'place_id': 194632110,\n", |
366 | | - " 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',\n", |
367 | | - " 'osm_type': 'way',\n", |
368 | | - " 'osm_id': 368946992,\n", |
369 | | - " 'boundingbox': ['52.5341829', '52.534317', '13.4237697', '13.4240606'],\n", |
370 | | - " 'lat': '52.53425085',\n", |
371 | | - " 'lon': '13.423921421750569',\n", |
372 | | - " 'display_name': '8, Marienburger Straße, Winsviertel, Prenzlauer Berg, Pankow, Berlin, 10405, Deutschland',\n", |
373 | | - " 'class': 'building',\n", |
374 | | - " 'type': 'apartments',\n", |
375 | | - " 'importance': 0.4200099999999999}]" |
376 | | - ] |
377 | | - }, |
378 | | - "execution_count": 9, |
379 | | - "metadata": {}, |
380 | | - "output_type": "execute_result" |
381 | | - } |
382 | | - ], |
383 | | - "source": [ |
384 | | - "nominatim_search(\n", |
385 | | - " address=None,\n", |
386 | | - " street=\"8, Marienburger Straße\",\n", |
387 | | - " city=\"Berlin\",\n", |
388 | | - " country=\"Germany\",\n", |
389 | | - ")" |
390 | | - ] |
391 | | - }, |
392 | 357 | { |
393 | 358 | "cell_type": "markdown", |
394 | 359 | "metadata": {}, |
|
400 | 365 | }, |
401 | 366 | { |
402 | 367 | "cell_type": "code", |
403 | | - "execution_count": 10, |
| 368 | + "execution_count": 9, |
404 | 369 | "metadata": {}, |
405 | 370 | "outputs": [], |
406 | 371 | "source": [ |
|
436 | 401 | "name": "python", |
437 | 402 | "nbconvert_exporter": "python", |
438 | 403 | "pygments_lexer": "ipython3", |
439 | | - "version": "3.11.4" |
| 404 | + "version": "3.11.5" |
440 | 405 | }, |
441 | 406 | "latex_envs": { |
442 | 407 | "LaTeX_envs_menu_present": true, |
|
0 commit comments