1212
1313LOG = logging .getLogger (__name__ )
1414
15- CLIENT_ID = os .getenv (
16- "SWISSCOM_CLIENT_ID" , ""
17- ) # customer key in the Swisscom digital marketplace
18- CLIENT_SECRET = os .getenv (
19- "SWISSCOM_CLIENT_SECRET" , ""
20- ) # customer secret in the Swisscom digital marketplace
15+ CLIENT_ID = os .getenv ("SWISSCOM_CLIENT_ID" , "" ) # customer key in the Swisscom digital marketplace
16+ CLIENT_SECRET = os .getenv ("SWISSCOM_CLIENT_SECRET" , "" ) # customer secret in the Swisscom digital marketplace
2117MIN_DATE = os .getenv ("MIN_DATE" , "03.10.2022" )
2218MAX_DATE = os .getenv ("MAX_DATE" , "16.10.2022" )
2319MAX_NB_TILES_REQUEST = int (os .getenv ("MAX_NB_TILES_REQUEST" , "100" ))
@@ -51,24 +47,18 @@ def auth(self) -> OAuth2Session:
5147 # Fetch an access token
5248 client = BackendApplicationClient (client_id = CLIENT_ID )
5349 oauth = OAuth2Session (client = client )
54- oauth .fetch_token (
55- token_url = TKN_URL , client_id = CLIENT_ID , client_secret = CLIENT_SECRET
56- )
50+ oauth .fetch_token (token_url = TKN_URL , client_id = CLIENT_ID , client_secret = CLIENT_SECRET )
5751 return oauth
5852
5953 def get_tiles_ids (self , oauth : OAuth2Session , postal_code : int ) -> list [int ]:
6054 # For muni/district id, see https://www.atlas.bfs.admin.ch/maps/13/fr/17804_229_228_227/27579.html
6155 # Municipalities and Districts doesn't work well probably because of the free plan
6256 # Get all the first MAX_NB_TILES_REQUEST tile ids associated with the postal code of interest
63- muni_tiles_json = oauth .get (
64- BASE_URL + f"/grids/postal-code-areas/{ postal_code } " , headers = HEADERS
65- )
57+ muni_tiles_json = oauth .get (BASE_URL + f"/grids/postal-code-areas/{ postal_code } " , headers = HEADERS )
6658 self .check_api_error (muni_tiles_json )
6759 tiles = muni_tiles_json .json ()["tiles" ]
6860 LOG .info ("Nb tiles received: %s" , len (tiles ))
69- return [t ["tileId" ] for t in muni_tiles_json .json ()["tiles" ]][
70- :MAX_NB_TILES_REQUEST
71- ]
61+ return [t ["tileId" ] for t in muni_tiles_json .json ()["tiles" ]][:MAX_NB_TILES_REQUEST ]
7262
7363 def query_api_generic (
7464 self , oauth : OAuth2Session , path : str , postal_code : int , date_time : datetime
@@ -89,32 +79,24 @@ def response_to_geojson_result(self, data: dict[str, Any]) -> FeatureCollection:
8979 features .append (Feature (geometry = Point (coordinate ), properties = element ))
9080 return FeatureCollection (features )
9181
92- def get_dwell_density (
93- self , postal_code : int , date_time : datetime
94- ) -> FeatureCollection | Response :
82+ def get_dwell_density (self , postal_code : int , date_time : datetime ) -> FeatureCollection | Response :
9583 self .error = None
9684 try :
9785 self .limit_query ()
9886 oauth = self .auth ()
99- api_request = self .query_api_generic (
100- oauth , "/dwell-density/hourly" , postal_code , date_time
101- )
87+ api_request = self .query_api_generic (oauth , "/dwell-density/hourly" , postal_code , date_time )
10288 response = oauth .get (api_request , headers = HEADERS )
10389 self .check_api_error (response )
10490 except (ExternalAPIError , APIUsageExceededError ):
10591 return self .error
10692 return self .response_to_geojson_result (response .json ())
10793
108- def get_dwell_demographics (
109- self , postal_code : int , date_time : datetime
110- ) -> FeatureCollection | Response :
94+ def get_dwell_demographics (self , postal_code : int , date_time : datetime ) -> FeatureCollection | Response :
11195 self .error = None
11296 try :
11397 self .limit_query ()
11498 oauth = self .auth ()
115- api_request = self .query_api_generic (
116- oauth , "/dwell-demographics/hourly" , postal_code , date_time
117- )
99+ api_request = self .query_api_generic (oauth , "/dwell-demographics/hourly" , postal_code , date_time )
118100 response = oauth .get (api_request , headers = HEADERS )
119101 self .check_api_error (response )
120102 except (ExternalAPIError , APIUsageExceededError ):
0 commit comments