1
1
import logging
2
2
from typing import List
3
3
4
- import httpx
5
-
6
4
from webviz_pkg .core_utils .perf_timer import PerfTimer
7
5
8
6
from primary import config
7
+ from primary .httpx_client import httpx_async_client
9
8
from primary .services .service_exceptions import ServiceRequestError , Service
10
9
11
10
LOGGER = logging .getLogger (__name__ )
@@ -28,35 +27,34 @@ async def smda_get_request(access_token: str, endpoint: str, params: dict) -> Li
28
27
timer = PerfTimer ()
29
28
single_lap_timer = PerfTimer ()
30
29
31
- async with httpx .AsyncClient () as client :
32
- results : List [dict ] = []
33
- page : int = 1
34
- while True :
35
- response = await client .get (urlstring , params = params , headers = headers , timeout = 60 )
36
- LOGGER .info (f"TIME SMDA fetch '{ endpoint } ', page { page } , took { single_lap_timer .lap_s ():.2f} seconds" )
37
- page += 1
38
- if response .status_code == 200 :
39
- result = response .json ()["data" ]["results" ]
40
- if not result :
41
- raise ServiceRequestError (f"No data found for endpoint: '{ endpoint } '" , Service .SMDA )
42
-
43
- results .extend (result )
44
-
45
- next_request = response .json ()["data" ]["next" ]
46
- if next_request is None :
47
- break
48
- params ["_next" ] = next_request
49
- elif response .status_code == 404 :
50
- LOGGER .error (f"{ str (response .status_code ) } { endpoint } either does not exists or can not be found" )
51
- raise ServiceRequestError (
52
- f"[{ str (response .status_code )} ] '{ endpoint } ' either does not exists or can not be found" ,
53
- Service .SMDA ,
54
- )
55
- else :
56
- raise ServiceRequestError (
57
- f"[{ str (response .status_code )} ] Cannot fetch data from endpoint: '{ endpoint } '" , Service .SMDA
58
- )
59
-
60
- LOGGER .info (f"TIME SMDA fetch '{ endpoint } ' took { timer .lap_s ():.2f} seconds" )
30
+ results : List [dict ] = []
31
+ page : int = 1
32
+ while True :
33
+ response = await httpx_async_client .client .get (urlstring , params = params , headers = headers , timeout = 60 )
34
+ LOGGER .debug (f"TIME SMDA fetch '{ endpoint } ', page { page } , took { single_lap_timer .lap_s ():.2f} seconds" )
35
+ page += 1
36
+ if response .status_code == 200 :
37
+ result = response .json ()["data" ]["results" ]
38
+ if not result :
39
+ raise ServiceRequestError (f"No data found for endpoint: '{ endpoint } '" , Service .SMDA )
40
+
41
+ results .extend (result )
42
+
43
+ next_request = response .json ()["data" ]["next" ]
44
+ if next_request is None :
45
+ break
46
+ params ["_next" ] = next_request
47
+ elif response .status_code == 404 :
48
+ LOGGER .error (f"{ str (response .status_code ) } { endpoint } either does not exists or can not be found" )
49
+ raise ServiceRequestError (
50
+ f"[{ str (response .status_code )} ] '{ endpoint } ' either does not exists or can not be found" ,
51
+ Service .SMDA ,
52
+ )
53
+ else :
54
+ raise ServiceRequestError (
55
+ f"[{ str (response .status_code )} ] Cannot fetch data from endpoint: '{ endpoint } '" , Service .SMDA
56
+ )
57
+
58
+ LOGGER .debug (f"TIME SMDA fetch '{ endpoint } ' took { timer .lap_s ():.2f} seconds" )
61
59
62
60
return results
0 commit comments