Skip to content

Commit

Permalink
Fixes for ORS checking workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Nov 9, 2024
1 parent 44960da commit 8401ce4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
40 changes: 16 additions & 24 deletions geest/core/tasks/ors_checker.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from typing import Optional
import json

from qgis.core import (
QgsTask,
QgsMessageLog,
Qgis,
QgsNetworkAccessManager,
QgsNetworkReplyContent,
)
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtNetwork import QNetworkRequest, QNetworkReply
from qgis.PyQt.QtCore import pyqtSignal
from geest.core import setting
from geest.core.ors_client import ORSClient


class OrsCheckerTask(QgsTask):
Expand All @@ -23,6 +19,7 @@ def __init__(self, url: str):
self.url = url
self.exception: Optional[Exception] = None
self.is_key_valid = False # Store whether the key is valid or not
self.ors_client = ORSClient("https://api.openrouteservice.org/v2/isochrones")

def run(self):
"""Do the work to validate the ORS API key."""
Expand All @@ -31,26 +28,21 @@ def run(self):
ors_key = setting("ors_key", "")
if not ors_key:
raise ValueError("ORS API key is missing from settings.")

# Prepare the network request with the API key in headers
nam = QgsNetworkAccessManager()
url = QUrl(f"{self.url}/v2/health") # Example endpoint for health check
request = QNetworkRequest(url)
request.setRawHeader(b"Authorization", ors_key.encode("utf-8"))

# Send the request and block until a response is received
reply: QgsNetworkReplyContent = nam.blockingGet(request)

# Check the HTTP status code
if reply.error() != QNetworkReply.NoError:
raise ValueError("Network error occurred while validating ORS API key.")

# Parse the response content to determine if the key is valid
response_content = reply.content().data().decode("utf-8")
response_json = json.loads(response_content)

params = {
"locations": [[8.681495, 49.41461]],
"range": [100, 500], # Distances or times in the list
"range_type": "distance",
}
mode = "foot-walking"
# Make the request to ORS API using ORSClient
response = self.ors_client.make_request(mode, params)
QgsMessageLog.logMessage(
f"ORS API Key Validation Task response: {response}",
tag="Geest",
level=Qgis.Info,
)
# Assuming the response JSON contains a 'status' field
if response_json.get("status") == "ready":
if response.get("type") == "FeatureCollection":
self.is_key_valid = True
QgsMessageLog.logMessage(
"ORS API Key is valid.", tag="Geest", level=Qgis.Info
Expand Down
12 changes: 10 additions & 2 deletions geest/gui/panels/ors_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def initUI(self):
self.banner_label.setPixmap(
QPixmap(resources_path("resources", "geest-banner.png"))
)
self.status_label.setPixmap(
QPixmap(resources_path("resources", "images", "ors-not-configured.png"))
)
self.next_button.clicked.connect(self.on_next_button_clicked)
# Connect the rich text label's linkActivated signal to open URLs in browser
self.description.linkActivated.connect(self.open_link_in_browser)
Expand All @@ -59,9 +62,14 @@ def check_ors_key(self):
def task_completed(self, result):
"""Handle the result of the ORS key check."""
if result:
QMessageBox.information(self, "Success", "ORS API key is valid.")
# Get the icon from the resource_path
self.status_label.setPixmap(
QPixmap(resources_path("resources", "images", "ors-ok.png"))
)
else:
QMessageBox.critical(self, "Error", "Invalid ORS API key.")
self.status_label.setPixmap(
QPixmap(resources_path("resources", "images", "ors-error.png"))
)

def open_link_in_browser(self, url: str):
"""Open the given URL in the user's default web browser using QDesktopServices."""
Expand Down
Binary file added geest/resources/images/ors-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added geest/resources/images/ors-not-configured.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added geest/resources/images/ors-ok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion geest/ui/ors_panel_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="status_label">
<property name="minimumSize">
<size>
<width>128</width>
Expand Down

0 comments on commit 8401ce4

Please sign in to comment.