Skip to content

Commit

Permalink
Replace from_response() with from_dict() method in each class
Browse files Browse the repository at this point in the history
  • Loading branch information
danduk82 committed Oct 14, 2024
1 parent 8f0cb92 commit ddcee50
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 147 deletions.
23 changes: 13 additions & 10 deletions geoservercloud/geoservercloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def create_wmts(self) -> None:

def get_workspaces(self) -> Workspaces:
response: Response = self.get_request(self.rest_endpoints.workspaces())
workspaces = Workspaces.from_response(response)
workspaces = Workspaces.from_dict(response.json())
return workspaces

def create_workspace(
Expand Down Expand Up @@ -171,18 +171,21 @@ def get_datastores(self, workspace_name: str) -> dict[str, Any]:
Get all datastores for a given workspace
"""
response = self.get_request(self.rest_endpoints.datastores(workspace_name))
return DataStores.from_response(response).datastores
return DataStores.from_dict(response.json()).datastores

def get_postgis_datastore(
self, workspace_name: str, datastore_name: str
) -> dict[str, Any]:
) -> dict[str, Any] | None:
"""
Get a specific datastore
"""
response = self.get_request(
self.rest_endpoints.datastore(workspace_name, datastore_name)
)
return PostGisDataStore.from_response(response)
if response.status_code == 404:
return None
else:
return PostGisDataStore.from_dict(response.json())

def create_pg_datastore(
self,
Expand Down Expand Up @@ -313,23 +316,23 @@ def get_feature_types(
"""
Get all feature types for a given workspace and datastore
"""
featuretypes = FeatureTypes.from_response(
featuretypes = FeatureTypes.from_dict(
self.get_request(
self.rest_endpoints.featuretypes(workspace_name, datastore_name)
)
).json()
)
return featuretypes

# TODO: add a test for this method
def get_feature_type(
self, workspace_name: str, datastore_name: str, feature_type_name: str
) -> dict[str, Any]:
return FeatureType.from_response(
return FeatureType.from_dict(
self.get_request(
self.rest_endpoints.featuretype(
workspace_name, datastore_name, feature_type_name
)
)
).json()
)

def create_feature_type(
Expand Down Expand Up @@ -495,7 +498,7 @@ def get_styles(self, workspace_name: str | None = None) -> dict[str, Any]:
if not workspace_name
else self.rest_endpoints.workspace_styles(workspace_name)
)
styles = Styles.from_response(self.get_request(path)).styles
styles = Styles.from_dict(self.get_request(path).json()).styles
return styles

def get_style(
Expand All @@ -509,7 +512,7 @@ def get_style(
if not workspace_name
else self.rest_endpoints.workspace_style(workspace_name, style)
)
return Style.from_response(self.get_request(path))
return Style.from_dict(self.get_request(path).json())

# TODO: add a create_style method that takes a Style object as input
def create_style_from_file(
Expand Down
21 changes: 9 additions & 12 deletions geoservercloud/models/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,21 @@ def put_payload(self):
return payload

@classmethod
def from_response(cls, response: Response):
if response.status_code == 404:
return None
json_data = response.json()
connection_parameters = cls.parse_connection_parameters(json_data)
def from_dict(cls, content: dict):
connection_parameters = cls.parse_connection_parameters(content)
return cls(
json_data.get("dataStore", {}).get("workspace", {}).get("name", None),
json_data.get("dataStore", {}).get("name", None),
content.get("dataStore", {}).get("workspace", {}).get("name", None),
content.get("dataStore", {}).get("name", None),
connection_parameters,
json_data.get("dataStore", {}).get("type", "PostGIS"),
json_data.get("dataStore", {}).get("enabled", True),
json_data.get("dataStore", {}).get("description", None),
content.get("dataStore", {}).get("type", "PostGIS"),
content.get("dataStore", {}).get("enabled", True),
content.get("dataStore", {}).get("description", None),
)

@classmethod
def parse_connection_parameters(cls, json_data):
def parse_connection_parameters(cls, content):
return KeyDollarListDict(
json_data.get("dataStore", {})
content.get("dataStore", {})
.get("connectionParameters", {})
.get("entry", [])
)
Expand Down
8 changes: 4 additions & 4 deletions geoservercloud/models/datastores.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def datastores(self):
return self._datastores

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
def from_dict(cls, content: dict):
datastores = []
workspace_name = (
json_data.get("dataStores", {}).get("workspace", {}).get("name", None)
content.get("dataStores", {}).get("workspace", {}).get("name", None)
)
for store in json_data.get("dataStores", {}).get("dataStore", []):

for store in content.get("dataStores", {}).get("dataStore", []):
datastores.append(store["name"])
for data_store_name in datastores:
log.debug(f"Name: {data_store_name}")
Expand Down
29 changes: 14 additions & 15 deletions geoservercloud/models/featuretype.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,34 @@ def create_metadata_link(
}

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
def from_dict(cls, content: dict):
try:
abstract = json_data["featureType"]["abstract"]
abstract = content["featureType"]["abstract"]
except KeyError:
abstract = json_data["featureType"]["internationalAbstract"]
abstract = content["featureType"]["internationalAbstract"]
try:
title = json_data["featureType"]["title"]
title = content["featureType"]["title"]
except KeyError:
title = json_data["featureType"]["internationalTitle"]
title = content["featureType"]["internationalTitle"]

return cls(
namespace_name=json_data["featureType"]["namespace"]["name"],
name=json_data["featureType"]["name"],
native_name=json_data["featureType"]["nativeName"],
namespace_name=content["featureType"]["namespace"]["name"],
name=content["featureType"]["name"],
native_name=content["featureType"]["nativeName"],
title=title,
abstract=abstract,
srs=json_data["featureType"]["srs"],
keywords=json_data["featureType"]["keywords"],
attributes=json_data["featureType"].get("attributes", None),
metadata_url=json_data["featureType"]
srs=content["featureType"]["srs"],
keywords=content["featureType"]["keywords"],
attributes=content["featureType"].get("attributes", None),
metadata_url=content["featureType"]
.get("metadataLinks", {})
.get("metadataLink", {})
.get("content"),
metadata_type=json_data["featureType"]
metadata_type=content["featureType"]
.get("metadataLinks", {})
.get("metadataLink", {})
.get("metadataType"),
metadata_format=json_data["featureType"]
metadata_format=content["featureType"]
.get("metadataLinks", {})
.get("metadataLink", {})
.get("type"),
Expand Down
5 changes: 2 additions & 3 deletions geoservercloud/models/featuretypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ def featuretypes(self):
return self._featuretypes

@classmethod
def from_response(cls, response: Response):
def from_dict(cls, content: dict):
featuretypes = []
json_data = response.json()
for featuretype in json_data.get("featureTypes", {}).get("featureType", []):
for featuretype in content.get("featureTypes", {}).get("featureType", []):
featuretypes.append(featuretype["name"])
return cls(featuretypes)

Expand Down
5 changes: 2 additions & 3 deletions geoservercloud/models/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ def post_payload(self):
return self.put_payload()

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
style_data = json_data.get("style", {})
def from_dict(cls, content: dict):
style_data = content.get("style", {})
return cls(
workspace=style_data.get("workspace"),
name=style_data.get("name"),
Expand Down
7 changes: 3 additions & 4 deletions geoservercloud/models/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ def styles(self):
return self._styles

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
def from_dict(cls, content: dict):
styles = []
try:
workspace = json_data["styles"]["workspace"]
workspace = content["styles"]["workspace"]
except KeyError:
workspace = None
try:
for style in json_data.get("styles", {}).get("style", []):
for style in content.get("styles", {}).get("style", []):
styles.append(style["name"])
except AttributeError:
styles = []
Expand Down
8 changes: 3 additions & 5 deletions geoservercloud/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ def post_payload(self):
return self.put_payload()

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
def from_dict(cls, content: dict):
return cls(
json_data.get("workspace", {}).get("name", None),
json_data.get("workspace", {}).get("isolated", False),
content.get("workspace", {}).get("name", None),
content.get("workspace", {}).get("isolated", False),
)
return cls(json_data.get("workspace", {}).get("name", None))

def __repr__(self):
return json.dumps(self.put_payload(), indent=4)
5 changes: 2 additions & 3 deletions geoservercloud/models/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ def workspaces(self):
return self._workspaces

@classmethod
def from_response(cls, response: Response):
json_data = response.json()
def from_dict(cls, content: dict):

workspaces = []
# Map the response to a list of Workspace instances
for ws in json_data.get("workspaces", {}).get("workspace", []):
for ws in content.get("workspaces", {}).get("workspace", []):
workspaces.append(ws["name"])

# Now 'workspaces' is a list of Workspace instances
Expand Down
15 changes: 5 additions & 10 deletions tests/models/test_datastore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from unittest.mock import Mock

import pytest

from geoservercloud.models import (
KeyDollarListDict,
PostGisDataStore,
Expand Down Expand Up @@ -61,9 +57,8 @@ def test_postgisdatastore_post_payload():
assert datastore.post_payload() == datastore.put_payload()


def test_postgisdatastore_from_response(mocker):
mock_response = Mock()
mock_response.json.return_value = {
def test_postgisdatastore_from_dict():
mock_response = {
"dataStore": {
"name": "test_datastore",
"type": "PostGIS",
Expand All @@ -76,7 +71,7 @@ def test_postgisdatastore_from_response(mocker):
}
}

datastore = PostGisDataStore.from_response(mock_response)
datastore = PostGisDataStore.from_dict(mock_response)

assert datastore.data_store_name == "test_datastore"
assert datastore.data_store_type == "PostGIS"
Expand All @@ -86,7 +81,7 @@ def test_postgisdatastore_from_response(mocker):


def test_postgisdatastore_parse_connection_parameters():
json_data = {
content = {
"dataStore": {
"connectionParameters": {
"entry": [
Expand All @@ -97,7 +92,7 @@ def test_postgisdatastore_parse_connection_parameters():
}
}

connection_params = PostGisDataStore.parse_connection_parameters(json_data)
connection_params = PostGisDataStore.parse_connection_parameters(content)

assert isinstance(connection_params, KeyDollarListDict)
assert connection_params["host"] == "localhost"
Expand Down
18 changes: 6 additions & 12 deletions tests/models/test_datastores.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from unittest.mock import Mock

import pytest

from geoservercloud.models import DataStores


Expand All @@ -15,28 +11,26 @@ def test_datastores_initialization():
assert ds.datastores == datastores


def test_datastores_from_response(mocker):
mock_response = Mock()
mock_response.json.return_value = {
def test_datastores_from_dict():
mock_response = {
"dataStores": {
"workspace": {"name": "test_workspace"},
"dataStore": [{"name": "store1"}, {"name": "store2"}],
}
}

ds = DataStores.from_response(mock_response)
ds = DataStores.from_dict(mock_response)

assert ds.workspace_name == "test_workspace"
assert ds.datastores == ["store1", "store2"]


def test_datastores_from_response_empty():
mock_response = Mock()
mock_response.json.return_value = {
def test_datastores_from_dict_empty():
mock_response = {
"dataStores": {"workspace": {"name": "empty_workspace"}, "dataStore": []}
}

ds = DataStores.from_response(mock_response)
ds = DataStores.from_dict(mock_response)

assert ds.workspace_name == "empty_workspace"
assert ds.datastores == []
Expand Down
10 changes: 4 additions & 6 deletions tests/models/test_featuretype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from unittest.mock import Mock

from geoservercloud.models import I18N, FeatureType
from geoservercloud.models import FeatureType


def test_featuretype_initialization():
Expand Down Expand Up @@ -74,9 +73,8 @@ def test_featuretype_create_metadata_link():
assert feature_type.metadataLink == expected_metadata_link


def test_featuretype_from_response():
mock_response = Mock()
mock_response.json.return_value = {
def test_featuretype_from_dict():
mock_response = {
"featureType": {
"namespace": {"name": "test_namespace"},
"name": "test_name",
Expand All @@ -96,7 +94,7 @@ def test_featuretype_from_response():
}
}

feature_type = FeatureType.from_response(mock_response)
feature_type = FeatureType.from_dict(mock_response)

assert feature_type.namespace_name == "test_namespace"
assert feature_type.name == "test_name"
Expand Down
Loading

0 comments on commit ddcee50

Please sign in to comment.