Skip to content

Commit 0e887cf

Browse files
committed
fix: reset user agent correctly
If the request raises an exception, the user agent was not reset properly. Due to the `finally`-statement, we can be sure that this definitely happens anyway. In case an old ORStools-related user agent was still set, it is now reset to an empty string, cleaning up old mistakes.
1 parent 02d66c4 commit 0e887cf

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

ORStools/common/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ def __init__(self, provider: Optional[dict] = None, agent: Optional[str] = None)
8181
"Authorization": provider["key"],
8282
}
8383

84+
# Set user agent
8485
self.settings = QgsSettings()
85-
# Read the current value
8686
self.user_agent = self.settings.value("qgis/networkAndProxy/userAgent")
87-
# Set a new value
87+
# In case the value was still set by the ORS Tools Plugin, reset to empty string
88+
if "ORStools" in self.user_agent:
89+
self.user_agent = ""
8890
self.settings.setValue("qgis/networkAndProxy/userAgent", agent)
8991

9092
# Save some references to retrieve in client instances
@@ -109,6 +111,9 @@ def _request(
109111
:rtype: QgsNetworkReplyContent
110112
:raises: Various ApiError exceptions based on HTTP status codes
111113
"""
114+
115+
116+
112117
if post_json is not None:
113118
result = blocking_request.post(request, json.dumps(post_json).encode())
114119
else:
@@ -193,14 +198,17 @@ def fetch_with_retry(
193198
)
194199
raise
195200

201+
finally:
202+
# Reset to old value
203+
self.settings.setValue("qgis/networkAndProxy/userAgent", self.user_agent)
204+
205+
196206
# Write env variables if successful
197207
if self.ENV_VARS:
198208
for env_var in self.ENV_VARS:
199209
header_value = reply.rawHeader(self.ENV_VARS[env_var].encode()).data().decode()
200210
configmanager.write_env_var(env_var, header_value)
201211

202-
# Reset to old value
203-
self.settings.setValue("qgis/networkAndProxy/userAgent", self.user_agent)
204212

205213
return json.loads(content)
206214

0 commit comments

Comments
 (0)