Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send common method to utils, to be used by filters and profiles #85

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions swiss_locator/core/filters/swiss_locator_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QLabel, QWidget, QTabWidget
from PyQt5.QtCore import QUrl, QUrlQuery, pyqtSignal, QEventLoop
from PyQt5.QtCore import QUrl, pyqtSignal, QEventLoop
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkAccessManager

from qgis.core import (
Expand Down Expand Up @@ -62,6 +62,7 @@
from swiss_locator.gui.config_dialog import ConfigDialog
from swiss_locator.gui.maptip import MapTip
from swiss_locator.gui.qtwebkit_conf import with_qt_web_kit
from swiss_locator.utils.utils import url_with_param


def result_from_data(result: QgsLocatorResult):
Expand Down Expand Up @@ -235,18 +236,9 @@ def box2geometry(box: str) -> QgsRectangle:
float(coords[0]), float(coords[1]), float(coords[2]), float(coords[3])
)

@staticmethod
def url_with_param(url, params) -> str:
url = QUrl(url)
q = QUrlQuery(url)
for key, value in params.items():
q.addQueryItem(key, value)
url.setQuery(q)
return url

@staticmethod
def request_for_url(url, params, headers) -> QNetworkRequest:
url = SwissLocatorFilter.url_with_param(url, params)
url = url_with_param(url, params)
request = QNetworkRequest(url)
for k, v in list(headers.items()):
request.setRawHeader(k, v)
Expand Down Expand Up @@ -540,7 +532,7 @@ def show_map_tip(self, layer, feature_id, point):
layer=layer, feature_id=feature_id
)
params = {"lang": self.lang, "sr": self.crs}
url = self.url_with_param(url, params)
url = url_with_param(url, params)
self.dbg_info(url)
request = QNetworkRequest(QUrl(url))
self.fetch_request(
Expand Down
3 changes: 2 additions & 1 deletion swiss_locator/core/filters/swiss_locator_filter_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from swiss_locator.core.filters.map_geo_admin import map_geo_admin_url
from swiss_locator.core.results import LocationResult
from swiss_locator.utils.html_stripper import strip_tags
from swiss_locator.utils.utils import url_with_param


class SwissLocatorFilterLocation(SwissLocatorFilter):
Expand Down Expand Up @@ -114,7 +115,7 @@ def fetch_feature(self, layer, feature_id):
# Try to get more info
url = f"https://api3.geo.admin.ch/rest/services/api/MapServer/{layer}/{feature_id}"
params = {"lang": self.lang, "sr": self.crs}
url = self.url_with_param(url, params)
url = url_with_param(url, params)
request = QNetworkRequest(QUrl(url))
self.fetch_request(request, QgsFeedback(), self.parse_feature_response)

Expand Down
11 changes: 2 additions & 9 deletions swiss_locator/core/profiles/profile_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from qgis.PyQt.QtCore import QUrl, QUrlQuery
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.core import (
Qgis,
Expand All @@ -18,6 +18,7 @@

from swiss_locator.core.profiles.profile_results import SwissProfileResults
from swiss_locator.core.profiles.profile_url import profile_url
from swiss_locator.utils.utils import url_with_param


class SwissProfileGenerator(QgsAbstractProfileGenerator):
Expand All @@ -42,14 +43,6 @@ def sourceId(self):
return "swiss-profile"

def __get_profile_from_rest_api(self):
def url_with_param(url, params) -> str:
url = QUrl(url)
q = QUrlQuery(url)
for key, value in params.items():
q.addQueryItem(key, value)
url.setQuery(q)
return url

result = {}
geojson = self.__transformed_curve.asJson(3)
base_url, base_params = profile_url(geojson)
Expand Down
10 changes: 10 additions & 0 deletions swiss_locator/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from qgis.PyQt.QtCore import QUrl, QUrlQuery


def url_with_param(url: str, params: dict) -> str:
url = QUrl(url)
q = QUrlQuery(url)
for key, value in params.items():
q.addQueryItem(key, value)
url.setQuery(q)
return url