def _request(self, path: str = None, as_dict: bool = True, kwargs: dict = {}) -> dict: """ helper function for request/response patterns against OGC API endpoints @type path: string @param path: path of request @type as_dict: bool @param as_dict: whether to return JSON dict (default True) @type kwargs: string @param kwargs: ``dict`` of keyword value pair request parameters @returns: response as JSON ``dict`` """ url = self._build_url(path) LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=self.headers, auth=self.auth, params=kwargs) if response.status_code != requests.codes.ok: raise RuntimeError(response.text) if as_dict: return response.json() else: return response.content
def collection_items(self, collection_id, **kwargs): """ implements /collection/{collectionId}/items @type collection_id: string @param collection_id: id of collection @type bbox: list @param bbox: list of minx,miny,maxx,maxy @type datetime: string @param datetime: time extent or time instant @type limit: int @param limit: limit number of features @type startindex: int @param startindex: start position of results @returns: feature results """ if 'bbox' in kwargs: kwargs['bbox'] = ','.join(kwargs['bbox']) path = 'collections/{}/items'.format(collection_id) url = self._build_url(path) LOGGER.debug('Request: {}'.format(url)) response = http_get( url, headers=self.headers, params=kwargs, auth=self.auth ).json() return response
def collection_items(self, collection_name, **kwargs): """ implements Requirement 17 (/req/core/fc-op) @type collection_name: string @param collection_name: name of collection @type bbox: list @param bbox: list of minx,miny,maxx,maxy @type time: string @param time: time extent or time instant @type limit: int @param limit: limit number of features @type startindex: int @param startindex: start position of results @returns: feature results """ if "bbox" in kwargs: kwargs["bbox"] = ",".join(kwargs["bbox"]) path = "collections/{}/items".format(collection_name) url = self._build_url(path) LOGGER.debug("Request: {}".format(url)) response = http_get(url, headers=REQUEST_HEADERS, params=kwargs, auth=self.auth).json() return response
def __init__(self, url, json_=None, timeout=30, headers=None, auth=None): """ Initializer; implements / @type url: string @param url: url of OGC API landing page document @type json_: string @param json_: json object @param headers: HTTP headers to send with requests @param timeout: time (in seconds) after which requests should timeout @param username: service authentication username @param password: service authentication password @param auth: instance of owslib.util.Authentication @returns: `owslib.ogcapi.API` """ if '?' in url: self.url, self.url_query_string = url.split('?') else: self.url = url.rstrip('/') + '/' self.url_query_string = None self.json_ = json_ self.timeout = timeout self.headers = REQUEST_HEADERS if headers: self.headers.update(headers) self.auth = auth if json_ is not None: # static JSON string self.links = json.loads(json_)['links'] else: response = http_get(url, headers=self.headers, auth=self.auth).json() self.links = response['links']
def conformance(self): """ implements Requirement 5 (/req/core/conformance-op) @returns: conformance object """ url = self._build_url("conformance") LOGGER.debug("Request: {}".format(url)) response = http_get(url, headers=self.headers, auth=self.auth).json() return response
def collections(self): """ implements Requirement 9 (/req/core/collections-op) @returns: collections object """ url = self._build_url('collections') LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() return response['collections']
def api(self): """ implements Requirement 3 (/req/core/api-definition-op) @returns: OpenAPI definition object """ url = self._build_url('api') LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() return response
def collections(self): """ implements /collections @returns: `dict` of collections object """ url = self._build_url('collections') LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=self.headers, auth=self.auth).json() return response['collections']
def collection(self, collection_id): """ implements Requirement 15 (/req/core/sfc-md-op) @type collection_id: string @param collection_id: id of collection @returns: feature collection metadata """ path = "collections/{}".format(collection_id) url = self._build_url(path) LOGGER.debug("Request: {}".format(url)) response = http_get(url, headers=self.headers, auth=self.auth).json() return response
def collection(self, collection_name): """ implements Requirement 15 (/req/core/sfc-md-op) @type collection_name: string @param collection_name: name of collection @returns: feature collection metadata """ path = 'collections/{}'.format(collection_name) url = self._build_url(path) LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() return response
def collection(self, collection_id): """ implements /collections/{collectionId} @type collection_id: string @param collection_id: id of collection @returns: `dict` of feature collection metadata """ path = 'collections/{}'.format(collection_id) url = self._build_url(path) LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=self.headers, auth=self.auth).json() return response
def _request(self, path: str = None, as_dict: bool = True, kwargs: dict = {}) -> dict: """ helper function for request/response patterns against OGC API endpoints @type path: string @param path: path of request @type as_dict: bool @param as_dict: whether to return JSON dict (default True) @type kwargs: string @param kwargs: ``dict`` of keyword value pair request parameters @returns: response as JSON ``dict`` """ url = self._build_url(path) self.request = url LOGGER.debug(f'Request: {url}') LOGGER.debug(f'Params: {kwargs}') if 'cql' not in kwargs: response = http_get(url, headers=self.headers, auth=self.auth, params=kwargs) else: LOGGER.debug('CQL query detected') kwargs2 = deepcopy(kwargs) cql = kwargs2.pop('cql') url2 = self._build_url(path, kwargs2) response = http_post(url2, request=cql, auth=self.auth) LOGGER.debug(f'URL: {response.url}') if response.status_code != requests.codes.ok: raise RuntimeError(response.text) self.request = response.url if as_dict: return response.json() else: return response.content
def collection_item(self, collection_id, identifier): """ implements Requirement 30 (/req/core/f-op) @type collection_id: string @param collection_id: id of collection @type identifier: string @param identifier: feature identifier @returns: single feature result """ path = "collections/{}/items/{}".format(collection_id, identifier) url = self._build_url(path) LOGGER.debug("Request: {}".format(url)) response = http_get(url, headers=self.headers, auth=self.auth).json() return response
def collection_item(self, collection_name, identifier): """ implements Requirement 30 (/req/core/f-op) @type collection_name: string @param collection_name: name of collection @type identifier: string @param identifier: feature identifier @returns: single feature result """ path = 'collections/{}/items/{}'.format(collection_name, identifier) url = self._build_url(path) LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() return response
def __init__(self, url, version, json_, timeout=30, username=None, password=None, auth=None): """ initializer; implements Requirement 1 (/req/core/root-op) @type url: string @param url: url of WFS root document @type json_: string @param json_: json object @param timeout: time (in seconds) after which requests should timeout @param username: service authentication username @param password: service authentication password @param auth: instance of owslib.util.Authentication @return: initialized WebFeatureService_3_0_0 object """ if "?" in url: self.url, self.url_query_string = url.split("?") else: self.url = url.rstrip("/") + "/" self.url_query_string = None self.version = version self.json_ = json_ self.timeout = timeout if auth: if username: auth.username = username if password: auth.password = password self.auth = auth or Authentication(username, password) if json_ is not None: # static JSON string self.links = json.loads(json_)["links"] else: response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() self.links = response["links"]
def api(self) -> dict: """ implements /api @returns: `dict` of OpenAPI definition object """ url = None openapi_format = None openapi_json_mimetype = 'application/vnd.oai.openapi+json;version=3.0' openapi_yaml_mimetype = 'application/vnd.oai.openapi;version=3.0' LOGGER.debug('Searching for OpenAPI JSON Document') for link in self.links: if link['rel'] == 'service-desc' and link[ 'type'] == openapi_json_mimetype: openapi_format = openapi_json_mimetype url = link['href'] break LOGGER.debug('Searching for OpenAPI YAML Document') if url is None: if link['rel'] == 'service-desc' and link[ 'type'] == openapi_yaml_mimetype: openapi_format = openapi_yaml_mimetype url = link['href'] break if url is not None: LOGGER.debug(f'Request: {url}') response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth) if openapi_format == openapi_json_mimetype: content = response.json() elif openapi_format == openapi_yaml_mimetype: content = yaml.load(response.text) return content else: msg = 'Did not find service-desc link' LOGGER.error(msg) raise RuntimeError(msg)
def api(self): """ implements Requirement 3 (/req/core/api-definition-op) @returns: OpenAPI definition object """ url = None for l in self.links: if l['rel'] == 'service-desc': url = l['href'] if url is not None: LOGGER.debug('Request: {}'.format(url)) response = http_get(url, headers=REQUEST_HEADERS, auth=self.auth).json() return response else: msg = 'Did not find service-desc link' LOGGER.error(msg) raise RuntimeError(msg)