Skip to content

Commit

Permalink
Use the current /datasets API endpoint (GeoNode#178)
Browse files Browse the repository at this point in the history
* Use the current `/datasets` API endpoint

Additionally, use the newer `subtype` API field in order to detect a dataset's layer type

* Adapt tests

* Adapt tests
  • Loading branch information
Ricardo Garcia Silva authored Nov 22, 2021
1 parent 09b13f6 commit ffb1e50
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 611 deletions.
74 changes: 42 additions & 32 deletions src/qgis_geonode/apiclient/apiv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_search_result_identifier(
def get_layers_url_endpoint(
self, search_params: models.GeonodeApiSearchParameters
) -> QtCore.QUrl:
url = QtCore.QUrl(f"{self.api_url}/layers/")
url = QtCore.QUrl(f"{self.api_url}/datasets/")
query = self._build_search_query(search_params)
url.setQuery(query.query())
return url
Expand Down Expand Up @@ -117,9 +117,9 @@ def _build_search_query(
if is_vector and is_raster:
pass
elif is_vector:
query.addQueryItem("filter{storeType}", "dataStore")
query.addQueryItem("filter{subtype}", "vector")
elif is_raster:
query.addQueryItem("filter{storeType}", "coverageStore")
query.addQueryItem("filter{subtype}", "raster")
else:
raise NotImplementedError
if search_params.ordering_field is not None:
Expand All @@ -131,10 +131,10 @@ def _build_search_query(
return query

def get_layer_detail_url_endpoint(self, id_: int) -> QtCore.QUrl:
return QtCore.QUrl(f"{self.api_url}/layers/{id_}/")
return QtCore.QUrl(f"{self.api_url}/datasets/{id_}/")

def get_layer_styles_url_endpoint(self, layer_id: int):
return QtCore.QUrl(f"{self.api_url}/layers/{layer_id}/styles/")
return QtCore.QUrl(f"{self.api_url}/datasets/{layer_id}/styles/")

def get_maps_url_endpoint(
self,
Expand All @@ -150,14 +150,16 @@ def get_layer_detail_from_brief_resource(
):
self.get_layer_detail(brief_resource.pk)

def deserialize_response_contents(self, contents: QtCore.QByteArray) -> typing.Dict:
def deserialize_response_contents(
self, contents: QtCore.QByteArray
) -> typing.Optional[typing.Union[typing.List, typing.Dict]]:
decoded_contents: str = contents.data().decode()
try:
contents = json.loads(decoded_contents)
except json.JSONDecodeError as exc:
log(f"decoded_contents: {decoded_contents}")
log(exc, debug=False)
contents = {}
contents = None
return contents

#
Expand Down Expand Up @@ -192,30 +194,36 @@ def handle_layer_list(
self.network_fetcher_task.reply_content
)
layers = []
for item in deserialized.get("layers", []):
try:
brief_resource = get_brief_geonode_resource(
item, self.base_url, self.auth_config
)
except ValueError:
log(f"Could not parse {item!r} into a valid item")
else:
layers.append(brief_resource)
pagination_info = models.GeoNodePaginationInfo(
total_records=deserialized.get("total") or 0,
current_page=deserialized.get("page") or 1,
page_size=deserialized.get("page_size") or 0,
)
if deserialized is not None:
for item in deserialized.get("datasets", []):
try:
brief_resource = get_brief_geonode_resource(
item, self.base_url, self.auth_config
)
except ValueError:
log(f"Could not parse {item!r} into a valid item")
else:
layers.append(brief_resource)
pagination_info = models.GeoNodePaginationInfo(
total_records=deserialized.get("total") or 0,
current_page=deserialized.get("page") or 1,
page_size=deserialized.get("page_size") or 0,
)
else:
pagination_info = models.GeoNodePaginationInfo(
total_records=0, current_page=1, page_size=0
)
self.layer_list_received.emit(layers, pagination_info)

def handle_layer_detail(self):
deserialized = self.deserialize_response_contents(
self.network_fetcher_task.reply_content
)
layer = get_geonode_resource(
deserialized["layer"], self.base_url, self.auth_config
)
self.layer_detail_received.emit(layer)
if deserialized is not None:
layer = get_geonode_resource(
deserialized["dataset"], self.base_url, self.auth_config
)
self.layer_detail_received.emit(layer)

def handle_layer_style_list(self):
deserialized = self.deserialize_response_contents(
Expand Down Expand Up @@ -321,8 +329,10 @@ def _get_common_model_fields(
"spatial_extent": _get_spatial_extent(deserialized_resource["bbox_polygon"]),
"crs": QgsCoordinateReferenceSystem(deserialized_resource["srid"]),
"thumbnail_url": deserialized_resource["thumbnail_url"],
"api_url": (f"{geonode_base_url}/api/v2/layers/{deserialized_resource['pk']}"),
"gui_url": f"{geonode_base_url}{deserialized_resource['detail_url']}",
"api_url": (
f"{geonode_base_url}/api/v2/datasets/{deserialized_resource['pk']}"
),
"gui_url": deserialized_resource["detail_url"],
"published_date": _get_published_date(deserialized_resource),
"temporal_extent": _get_temporal_extent(deserialized_resource),
"keywords": [k["name"] for k in deserialized_resource.get("keywords", [])],
Expand All @@ -346,14 +356,14 @@ def get_brief_geonode_style(deserialized_style: typing.Dict, geonode_base_url: s
def _get_resource_type(
payload: typing.Dict,
) -> typing.Optional[models.GeonodeResourceType]:
resource_type = payload["resource_type"]
resource_type = payload.get("resource_type")
if resource_type == "map":
result = models.GeonodeResourceType.MAP
elif resource_type == "layer":
elif resource_type == "dataset":
result = {
"coverageStore": models.GeonodeResourceType.RASTER_LAYER,
"dataStore": models.GeonodeResourceType.VECTOR_LAYER,
}.get(payload.get("storeType"))
"raster": models.GeonodeResourceType.RASTER_LAYER,
"vector": models.GeonodeResourceType.VECTOR_LAYER,
}.get(payload.get("subtype"))
else:
result = None
return result
Expand Down
14 changes: 5 additions & 9 deletions test/_mock_geonode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
ROOT = Path(__file__).parent / "_mock_geonode_data"


@geonode_flask_app.route("/api/v2/layers/")
@geonode_flask_app.route("/api/v2/datasets/")
def _mock_layer_list():
query_string = urllib.parse.unquote(
request.query_string.decode("utf-8")
)
query_string = urllib.parse.unquote(request.query_string.decode("utf-8"))
if "filter" in query_string:
pattern = re.compile("filter(.*)")
match = pattern.search(query_string)
Expand All @@ -34,15 +32,15 @@ def _mock_layer_list():
return result


@geonode_flask_app.route("/api/v2/layers/<pk>/")
@geonode_flask_app.route("/api/v2/datasets/<pk>/")
def _mock_layer_details(pk):
data_path = ROOT / "layer_detail_response1.json"
with data_path.open() as fh:
result = json.load(fh)
return result


@geonode_flask_app.route("/api/v2/layers/<layer_id>/styles/")
@geonode_flask_app.route("/api/v2/datasets/<layer_id>/styles/")
def _mock_layer_styles(layer_id):
data_path = ROOT / "layer_style_list_response1.json"
with data_path.open() as fh:
Expand All @@ -52,9 +50,7 @@ def _mock_layer_styles(layer_id):

@geonode_flask_app.route("/api/v2/maps/")
def _mock_map_list():
query_string = urllib.parse.unquote(
request.query_string.decode("utf-8")
)
query_string = urllib.parse.unquote(request.query_string.decode("utf-8"))
if "filter" in query_string:
pattern = re.compile("filter(.*)")
match = pattern.search(query_string)
Expand Down
4 changes: 2 additions & 2 deletions test/_mock_geonode_data/layer_detail_response1.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"layer": {
"dataset": {
"pk": "184",
"uuid": "818898b8-5c1a-11eb-b604-0242ac150007",
"name": "TEMPERATURASMINENERO2030",
"workspace": "geonode",
"store": "TEMPERATURASMINENERO2030",
"storeType": "coverageStore",
"subtype": "raster",
"charset": "UTF-8",
"is_mosaic": false,
"has_time": false,
Expand Down
Loading

0 comments on commit ffb1e50

Please sign in to comment.