def delete_granule(self, coverage, store, granule_id, workspace=None): """Deletes a granule of an existing imagemosaic""" workspace_name = workspace if isinstance(store, basestring): store_name = store else: store_name = store.name workspace_name = store.workspace.name if workspace_name is None: raise ValueError("Must specify workspace") cs_url = url(self.service_url, [ "workspaces", workspace_name, "coveragestores", store_name, "coverages", coverage, "index/granules", granule_id, ".json" ]) # DELETE /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules/<granule_id>.json headers = { "Content-type": "application/json", "Accept": "application/json" } response = self.request(method='delete', url=cs_url, headers=headers) if response.status_code != 200: raise FailedRequestError('{0} - "{1}"'.format( response.status_code, response.text)) self._cache.clear() return "Deleted granule"
def get_xml(self, rest_url): logger.debug("GET %s", rest_url) cached_response = self._cache.get(rest_url) def is_valid(cached_response): return cached_response is not None and datetime.now( ) - cached_response[0] < timedelta(seconds=5) def parse_or_raise(xml): try: return XML(xml) except (ExpatError, SyntaxError) as e: msg = "GeoServer gave non-XML response for [GET %s]: %s" msg = msg % (rest_url, xml) raise Exception(msg, e) if is_valid(cached_response): raw_text = cached_response[1] return parse_or_raise(raw_text) else: response = self.request(method='get', url=rest_url) if response.status_code == 200: self._cache[rest_url] = (datetime.now(), response.text) return parse_or_raise(response.text) else: raise FailedRequestError( "Tried to make a GET request to %s but got a %d status code: \n%s" % (rest_url, response.status_code, response.text))
def delete(self, config_object, purge=None, recurse=False): """ send a delete request XXX [more here] """ rest_url = config_object.href # Assemble params params = {} # purge deletes the SLD from disk when a style is deleted if purge: params["purge"] = str(purge) # recurse deletes the resource when a layer is deleted. if recurse: params["recurse"] = "true" headers = { "Content-type": "application/xml", "Accept": "application/xml" } response = self.request(method='delete', url=rest_url, headers=headers, params=params) self._cache.clear() if response.status_code == 200: return (response, response.text) else: raise FailedRequestError( "Tried to make a DELETE request to %s but got a %d status code: \n%s" % (rest_url, response.status_code, response.text))
def about(self): """return the about information as a formatted html""" about_url = self.service_url + "/about/version.html" response = self.request(method='get', url=about_url) if response.status_code == 200: return response.text raise FailedRequestError('Unable to determine version: %s' % (response.text or response.status_code))
def list_granules(self, coverage, store, workspace=None, filter=None, limit=None, offset=None): """List granules of an imagemosaic""" params = dict() if filter is not None: params['filter'] = filter if limit is not None: params['limit'] = limit if offset is not None: params['offset'] = offset workspace_name = workspace if isinstance(store, basestring): store_name = store else: store_name = store.name workspace_name = store.workspace.name if workspace_name is None: raise ValueError("Must specify workspace") cs_url = url(self.service_url, [ "workspaces", workspace_name, "coveragestores", store_name, "coverages", coverage, "index/granules.json" ]) # GET /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules.json headers = { "Content-type": "application/json", "Accept": "application/json" } response = self.request(method='get', url=cs_url, headers=headers, params=params) if response.status_code != 200: raise FailedRequestError('{0} - "{1}"'.format( response.status_code, response.text)) self._cache.clear() granules = response.json(object_hook=_decode_dict) return granules
def mosaic_coverages(self, store): """Returns all coverages in a coverage store""" cs_url = url(self.service_url, [ "workspaces", store.workspace.name, "coveragestores", store.name, "coverages.json" ]) # GET /workspaces/<ws>/coveragestores/<name>/coverages.json headers = { "Content-type": "application/json", "Accept": "application/json" } response = self.request(method='get', url=cs_url, headers=headers) if response.status_code != 200: raise FailedRequestError('{0} - "{1}"'.format( response.status_code, response.text)) self._cache.clear() coverages = response.json(object_hook=_decode_dict) return coverages
def set_default_workspace(self, name): if getattr(name, 'name', None) is not None: name = name.name workspace = self.get_workspace(name) if workspace is not None: headers = {"Content-Type": "application/xml"} default_workspace_url = self.service_url + "/workspaces/default.xml" msg = "<workspace><name>%s</name></workspace>" % name response = requests.put(default_workspace_url, headers=headers, data=msg, auth=self._auth, verify=self._verify) assert 200 <= response.status_code < 300, "Error setting default workspace: " \ + str(response.status_code) + ": " + response.text self._cache.pop(default_workspace_url, None) self._cache.pop("%s/workspaces.xml" % self.service_url, None) else: raise FailedRequestError("no workspace named '%s'" % name)
def save(self, obj, content_type="application/xml"): """ saves an object to the REST service gets the object's REST location and the data from the object, then POSTS the request. """ rest_url = obj.href message = obj.message() headers = {"Content-type": content_type, "Accept": content_type} logger.debug("%s %s", obj.save_method, obj.href) response = self.request(method=obj.save_method, url=rest_url, headers=headers, data=message) self._cache.clear() if 400 <= response.status_code < 600: raise FailedRequestError("Error code (%s) from GeoServer: %s" % (response.status_code, response.text)) return response