1111
1212# 3rd party:
1313from requests import request , Response
14+ import certifi
1415
1516# Internal:
16- from .utils import save_data
17+ from uk_covid19 .utils import save_data
1718
1819# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1920
@@ -32,7 +33,6 @@ class Cov19API:
3233 ------------
3334 Interface to access the API service for COVID-19 data in the United Kingdom.
3435 """
35-
3636 endpoint = "https://api.coronavirus.data.gov.uk/v1/data"
3737 _valid_formats = [
3838 "csv" ,
@@ -163,7 +163,8 @@ def head(self):
163163 """
164164 api_params = self .api_params
165165
166- with request ("HEAD" , self .endpoint , params = api_params ) as response :
166+ with request ("HEAD" , self .endpoint , params = api_params ,
167+ verify = certifi .where ()) as response :
167168 response .raise_for_status ()
168169 return response .headers
169170
@@ -189,7 +190,7 @@ def options():
189190 ...
190191 }
191192 """
192- with request ("OPTIONS" , Cov19API .endpoint ) as response :
193+ with request ("OPTIONS" , Cov19API .endpoint , verify = certifi . where () ) as response :
193194 response .raise_for_status ()
194195 return response .json ()
195196
@@ -220,21 +221,22 @@ def _get(self, format_as: str) -> Iterator[Response]:
220221 })
221222
222223 while True :
223- response = request ("GET" , self .endpoint , params = api_params )
224-
225- if response .status_code >= HTTPStatus .BAD_REQUEST :
226- raise RuntimeError (f'Request failed: { response .text } ' )
227- elif response .status_code == HTTPStatus .NO_CONTENT :
228- self .total_pages = api_params ["page" ] - 1
229- break
224+ with request ("GET" , self .endpoint , params = api_params ,
225+ verify = certifi . where ()) as response :
226+ if response .status_code >= HTTPStatus .BAD_REQUEST :
227+ raise RuntimeError (f'Request failed: { response .text } ' )
228+ if response .status_code == HTTPStatus .NO_CONTENT :
229+ self .total_pages = api_params ["page" ] - 1
230+ break
230231
231- self ._last_update = response .headers ["Last-Modified" ]
232+ self ._last_update = response .headers ["Last-Modified" ]
232233
233- yield response
234+ yield response
234235
235236 api_params ["page" ] += 1
236237
237- def get_json (self , save_as : Union [str , None ] = None , as_string : bool = False ) -> Union [dict , str ]:
238+ def get_json (self , save_as : Union [str , None ] = None ,
239+ as_string : bool = False ) -> Union [dict , str ]:
238240 """
239241 Provides full data (all pages) in JSON.
240242
@@ -446,7 +448,7 @@ def get_csv(self, save_as=None) -> str:
446448 return resp
447449
448450 def __str__ (self ):
449- resp = "COVID-19 API\n Current parameters: \n "
451+ resp = "COVID-19 in the UK - API Service \n Current parameters: \n "
450452 return resp + dumps (self .api_params , indent = 4 )
451453
452454 __repr__ = __str__
0 commit comments