diff --git a/geonoderest/geonodeobject.py b/geonoderest/geonodeobject.py index dc8e1d2..6edb791 100644 --- a/geonoderest/geonodeobject.py +++ b/geonoderest/geonodeobject.py @@ -22,6 +22,19 @@ class GeonodeObjectHandler(GeonodeRest): ENDPOINT_NAME: str = "" SINGULAR_RESOURCE_NAME: str = "" + def __handle_http_params__(self, params: Dict, kwargs: Dict) -> Dict: + if "page_size" in kwargs: + params["page_size"] = kwargs["page_size"] + if "page" in kwargs: + params["page"] = kwargs["page"] + + if "filter" in kwargs and kwargs["filter"] is not None: + for field, value in kwargs["filter"].items(): + field = "filter{" + field + "}" + params[field] = value + + return params + def cmd_list(self, **kwargs): """show list of geonode obj on the cmdline""" obj = self.list(**kwargs) @@ -37,17 +50,8 @@ def list(self, **kwargs) -> Dict: Dict: request response """ endpoint = f"{self.ENDPOINT_NAME}/" - params = {} - - if "page_size" in kwargs: - params["page_size"] = kwargs["page_size"] - if "page" in kwargs: - params["page"] = kwargs["page"] - if "filter" in kwargs and kwargs["filter"] is not None: - for field, value in kwargs["filter"].items(): - field = "filter{" + field + "}" - params[field] = value + params = self.__handle_http_params__({}, kwargs) r = self.http_get(endpoint=endpoint, params=params) return r[self.JSON_OBJECT_NAME] diff --git a/geonoderest/rest.py b/geonoderest/rest.py index 67bbd1c..3251ed2 100644 --- a/geonoderest/rest.py +++ b/geonoderest/rest.py @@ -181,6 +181,8 @@ def http_delete(self, endpoint: str, params: Dict = {}) -> Dict: try: r = requests.delete(url, headers=self.header, verify=self.verify) r.raise_for_status() + if r.status_code in [204]: + return "" except requests.exceptions.HTTPError as err: raise SystemExit(err) return r.json() diff --git a/geonoderest/users.py b/geonoderest/users.py index 6c54ef8..3714353 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -75,19 +75,19 @@ def get( "cannot handle user_resources and user_groups True at the same time ..." ) r: Dict + params = self.__handle_http_params__({}, kwargs) + if user_groups is True: - r = self.http_get( - endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups?page_size={kwargs['page_size']}&page={kwargs['page']}" - ) + r = self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups") return r elif user_resources is True: - endpoint = f"{GeonodeResourceHandler.ENDPOINT_NAME}?page_size={kwargs['page_size']}&page={kwargs['page']}" - endpoint += "&filter{owner.pk}=" + str(pk) - r = self.http_get(endpoint=endpoint) + endpoint = f"{GeonodeResourceHandler.ENDPOINT_NAME}" + r = self.http_get(endpoint=endpoint, params=params) return r else: r = self.http_get( - endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}&page={kwargs['page']}" + endpoint=f"{self.ENDPOINT_NAME}/{pk}", + params=params, ) return r[self.SINGULAR_RESOURCE_NAME]