From 8fc25ea842d76fc8b59de700086c0e52e41b2a74 Mon Sep 17 00:00:00 2001 From: jeafreezy Date: Sun, 15 May 2022 15:39:41 +0100 Subject: [PATCH 1/4] updating request call in Geoserver methods --- geo/Geoserver.py | 140 +++++++++++++++++++++-------------------------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/geo/Geoserver.py b/geo/Geoserver.py index c4d21c3..e8d0927 100644 --- a/geo/Geoserver.py +++ b/geo/Geoserver.py @@ -68,6 +68,8 @@ def _requests(self, method: str, url: str, **kwargs) -> requests.Response: return requests.get(url, auth=(self.username, self.password), **kwargs) elif method == "put": return requests.put(url, auth=(self.username, self.password), **kwargs) + elif method == "delete": + return requests.delete(url, auth=(self.username, self.password), **kwargs) # _______________________________________________________________________________________________ # @@ -82,7 +84,7 @@ def get_manifest(self): """ try: url = "{}/about/manifest.json".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -94,7 +96,7 @@ def get_version(self): """ try: url = "{}/rest/about/version.json".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -106,7 +108,7 @@ def get_status(self): """ try: url = "{}/about/status.json".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -118,7 +120,7 @@ def get_system_status(self): """ try: url = "{}/about/system-status.json".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -134,7 +136,7 @@ def reload(self): """ try: url = "{}/reload".format(self.service_url) - r = requests.post(url, auth=(self.username, self.password)) + r = self._requests("post", url) return "Status code: {}".format(r.status_code) except Exception as e: @@ -177,7 +179,7 @@ def get_datastore(self, store_name: str, workspace: Optional[str] = None): self.service_url, workspace, store_name ) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -195,7 +197,7 @@ def get_datastores(self, workspace: Optional[str] = None): workspace = "default" url = "{}/workspaces/{}/datastores.json".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -220,7 +222,7 @@ def get_coveragestore( url = "{}/workspaces/{}/coveragestores/{}.json".format( self.service_url, workspace, coveragestore_name ) - r = requests.get(url, auth=(self.username, self.password), params=payload) + r = self._requests(method="get", url=url, params=payload) # print("Status code: {}, Get coverage store".format(r.status_code)) return r.json() @@ -237,7 +239,7 @@ def get_coveragestores(self, workspace: str = None): workspace = "default" url = "{}/workspaces/{}/coveragestores".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -291,14 +293,14 @@ def create_coveragestore( r = None try: with open(path, "rb") as f: - r = requests.put( - url, data=f, auth=(self.username, self.password), headers=headers - ) + r = self._requests(method="put", url=url, data=f, headers=headers) - if r.status_code != 201: - return "{}: The coveragestore can not be created!".format(r.status_code) - else: - return "The coveragestore has been created successfully!" + if r.status_code != 201: + return "{}: The coveragestore can not be created!".format( + r.status_code + ) + else: + return "The coveragestore has been created successfully!" except Exception as e: return "Error: {}".format(e) @@ -356,11 +358,8 @@ def publish_time_dimension_to_coveragestore( r = None try: - r = requests.put( - url, - data=time_dimension_data, - auth=(self.username, self.password), - headers=headers, + r = self._requests( + method="put", url=url, data=time_dimension_data, headers=headers ) if r.status_code not in [200, 201]: @@ -388,7 +387,7 @@ def get_layer(self, layer_name: str, workspace: Optional[str] = None): self.service_url, workspace, layer_name ) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -404,7 +403,7 @@ def get_layers(self, workspace: Optional[str] = None): if workspace is not None: url = "{}/workspaces/{}/layers".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password)) + r = r = self._requests("get", url) return r.json() except Exception as e: @@ -427,9 +426,7 @@ def delete_layer(self, layer_name: str, workspace: Optional[str] = None): if workspace is None: url = "{}/layers/{}".format(self.service_url, layer_name) - r = requests.delete( - url, auth=(self.username, self.password), params=payload - ) + r = self._requests(method="delete", url=url, params=payload) if r.status_code == 200: return "Status code: {}, delete layer".format(r.status_code) @@ -458,7 +455,7 @@ def get_layergroups(self, workspace: Optional[str] = None): if workspace is not None: url = "{}/workspaces/{}/layergroups".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -475,7 +472,7 @@ def get_layergroup(self, layer_name: str, workspace: Optional[str] = None): self.service_url, workspace, layer_name ) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -646,7 +643,7 @@ def get_style(self, style_name, workspace: Optional[str] = None): self.service_url, workspace, style_name ) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -661,7 +658,7 @@ def get_styles(self, workspace: Optional[str] = None): if workspace is not None: url = "{}/workspaces/{}/styles.json".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -716,13 +713,7 @@ def upload_style( r = None try: - r = requests.post( - url, - data=style_xml, - auth=(self.username, self.password), - headers=headers, - ) - + r = self._requests(method="post", url=url, data=style_xml, headers=headers) with open(path, "rb") as f: r_sld = requests.put( url + "/" + name, @@ -801,10 +792,10 @@ def create_coveragestyle( r = None try: - r = requests.post( + r = self._requests( + "post", url, data=style_xml, - auth=(self.username, self.password), headers=headers, ) @@ -873,18 +864,18 @@ def create_catagorized_featurestyle( r = None try: - r = requests.post( + r = self._requests( + "post", url, data=style_xml, - auth=(self.username, self.password), headers=headers, ) with open("style.sld", "rb") as f: - r_sld = requests.put( + r_sld = self._requests( + "put", url + "/" + style_name, data=f.read(), - auth=(self.username, self.password), headers=header_sld, ) if r_sld.status_code not in [200, 201]: @@ -940,18 +931,18 @@ def create_outline_featurestyle( r = None try: - r = requests.post( + r = self._requests( + "post", url, data=style_xml, - auth=(self.username, self.password), headers=headers, ) with open("style.sld", "rb") as f: - r_sld = requests.put( + r_sld = self._requests( + "put", url + "/" + style_name, data=f.read(), - auth=(self.username, self.password), headers=header_sld, ) if r_sld.status_code not in [200, 201]: @@ -1015,18 +1006,18 @@ def create_classified_featurestyle( r = None try: - r = requests.post( + r = self._requests( + "post", url, data=style_xml, - auth=(self.username, self.password), headers=headers, ) with open("style.sld", "rb") as f: - r_sld = requests.put( + r_sld = self._requests( + "put", url + "/" + style_name, data=f.read(), - auth=(self.username, self.password), headers=header_sld, ) if r_sld.status_code not in [200, 201]: @@ -1073,10 +1064,10 @@ def publish_style( r = None try: - r = requests.put( + r = self._requests( + "put", url, data=style_xml, - auth=(self.username, self.password), headers=headers, ) @@ -1101,9 +1092,7 @@ def delete_style(self, style_name: str, workspace: Optional[str] = None): if workspace is None: url = "{}/styles/{}".format(self.service_url, style_name) - r = requests.delete( - url, auth=(self.username, self.password), params=payload - ) + r = self._requests("delete", url, params=payload) if r.status_code == 200: return "Status code: {}, delete style".format(r.status_code) @@ -1126,7 +1115,7 @@ def get_default_workspace(self): """ try: url = "{}/workspaces/default".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -1140,7 +1129,7 @@ def get_workspace(self, workspace): try: payload = {"recurse": "true"} url = "{}/workspaces/{}.json".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password), params=payload) + r = self._requests("get", url, params=payload) if r.status_code == 200: return r.json() else: @@ -1155,7 +1144,7 @@ def get_workspaces(self): """ try: url = "{}/workspaces".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) + r = self._requests("get", url) return r.json() except Exception as e: @@ -1169,10 +1158,10 @@ def set_default_workspace(self, workspace: str): url = "{}/workspaces/default".format(self.service_url) data = "{}".format(workspace) print(url, data) - r = requests.put( + r = self._requests( + "put", url, - data, - auth=(self.username, self.password), + data=data, headers={"content-type": "text/xml"}, ) @@ -1194,9 +1183,7 @@ def create_workspace(self, workspace: str): url = "{}/workspaces".format(self.service_url) data = "{}".format(workspace) headers = {"content-type": "text/xml"} - r = requests.post( - url, data, auth=(self.username, self.password), headers=headers - ) + r = self._requests("post", url, data=data, headers=headers) if r.status_code == 201: return "{} Workspace {} created!".format(r.status_code, workspace) @@ -1221,9 +1208,7 @@ def delete_workspace(self, workspace: str): try: payload = {"recurse": "true"} url = "{}/workspaces/{}".format(self.service_url, workspace) - r = requests.delete( - url, auth=(self.username, self.password), params=payload - ) + r = self._requests("delete", url, params=payload) if r.status_code == 200: return "Status code: {}, delete workspace".format(r.status_code) @@ -1388,10 +1373,10 @@ def create_featurestore( self.service_url, workspace, store_name ) - r = requests.put( + r = self._requests( + "put", url, data=database_connection, - auth=(self.username, self.password), headers=headers, ) @@ -1400,10 +1385,10 @@ def create_featurestore( r.status_code, r.content ) else: - r = requests.post( + r = self._requests( + "post", url, data=database_connection, - auth=(self.username, self.password), headers=headers, ) @@ -1461,15 +1446,11 @@ def create_datastore( url = "{}/workspaces/{}/datastores/{}".format( self.service_url, workspace, name ) - r = requests.put( - url, data, auth=(self.username, self.password), headers=headers - ) + r = self._requests("put", url, data=data, headers=headers) else: url = "{}/workspaces/{}/datastores".format(self.service_url, workspace) - r = requests.post( - url, data, auth=(self.username, self.password), headers=headers - ) + r = self._requests("post", url, data=data, headers=headers) if r.status_code in [200, 201]: return "Data store created/updated successfully" @@ -2076,3 +2057,6 @@ def delete_usergroup(self, group: str, service=None): except Exception as e: return "Error: {}".format(e) + + +print(Geoserver().get_manifest()) From 0c788f6b7be502fe1e59cf4e890d6d52a3b30e06 Mon Sep 17 00:00:00 2001 From: jeafreezy Date: Sun, 15 May 2022 15:40:42 +0100 Subject: [PATCH 2/4] merging with master branch --- geo/Geoserver.py | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/geo/Geoserver.py b/geo/Geoserver.py index e8d0927..24600d0 100644 --- a/geo/Geoserver.py +++ b/geo/Geoserver.py @@ -1305,35 +1305,35 @@ def create_featurestore( database_connection = """ - {0} - {1} + {} + {} - {2} - {3} - {4} - {5} - {6} + {} + {} + {} + {} + {} postgis - {7} - {8} - {9} - {10} - {11} - {12} - {13} - {14} - {15} - {16} - {17} - {18} - {19} - {20} - {21} - {22} - {23} - {24} - {25} - {26} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} + {} """.format( From 394a7fa714236b954434551aa3d7c44241d25319 Mon Sep 17 00:00:00 2001 From: jeafreezy Date: Sun, 15 May 2022 22:34:43 +0100 Subject: [PATCH 3/4] added functions for update layergroup and delete layergroup --- docs/source/change_log.rst | 3 + geo/Geoserver.py | 412 +++++++++++++++++++++++++------------ 2 files changed, 283 insertions(+), 132 deletions(-) diff --git a/docs/source/change_log.rst b/docs/source/change_log.rst index e68b944..aacfeb6 100644 --- a/docs/source/change_log.rst +++ b/docs/source/change_log.rst @@ -14,6 +14,9 @@ Change Log * Removed ``key_column`` parameter and added ``srid`` parameter (coordinate system of the layer, default is 4326) from ``publish_featurestore_sqlview`` function * Solved the Bug `#73 `_ and `#69 `_ * ``create_layergroup`` function added +* ``update_layergroup`` function added +* ``delete_layergroup`` function added +* Added layer and workspace checks to layergroup methods ``[V2.1.2 - 2021-10-14]`` diff --git a/geo/Geoserver.py b/geo/Geoserver.py index 524f85b..922ec4b 100644 --- a/geo/Geoserver.py +++ b/geo/Geoserver.py @@ -12,7 +12,7 @@ from .supports import prepare_zip_file -# call back class for read the data +# call back class for reading the data class DataProvider(object): def __init__(self, data): self.data = data @@ -158,6 +158,124 @@ def reset(self): except Exception as e: return "reload error: {}".format(e) + # _______________________________________________________________________________________________ + # + # WORKSPACES + # _______________________________________________________________________________________________ + # + + def get_default_workspace(self): + """ + Returns the default workspace. + """ + try: + url = "{}/rest/workspaces/default".format(self.service_url) + r = requests.get(url, auth=(self.username, self.password)) + return r.json() + + except Exception as e: + return "get_default_workspace error: {}".format(e) + + def get_workspace(self, workspace): + """ + get name workspace if exist + Example: curl -v -u admin:admin -XGET -H "Accept: text/xml" http://localhost:8080/geoserver/rest/workspaces/acme.xml + """ + try: + payload = {"recurse": "true"} + url = "{}/rest/workspaces/{}.json".format(self.service_url, workspace) + r = requests.get(url, auth=(self.username, self.password), params=payload) + if r.status_code == 200: + return r.json() + else: + return None + + except Exception as e: + return "Error: {}".format(e) + + def get_workspaces(self): + """ + Returns all the workspaces. + """ + try: + url = "{}/rest/workspaces".format(self.service_url) + r = requests.get(url, auth=(self.username, self.password)) + return r.json() + + except Exception as e: + return "get_workspaces error: {}".format(e) + + def set_default_workspace(self, workspace: str): + """ + Set the default workspace. + """ + try: + url = "{}/rest/workspaces/default".format(self.service_url) + data = "{}".format(workspace) + print(url, data) + r = self._requests( + "put", + url, + data=data, + headers={"content-type": "text/xml"}, + ) + + if r.status_code == 200: + return "Status code: {}, default workspace {} set!".format( + r.status_code, workspace + ) + + except Exception as e: + return "reload error: {}".format(e) + + def create_workspace(self, workspace: str): + """ + Create a new workspace in geoserver. + + The geoserver workspace url will be same as the name of the workspace. + """ + try: + url = "{}/rest/workspaces".format(self.service_url) + data = "{}".format(workspace) + headers = {"content-type": "text/xml"} + r = self._requests("post", url, data=data, headers=headers) + + if r.status_code == 201: + return "{} Workspace {} created!".format(r.status_code, workspace) + + if r.status_code == 401: + raise Exception("The workspace already exist") + + else: + raise Exception("The workspace can not be created") + + except Exception as e: + return "Error: {}".format(e) + + def delete_workspace(self, workspace: str): + """ + + Parameters + ---------- + workspace : str + + """ + try: + payload = {"recurse": "true"} + url = "{}/rest/workspaces/{}".format(self.service_url, workspace) + r = requests.delete( + url, auth=(self.username, self.password), params=payload + ) + + if r.status_code == 200: + return "Status code: {}, delete workspace".format(r.status_code) + + else: + raise Exception("Error: {} {}".format(r.status_code, r.content)) + + except Exception as e: + return "Error: {}".format(e) + # _______________________________________________________________________________________________ # # DATASTORES @@ -392,7 +510,10 @@ def get_layer(self, layer_name: str, workspace: Optional[str] = None): ) r = self._requests("get", url) - return r.json() + if r.status_code in [200, 201]: + return r.json() + else: + return None except Exception as e: return "get_layer error: {}".format(e) @@ -479,14 +600,17 @@ def get_layergroup(self, layer_name: str, workspace: Optional[str] = None): ) r = self._requests("get", url) - return r.json() + if r.status_code in [200, 201]: + return r.json() + else: + return None except Exception as e: return "get_layer error: {}".format(e) def create_layergroup( self, - name: str = "geoserver-rest layergroup", + name: str = "geoserver-rest-layergroup", mode: str = "single", title: str = "geoserver-rest layer group", abstract_text: str = "A new layergroup created with geoserver-rest python package", @@ -535,6 +659,9 @@ def create_layergroup( if workspace: assert isinstance(workspace, str), "Workspace must be of type String:''" + # check if the workspace is valid in Geoserver + if self.get_workspace(workspace) is None: + raise Exception("Workspace is not valid in Geoserver Instance") supported_modes: Set = { "single", @@ -556,19 +683,37 @@ def create_layergroup( raise Exception( f"Format not supported. Acceptable formats are : {supported_formats}" ) + # check if it already exist in Geoserver + if self.get_layergroup(name) is not None: + raise Exception(f"Layergroup: {name} already exist in Geoserver instance") + if len(layers) == 0: raise Exception("No layer provided!") + else: + for layer in layers: + # check if it is valid in geoserver + + if ( + self.get_layer( + layer_name=layer, + workspace=workspace if workspace is not None else None, + ) + is not None + ): + ... + else: + raise Exception( + f"Layer: {layer} is not a valid layer in the Geoserver instance" + ) skeleton = "" if workspace: - # TODO: check if it is valid in geoserver skeleton += f"{workspace}" - # metadata structure = [{about:"",content_url:""},{...}] metadata_xml_list = [] - if len(metadata) > 1: + if len(metadata) >= 1: for meta in metadata: metadata_about = meta.get("about") metadata_content_url = meta.get("content_url") @@ -585,8 +730,6 @@ def create_layergroup( metadata_xml = f"{''.join(['{}']*len(metadata_xml_list)).format(*metadata_xml_list)}" skeleton += metadata_xml - - # TODO: check if Layer exist in Geoserver layers_xml_list: List[str] = [] for layer in layers: @@ -602,7 +745,7 @@ def create_layergroup( layers_xml: str = f"{''.join(['{}']*len(layers)).format(*layers_xml_list)}" skeleton += layers_xml - if len(keywords) > 1: + if len(keywords) >= 1: keyword_xml_list: List[str] = [ f"{keyword}" for keyword in keywords @@ -627,11 +770,137 @@ def create_layergroup( ) if response.status_code in [200, 201]: - layergroup_url = f"{self.service_url}/layergroups/{name}.{formats}" + layergroup_url = f"{self.service_url}/rest/layergroups/{name}.{formats}" return f"layergroup created successfully! Layergroup link: {layergroup_url}" else: return "Error creating layergroup" + def update_layergroup( + self, + layergroup_name, + title: Optional[str] = None, + abstract_text: Optional[str] = None, + formats: str = "html", + metadata: List[dict] = [], + keywords: List[str] = [], + ) -> str: + """ + Updates a Layergroup. + + Parameters + ---------- + layergroup_name: str, required + mode : str, optional + title : str, optional + abstract_text : str, optional + formats : str, optional + metadata : list, optional + keywords : list, optional + + """ + + # check if layergroup is valid in Geoserver + + if self.get_layergroup(layer_name=layergroup_name) is None: + raise Exception( + f"Layer group: {layergroup_name} is not a valid layer group in the Geoserver instance" + ) + if title is not None: + assert isinstance(title, str), "Title must be of type String:''" + if abstract_text is not None: + assert isinstance( + abstract_text, str + ), "Abstract text must be of type String:''" + assert isinstance(formats, str), "Format must be of type String:''" + assert isinstance( + metadata, list + ), "Metadata must be of type List of dict:[{'about':'geoserver rest data metadata','content_url':'lint to content url'}]" + assert isinstance( + keywords, list + ), "Keywords must be of type List:['keyword1','keyword2'...]" + + supported_formats: Set = {"html", "json", "xml"} + + if formats.lower() != "html" and formats.lower() not in supported_formats: + + raise Exception( + f"Format not supported. Acceptable formats are : {supported_formats}" + ) + + skeleton = "" + + if title: + skeleton += f"{title}" + if abstract_text: + skeleton += f"{abstract_text}" + + metadata_xml_list = [] + + if len(metadata) >= 1: + for meta in metadata: + metadata_about = meta.get("about") + metadata_content_url = meta.get("content_url") + metadata_xml_list.append( + f""" + + text/plain + {metadata_about} + ISO19115:2003 + {metadata_content_url} + + """ + ) + + metadata_xml = f"{''.join(['{}']*len(metadata_xml_list)).format(*metadata_xml_list)}" + skeleton += metadata_xml + + if len(keywords) >= 1: + + keyword_xml_list: List[str] = [ + f"{keyword}" for keyword in keywords + ] + keywords_xml: str = f"{''.join(['{}']*len(keywords)).format(*keyword_xml_list)}" + skeleton += keywords_xml + + data = f""" + + {skeleton} + + """ + + url = f"{self.service_url}/rest/layergroups/{layergroup_name}" + + response = self._requests( + method="put", + url=url, + data=data, + headers={"content-type": "text/xml", "accept": "application/xml"}, + ) + if response.status_code in [200, 201]: + layergroup_url = ( + f"{self.service_url}/rest/layergroups/{layergroup_name}.{formats}" + ) + return f"layergroup updated successfully! Layergroup link: {layergroup_url}" + else: + return "Error updating layergroup" + + def delete_layergroup( + self, + layergroup_name: str, + ) -> str: + if self.get_layergroup(layer_name=layergroup_name) is None: + raise Exception( + f"Layer group: {layergroup_name} is not a valid layer group in the Geoserver instance" + ) + + url = f"{self.service_url}/rest/layergroups/{layergroup_name}" + + response = self._requests(url=url, method="delete") + if response.status_code in [200, 201]: + return "Layer group deleted successfully" + else: + return "Error deleting layer group" + # _______________________________________________________________________________________________ # # STYLES @@ -1111,124 +1380,6 @@ def delete_style(self, style_name: str, workspace: Optional[str] = None): except Exception as e: return "Error: {}".format(e) - # _______________________________________________________________________________________________ - # - # WORKSPACES - # _______________________________________________________________________________________________ - # - - def get_default_workspace(self): - """ - Returns the default workspace. - """ - try: - url = "{}/rest/workspaces/default".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) - return r.json() - - except Exception as e: - return "get_default_workspace error: {}".format(e) - - def get_workspace(self, workspace): - """ - get name workspace if exist - Example: curl -v -u admin:admin -XGET -H "Accept: text/xml" http://localhost:8080/geoserver/rest/workspaces/acme.xml - """ - try: - payload = {"recurse": "true"} - url = "{}/rest/workspaces/{}.json".format(self.service_url, workspace) - r = requests.get(url, auth=(self.username, self.password), params=payload) - if r.status_code == 200: - return r.json() - else: - return None - - except Exception as e: - return "Error: {}".format(e) - - def get_workspaces(self): - """ - Returns all the workspaces. - """ - try: - url = "{}/rest/workspaces".format(self.service_url) - r = requests.get(url, auth=(self.username, self.password)) - return r.json() - - except Exception as e: - return "get_workspaces error: {}".format(e) - - def set_default_workspace(self, workspace: str): - """ - Set the default workspace. - """ - try: - url = "{}/rest/workspaces/default".format(self.service_url) - data = "{}".format(workspace) - print(url, data) - r = self._requests( - "put", - url, - data=data, - headers={"content-type": "text/xml"}, - ) - - if r.status_code == 200: - return "Status code: {}, default workspace {} set!".format( - r.status_code, workspace - ) - - except Exception as e: - return "reload error: {}".format(e) - - def create_workspace(self, workspace: str): - """ - Create a new workspace in geoserver. - - The geoserver workspace url will be same as the name of the workspace. - """ - try: - url = "{}/rest/workspaces".format(self.service_url) - data = "{}".format(workspace) - headers = {"content-type": "text/xml"} - r = self._requests("post", url, data=data, headers=headers) - - if r.status_code == 201: - return "{} Workspace {} created!".format(r.status_code, workspace) - - if r.status_code == 401: - raise Exception("The workspace already exist") - - else: - raise Exception("The workspace can not be created") - - except Exception as e: - return "Error: {}".format(e) - - def delete_workspace(self, workspace: str): - """ - - Parameters - ---------- - workspace : str - - """ - try: - payload = {"recurse": "true"} - url = "{}/rest/workspaces/{}".format(self.service_url, workspace) - r = requests.delete( - url, auth=(self.username, self.password), params=payload - ) - - if r.status_code == 200: - return "Status code: {}, delete workspace".format(r.status_code) - - else: - raise Exception("Error: {} {}".format(r.status_code, r.content)) - - except Exception as e: - return "Error: {}".format(e) - # _______________________________________________________________________________________________ # # FEATURES AND DATASTORES @@ -2071,6 +2222,3 @@ def delete_usergroup(self, group: str, service=None): except Exception as e: return "Error: {}".format(e) - - -print(Geoserver().get_manifest()) From b0ac75e5d06009f68510e7ba346db98dee898514 Mon Sep 17 00:00:00 2001 From: jeafreezy Date: Sun, 15 May 2022 22:50:56 +0100 Subject: [PATCH 4/4] returned order to database connections --- geo/Geoserver.py | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/geo/Geoserver.py b/geo/Geoserver.py index 922ec4b..4eb831c 100644 --- a/geo/Geoserver.py +++ b/geo/Geoserver.py @@ -1466,35 +1466,35 @@ def create_featurestore( database_connection = """ - {} - {} + {0} + {1} - {} - {} - {} - {} - {} + {2} + {3} + {4} + {5} + {6} postgis - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} - {} + {7} + {8} + {9} + {10} + {11} + {12} + {13} + {14} + {15} + {16} + {17} + {18} + {19} + {20} + {21} + {22} + {23} + {24} + {25} + {26} """.format(