GIScience / orstools-qgis-plugin

Plugin for QGIS with a set of tools to use openrouteservice API´s, based on openstreetmap
https://plugins.qgis.org/plugins/ORStools/
MIT License
91 stars 31 forks source link

Add origin of request to header to find out about plugin usage #202

Closed merydian closed 6 months ago

merydian commented 6 months ago

The information wether a request from came from the main GUI or not could be helpful to determine why and what for people use the plugin. This information can be of value for further development.

koebi commented 6 months ago

I investigated this a bit - it turns out we want to do that, but it doesn't work:

https://github.com/GIScience/orstools-qgis-plugin/blob/80c106afe6d7505e33c033fad91258e9e2b7118e/ORStools/common/client.py#L43 https://github.com/GIScience/orstools-qgis-plugin/blob/80c106afe6d7505e33c033fad91258e9e2b7118e/ORStools/common/client.py#L70-L74

That outputs the following user agent

User-Agent: Mozilla/5.0 QGIS/32813/Debian GNU/Linux trixie/sid

since it's being overwritten by the QGIS settings. We can overwrite them ourselves like:

from qgis.core import QgsSettings

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

# HERE BE REQUEST SHENANIGANS

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

Which will produce something like

User-Agent: ORSQGISClient@v1.6.0 QGIS/32813/Debian GNU/Linux trixie/sid