Skip to content

Commit

Permalink
feat: add custom user agent to requests (#208)
Browse files Browse the repository at this point in the history
This enables us to gather information on the plugin usage, in particular on whether it's the GUI or the processing algorithms that people use.
Given that QGIS already adds a custom user agent, this doesn't add any privacy-related information.

One algorithms name was changed to be able to remove a common suffix to keep the User Agent short .

Co-authored-by: Jakob Schnell <Jakob.Schnell@heigit.org>
  • Loading branch information
merydian and koebi authored Dec 22, 2023
1 parent 1d1e856 commit 852e1d0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
13 changes: 12 additions & 1 deletion ORStools/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
from ORStools.common import networkaccessmanager
from ORStools.utils import exceptions, configmanager, logger

from qgis.core import QgsSettings

_USER_AGENT = f"ORSQGISClient@v{__version__}"


class Client(QObject):
"""Performs requests to the ORS API services."""

def __init__(self, provider=None):
def __init__(self, provider=None, agent=None):
"""
:param provider: A openrouteservice provider from config.yml
:type provider: dict
Expand All @@ -73,6 +75,12 @@ def __init__(self, provider=None):
"Authorization": provider["key"],
}

self.settings = QgsSettings()
# Read the current value
self.user_agent = self.settings.value("qgis/networkAndProxy/userAgent")
# Set a new value
self.settings.setValue("qgis/networkAndProxy/userAgent", agent)

# Save some references to retrieve in client instances
self.url = None
self.warnings = None
Expand Down Expand Up @@ -181,6 +189,9 @@ def request(self, url, params, first_request_time=None, retry_counter=0, post_js
env_var, response.headers.get(self.ENV_VARS[env_var], "None")
)

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

return json.loads(content.decode("utf-8"))

def _check_status(self):
Expand Down
3 changes: 2 additions & 1 deletion ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def run_gui_control(self):
)
return

clnt = client.Client(provider)
agent = "QGIS_ORStoolsDialog"
clnt = client.Client(provider, agent)
clnt_msg = ""

directions = directions_gui.Directions(self.dlg)
Expand Down
10 changes: 7 additions & 3 deletions ORStools/proc/base_processing_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,20 @@ def option_parameters(self) -> [QgsProcessingParameterDefinition]:
),
]

@staticmethod
@classmethod
def _get_ors_client_from_provider(
provider: str, feedback: QgsProcessingFeedback
cls, provider: str, feedback: QgsProcessingFeedback
) -> client.Client:
"""
Connects client to provider and returns a client instance for requests to the ors API
"""
name = cls.__name__
name = name.removeprefix("ORS").removesuffix("Algo")
agent = f"QGIS_{name}"

providers = configmanager.read_config()["providers"]
ors_provider = providers[provider]
ors_client = client.Client(ors_provider)
ors_client = client.Client(ors_provider, agent)
ors_client.overQueryLimit.connect(
lambda: feedback.reportError("OverQueryLimit: Retrying...")
)
Expand Down
2 changes: 1 addition & 1 deletion ORStools/proc/directions_lines_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@


# noinspection PyPep8Naming
class ORSDirectionsLinesAlgorithm(ORSBaseProcessingAlgorithm):
class ORSDirectionsLinesAlgo(ORSBaseProcessingAlgorithm):
"""Algorithm class for Directions Lines."""

def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions ORStools/proc/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from PyQt5.QtGui import QIcon

from ORStools import RESOURCE_PREFIX, PLUGIN_NAME, __version__
from .directions_lines_proc import ORSDirectionsLinesAlgorithm
from .directions_lines_proc import ORSDirectionsLinesAlgo
from .directions_points_layer_proc import ORSDirectionsPointsLayerAlgo
from .directions_points_layers_proc import ORSDirectionsPointsLayersAlgo
from .isochrones_layer_proc import ORSIsochronesLayerAlgo
Expand All @@ -59,7 +59,7 @@ def loadAlgorithms(self):
#
self.addAlgorithm(ORSDirectionsPointsLayersAlgo())
self.addAlgorithm(ORSDirectionsPointsLayerAlgo())
self.addAlgorithm(ORSDirectionsLinesAlgorithm())
self.addAlgorithm(ORSDirectionsLinesAlgo())
self.addAlgorithm(ORSIsochronesLayerAlgo())
self.addAlgorithm(ORSIsochronesPointAlgo())
self.addAlgorithm(ORSMatrixAlgo())
Expand Down

0 comments on commit 852e1d0

Please sign in to comment.