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

[Fixes #47] Feature: implement /api/v2/tkeywords endpoint #52

Merged
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
72 changes: 72 additions & 0 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ from geonoderest.users import GeonodeUsersHandler
from geonoderest.geoapps import GeonodeGeoappsHandler
from geonoderest.uploads import GeonodeUploadsHandler
from geonoderest.executionrequest import GeonodeExecutionRequestHandler
from geonoderest.tkeywords import GeonodeThesauriKeywordsRequestHandler
from geonoderest.tkeywordlabels import GeonodeThesauriKeywordLabelsRequestHandler


GEONODECTL_URL_ENV_VAR: str = "GEONODE_API_URL"
GEONODECTL_BASIC_ENV_VAR: str = "GEONODE_API_BASIC_AUTH"
Expand Down Expand Up @@ -650,6 +653,70 @@ To use this tool you have to set the following environment variables before star
type=str, dest="exec_id", help="exec_id of executionrequest to describe ..."
)

#####################################
# THESAURI KEYWORD ARGUMENT PARSING #
#####################################
thesaurikeywords = subparsers.add_parser(
"tkeywords", help="thesaurikeyword commands"
)
thesaurikeywords_subparsers = thesaurikeywords.add_subparsers(
help="geonodectl thesaurikeywords commands", dest="subcommand", required=True
)

# LIST
thesaurikeywords_list = thesaurikeywords_subparsers.add_parser(
"list", help="list thesaurikeywords"
)
thesaurikeywords_list.add_argument(
"--filter",
nargs="*",
action=kwargs_append_action,
dest="filter",
type=str,
help="filter thesaurikeywords requests by key value pairs. E.g. --filter alt_label=soil",
)
# DESCRIBE
thesaurikeywords_describe = thesaurikeywords_subparsers.add_parser(
"describe", help="get thesaurikeyword details"
)
# not fully clean to use pk here, as it is actually keyword but for now ...
thesaurikeywords_describe.add_argument(
type=str, dest="pk", help="keyword of thesaurikeywords to describe ..."
)

#####################################
# THESAURI KEYWORD LABEL ARGUMENT PARSING #
#####################################
thesaurikeywordlabels = subparsers.add_parser(
"tkeywordlabels", help="thesaurikeywordlabel commands"
)
thesaurikeywordlabels_subparsers = thesaurikeywordlabels.add_subparsers(
help="geonodectl thesaurikeywordlabels commands",
dest="subcommand",
required=True,
)

# LIST
thesaurikeywordlabels_list = thesaurikeywordlabels_subparsers.add_parser(
"list", help="list thesaurikeywordlabels"
)
thesaurikeywordlabels_list.add_argument(
"--filter",
nargs="*",
action=kwargs_append_action,
dest="filter",
type=str,
help="filter thesaurikeywordlabels requests by key value pairs. E.g. --filter lang=de label=Abbau",
)
# DESCRIBE
hesaurikeywordlabels_describe = thesaurikeywordlabels_subparsers.add_parser(
"describe", help="get thesaurikeywordlabels details"
)
# not fully clean to use pk here, as it is actually keyword but for now ...
hesaurikeywordlabels_describe.add_argument(
type=str, dest="pk", help="keyword of thesaurikeywordlabels to describe ..."
)

#####################
# END OF ARGPARSING #
#####################
Expand Down Expand Up @@ -688,6 +755,11 @@ To use this tool you have to set the following environment variables before star
g_obj = GeonodeUploadsHandler(env=geonode_env)
case "executionrequest" | "execrequest":
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
case "thesaurikeywords" | "tkeywords":
g_obj = GeonodeThesauriKeywordsRequestHandler(env=geonode_env)
case "thesaurikeywordlabels" | "tkeywordlabels":
g_obj = GeonodeThesauriKeywordLabelsRequestHandler(env=geonode_env)

case _:
raise NotImplemented
g_obj_func = getattr(g_obj, "cmd_" + args.subcommand)
Expand Down
23 changes: 1 addition & 22 deletions geonoderest/executionrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,5 @@ def get(self, exec_id: str, **kwargs) -> Dict:
Returns:
Dict: obj details
"""
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/{exec_id}?page_size={kwargs['page_size']}"
)
r = self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{exec_id}")
return r[self.SINGULAR_RESOURCE_NAME]

def cmd_list(self, **kwargs):
"""show list of geonode obj on the cmdline"""
obj = self.list(**kwargs)
if kwargs["json"]:
print_json(obj)
else:
print_list_on_cmd(obj, self.LIST_CMDOUT_HEADER)

def list(self, **kwargs) -> Dict:
"""returns dict of datasets from geonode

Returns:
Dict: request response
"""
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/?page_size={kwargs['page_size']}"
)
return r[self.JSON_OBJECT_NAME]
1 change: 0 additions & 1 deletion geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,4 @@ def get(self, pk: int, **kwargs) -> Dict:
"""
endpoint = f"{self.ENDPOINT_NAME}/{pk}"
r = self.http_get(endpoint=endpoint)

return r[self.SINGULAR_RESOURCE_NAME]
6 changes: 5 additions & 1 deletion geonoderest/geonodetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def get_key(self, ds: dict):
Returns:
The value associated with the key in the dictionary.
"""
return ds[self.key]
try:
ret = ds[self.key]
except KeyError:
ret = ""
return ret


GeonodeCmdOutputKeys: TypeAlias = List[GeonodeCmdOutObjectKey]
Expand Down
29 changes: 29 additions & 0 deletions geonoderest/tkeywordlabels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Dict, List

from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from geonoderest.geonodeobject import GeonodeObjectHandler


class GeonodeThesauriKeywordLabelsRequestHandler(GeonodeObjectHandler):
ENDPOINT_NAME = "tkeywordlabels"
JSON_OBJECT_NAME = "ThesaurusKeywordLabels"
SINGULAR_RESOURCE_NAME = "ThesaurusKeywordLabels"

LIST_CMDOUT_HEADER: List[GeonodeCmdOutObjectKey] = [
GeonodeCmdOutListKey(key="keyword"), # this only works on ZALF GeoNode backend
GeonodeCmdOutListKey(key="lang"),
GeonodeCmdOutListKey(key="label"),
]

def get(self, pk: int, **kwargs) -> Dict:
"""
get details for a given keyword (identifier)

Args:
pk (int): keyword of the object

Returns:
Dict: obj details
"""
r = self.http_get(endpoint=f"{self.ENDPOINT_NAME}?keyword={pk}")
return r[self.SINGULAR_RESOURCE_NAME]
22 changes: 22 additions & 0 deletions geonoderest/tkeywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Dict, List

from geonoderest.geonodetypes import (
GeonodeCmdOutListKey,
GeonodeCmdOutObjectKey,
GeonodeCmdOutDictKey,
)
from geonoderest.geonodeobject import GeonodeObjectHandler


class GeonodeThesauriKeywordsRequestHandler(GeonodeObjectHandler):
ENDPOINT_NAME = "tkeywords"
JSON_OBJECT_NAME = "tkeywords"
SINGULAR_RESOURCE_NAME = "tkeywords"

LIST_CMDOUT_HEADER: List[GeonodeCmdOutObjectKey] = [
GeonodeCmdOutListKey(key="keyword"), # this only works on ZALF GeoNode backend
GeonodeCmdOutDictKey(key=["thesaurus", "slug"]),
GeonodeCmdOutListKey(key="name"),
GeonodeCmdOutListKey(key="slug"),
GeonodeCmdOutListKey(key="uri"),
]
Loading