-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CI - Some refactoring on annotations
- Loading branch information
Showing
6 changed files
with
95 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ mypy | |
mypy-extensions | ||
pipdeptree | ||
xmldiff | ||
Pillow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,60 @@ | |
import json | ||
import xml.etree.ElementTree as ET | ||
|
||
from typing import Dict, Union | ||
|
||
import lxml.etree | ||
|
||
from PIL import Image | ||
from qgis.server import QgsBufferServerResponse | ||
|
||
__copyright__ = 'Copyright 2024, 3Liz' | ||
__license__ = 'GPL version 3' | ||
__email__ = '[email protected]' | ||
|
||
NAMESPACES = { | ||
'xlink': "http://www.w3.org/1999/xlink", | ||
'wms': "http://www.opengis.net/wms", | ||
'wfs': "http://www.opengis.net/wfs", | ||
'wcs': "http://www.opengis.net/wcs", | ||
'ows': "http://www.opengis.net/ows/1.1", | ||
'gml': "http://www.opengis.net/gml", | ||
'xsi': "http://www.w3.org/2001/XMLSchema-instance", | ||
} | ||
|
||
|
||
class OWSResponse: | ||
|
||
def __init__(self, resp: QgsBufferServerResponse) -> None: | ||
self._resp = resp | ||
self._xml = None | ||
|
||
@property | ||
def xml(self) -> 'xml': | ||
if self._xml is None and self._resp.headers().get('Content-Type', '').find('text/xml') == 0: | ||
self._xml = lxml.etree.fromstring(self.content) | ||
return self._xml | ||
|
||
@property | ||
def content(self) -> bytes: | ||
return bytes(self._resp.body()) | ||
|
||
@property | ||
def status_code(self) -> int: | ||
return self._resp.statusCode() | ||
|
||
@property | ||
def headers(self) -> Dict[str, str]: | ||
return self._resp.headers() | ||
|
||
def xpath(self, path: str) -> lxml.etree.Element: | ||
assert self.xml is not None | ||
return self.xml.xpath(path, namespaces=NAMESPACES) | ||
|
||
def xpath_text(self, path: str) -> str: | ||
assert self.xml is not None | ||
return ' '.join(e.text for e in self.xpath(path)) | ||
|
||
|
||
def _build_query_string(params: dict) -> str: | ||
""" Build a query parameter from a dictionary. """ | ||
|
@@ -17,7 +65,9 @@ def _build_query_string(params: dict) -> str: | |
return query_string | ||
|
||
|
||
def _check_request(result, content_type: str = 'application/json', http_code: int = 200): # noqa ANN401 | ||
def _check_request( | ||
result: OWSResponse, content_type: str = 'application/json', http_code: int = 200, | ||
) -> Union[dict, ET.Element, Image.Image]: | ||
""" Check the output and return the content. """ | ||
assert result.status_code == http_code, f'HTTP code {result.status_code}, expected {http_code}' | ||
assert result.headers.get('Content-Type', '').lower().find(content_type) == 0, f'Headers {result.headers}' | ||
|