Debian Codegraph @

examples/timeout.py

raw
1 # install cli-oauth2 for requests: pip install cli-oauth2
2 from oauthcli import OpenStreetMapDevAuth
3 import osmapi
4 from dotenv import load_dotenv, find_dotenv
5 import os
6
7 load_dotenv(find_dotenv())
8
9 # load secrets for OAuth
10 client_id = os.getenv("OSM_OAUTH_CLIENT_ID")
11 client_secret = os.getenv("OSM_OAUTH_CLIENT_SECRET")
12
13 auth = OpenStreetMapDevAuth(
14 client_id, client_secret, ["write_api", "write_notes"]
15 ).auth_code()
16
17
18 # Use a normal timeout (30s is the default value)
19
20 normal_timeout_api = osmapi.OsmApi(
21 api="https://api06.dev.openstreetmap.org", session=auth.session, timeout=30
22 )
23 changeset_id = normal_timeout_api.changeset_create({"comment": "My first test"})
24 print(f"Create new changeset {changeset_id}")
25
26 # Deliberately using a very small timeout to show what happens when a timeout occurs
27
28 low_timeout_api = osmapi.OsmApi(
29 api="https://api06.dev.openstreetmap.org", session=auth.session, timeout=0.00001
30 )
31 try:
32 changeset_id = low_timeout_api.changeset_create({"comment": "My first test"})
33 print(f"Create new changeset {changeset_id}")
34 except osmapi.errors.TimeoutApiError as e:
35 print(f"Timeout error occured: {str(e)}")