22Download client for Sentinel Hub service
33"""
44
5- from defusedxml import ElementTree
6-
75import requests
86import requests .auth
97from qgis .PyQt .QtCore import QSettings
108
119from ..constants import DEFAULT_REQUEST_TIMEOUT
12- from ..exceptions import DownloadError
10+ from ..exceptions import DownloadError , get_error_message
1311from ..utils .meta import get_plugin_version
1412from .session import Session
1513
@@ -38,7 +36,9 @@ def download(self, url, timeout=DEFAULT_REQUEST_TIMEOUT, session_settings=None):
3836 proxy_dict , auth = get_proxy_config ()
3937 headers = self ._prepare_headers (session_settings )
4038 try :
41- response = requests .get (url , headers = headers , timeout = timeout , proxies = proxy_dict , auth = auth )
39+ response = requests .get (
40+ url , headers = headers , timeout = timeout , proxies = proxy_dict , auth = auth
41+ )
4242 response .raise_for_status ()
4343 except requests .RequestException as exception :
4444 raise DownloadError (get_error_message (exception )) from exception
@@ -63,61 +63,15 @@ def _get_session(settings):
6363 return Client ._CACHED_SESSIONS [cache_key ]
6464
6565 session = Session (
66- base_url = settings .base_url , client_id = settings .client_id , client_secret = settings .client_secret
66+ base_url = settings .base_url ,
67+ client_id = settings .client_id ,
68+ client_secret = settings .client_secret ,
6769 )
6870
6971 Client ._CACHED_SESSIONS [cache_key ] = session
7072 return session
7173
7274
73- def get_error_message (exception ):
74- """Creates an error message from the given exception
75-
76- :param exception: Exception obtained during download
77- :type exception: requests.RequestException
78- :return: error message
79- :rtype: str
80- """
81- message = f"{ exception .__class__ .__name__ } : "
82-
83- if isinstance (exception , (requests .ConnectionError , requests .Timeout )):
84- if isinstance (exception , requests .ConnectionError ):
85- message += "Cannot access service, check your internet connection."
86- else :
87- message += "Connection timed out, service is too slow"
88-
89- enabled , host , port , _ , _ = get_proxy_from_qsettings ()
90- if enabled :
91- message += f" QGIS is configured to use proxy: { host } "
92- if port :
93- message += f":{ port } "
94-
95- return message
96-
97- if isinstance (exception , requests .HTTPError ):
98- try :
99- server_message = ""
100- for elem in ElementTree .fromstring (exception .response .content ):
101- if "ServiceException" in elem .tag :
102- server_message += elem .text .strip ("\n \t " )
103- except ElementTree .ParseError :
104- server_message = exception .response .text .strip ("\n \t " )
105-
106- server_message = server_message .encode ("ascii" , errors = "ignore" ).decode ("utf-8" )
107-
108- # Include HTTP status code
109- status_code = exception .response .status_code
110- message += f"HTTP { status_code } - "
111-
112- # Provide meaningful message when server response is empty
113- if not server_message :
114- server_message = "No additional details provided by server"
115-
116- return message + f"{ server_message } "
117-
118- return message + str (exception )
119-
120-
12175def get_proxy_config ():
12276 """Get proxy config from QSettings and builds proxy parameters
12377
@@ -132,7 +86,11 @@ def get_proxy_config():
13286 for protocol in ["http" , "https" , "ftp" ]:
13387 proxy_dict [protocol ] = f"{ protocol } ://{ host } { port_str } "
13488
135- auth = requests .auth .HTTPProxyAuth (user , password ) if enabled and user and password else None
89+ auth = (
90+ requests .auth .HTTPProxyAuth (user , password )
91+ if enabled and user and password
92+ else None
93+ )
13694
13795 return proxy_dict , auth
13896
0 commit comments