11# =============================================================================
2- # Copyright (c) 2020 Tom Kralidis
2+ # Copyright (c) 2022 Tom Kralidis
33#
44# Author: Tom Kralidis <tomkralidis@gmail.com>
55#
1515import yaml
1616
1717from owslib import __version__
18- from owslib .util import Authentication , http_get , http_post
18+ from owslib .util import (Authentication , http_delete , http_get , http_post ,
19+ http_put )
1920
2021LOGGER = logging .getLogger (__name__ )
2122
@@ -114,7 +115,7 @@ def conformance(self) -> dict:
114115 """
115116
116117 path = 'conformance'
117- return self ._request (path )
118+ return self ._request (path = path )
118119
119120 def _build_url (self , path : str = None , params : dict = {}) -> str :
120121 """
@@ -141,15 +142,20 @@ def _build_url(self, path: str = None, params: dict = {}) -> str:
141142
142143 return url
143144
144- def _request (self , path : str = None , as_dict : bool = True ,
145+ def _request (self , method : str = 'GET' , path : str = None ,
146+ data : str = None , as_dict : bool = True ,
145147 kwargs : dict = {}) -> dict :
146148 """
147149 helper function for request/response patterns against OGC API endpoints
148150
149151 @type path: string
150152 @param path: path of request
153+ @type method: string
154+ @param method: HTTP method (default ``GET``)
155+ @type data: string
156+ @param data: request data payload
151157 @type as_dict: bool
152- @param as_dict: whether to return JSON dict (default True)
158+ @param as_dict: whether to return JSON dict (default `` True`` )
153159 @type kwargs: string
154160 @param kwargs: ``dict`` of keyword value pair request parameters
155161
@@ -159,28 +165,35 @@ def _request(self, path: str = None, as_dict: bool = True,
159165 url = self ._build_url (path )
160166 self .request = url
161167
168+ LOGGER .debug (f'Method: { method } ' )
162169 LOGGER .debug (f'Request: { url } ' )
170+ LOGGER .debug (f'Data: { data } ' )
163171 LOGGER .debug (f'Params: { kwargs } ' )
164172
165- if 'cql' not in kwargs :
173+ if method == 'GET' :
166174 response = http_get (url , headers = self .headers , auth = self .auth ,
167175 params = kwargs )
168- else :
169- LOGGER . debug ( 'CQL query detected' )
170- kwargs2 = deepcopy ( kwargs )
171- cql = kwargs2 . pop ( 'cql' )
172- url2 = self . _build_url ( path , kwargs2 )
173- response = http_post ( url2 , request = cql , auth = self .auth )
176+ elif method == 'POST' :
177+ response = http_post ( url , request = data , auth = self . auth )
178+ elif method == 'PUT' :
179+ response = http_put ( url , data = data , auth = self . auth )
180+ elif method == 'DELETE' :
181+ response = http_delete ( url , auth = self .auth )
174182
175183 LOGGER .debug (f'URL: { response .url } ' )
184+ LOGGER .debug (f'Response status code: { response .status_code } ' )
176185
177- if response . status_code != requests . codes . ok :
186+ if not response :
178187 raise RuntimeError (response .text )
179188
180189 self .request = response .url
181190
182191 if as_dict :
183- return response .json ()
192+ if len (response .content ) == 0 :
193+ LOGGER .debug ('Empty response' )
194+ return {}
195+ else :
196+ return response .json ()
184197 else :
185198 return response .content
186199
@@ -199,7 +212,7 @@ def collections(self) -> dict:
199212 """
200213
201214 path = 'collections'
202- return self ._request (path )
215+ return self ._request (path = path )
203216
204217 def collection (self , collection_id : str ) -> dict :
205218 """
@@ -212,7 +225,7 @@ def collection(self, collection_id: str) -> dict:
212225 """
213226
214227 path = f'collections/{ collection_id } '
215- return self ._request (path )
228+ return self ._request (path = path )
216229
217230 def collection_queryables (self , collection_id : str ) -> dict :
218231 """
@@ -225,4 +238,4 @@ def collection_queryables(self, collection_id: str) -> dict:
225238 """
226239
227240 path = f'collections/{ collection_id } /queryables'
228- return self ._request (path )
241+ return self ._request (path = path )
0 commit comments