def links(): for layer in layers: yield { 'id': layer, 'links': [{ 'href': self.href(f"/{layer}"), 'rel': QgsServerOgcApi.relToString(QgsServerOgcApi.item), 'type': QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), 'title': "Cache layer", }]}
def links(): for name,project in coll: yield { 'id': name, 'project': project, 'links': [{ "href": self.href(f"/{name}"), "rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item), "type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), "title": "Cache collection", }]}
def get(self) -> None: data = { 'links': [{ "href": self.href("/collections"), "rel": QgsServerOgcApi.relToString(QgsServerOgcApi.data), "type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), "title": "Cache collections", }] } self.write(data)
def get(self, collectionid: str): """ Return project metadata """ metadata, project, layers = self.get_metadata(collectionid) def links(): for layer in layers: yield { 'id': layer, 'links': [{ 'href': self.href(f"/layers/{layer}"), 'rel': QgsServerOgcApi.relToString(QgsServerOgcApi.item), 'type': QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), 'title': "Cache layer", }]} data = { 'id': collectionid, 'project': project, 'layers' : list(links()), 'links' : [ { "href": self.href("/docs"), "rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item), "type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), "title": "Cache collection documents", }, { "href": self.href("/layers"), "rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item), "type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON), "title": "Cache collection layers", }, ], } self.write(data)
def testOgcApiHandler(self): """Test OGC API Handler""" project = QgsProject() project.read(unitTestDataPath('qgis_server') + '/test_project.qgs') request = QgsBufferServerRequest( 'http://server.qgis.org/wfs3/collections/testlayer%20èé/items?limit=-1' ) response = QgsBufferServerResponse() ctx = QgsServerApiContext('/services/api1', request, response, project, self.server.serverInterface()) h = Handler1() self.assertTrue( h.staticPath(ctx).endswith('/resources/server/api/ogc/static')) self.assertEqual(h.path(), QtCore.QRegularExpression("/handlerone")) self.assertEqual(h.description(), 'The first handler ever') self.assertEqual(h.operationId(), 'handlerOne') self.assertEqual(h.summary(), 'First of its name') self.assertEqual(h.linkTitle(), 'Handler One Link Title') self.assertEqual(h.linkType(), QgsServerOgcApi.data) with self.assertRaises(QgsServerApiBadRequestException) as ex: h.handleRequest(ctx) self.assertEqual(str(ex.exception), 'Missing required argument: \'value1\'') r = ctx.response() self.assertEqual(r.data(), '') with self.assertRaises(QgsServerApiBadRequestException) as ex: h.values(ctx) self.assertEqual(str(ex.exception), 'Missing required argument: \'value1\'') # Add handler to API and test for /api2 ctx = QgsServerApiContext('/services/api2', request, response, project, self.server.serverInterface()) api = QgsServerOgcApi(self.server.serverInterface(), '/api2', 'apitwo', 'a second api', '1.2') api.registerHandler(h) # Add a second handler (will be tested later) h2 = Handler2() api.registerHandler(h2) ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/services/api1')) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual( str(ex.exception), 'Requested URI does not match any registered API handler') ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/services/api2')) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual( str(ex.exception), 'Requested URI does not match any registered API handler') ctx.request().setUrl( QtCore.QUrl('http://www.qgis.org/services/api2/handlerone')) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual(str(ex.exception), 'Missing required argument: \'value1\'') ctx.request().setUrl( QtCore.QUrl( 'http://www.qgis.org/services/api2/handlerone?value1=not+a+double' )) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual( str(ex.exception), 'Argument \'value1\' could not be converted to Double') ctx.request().setUrl( QtCore.QUrl( 'http://www.qgis.org/services/api2/handlerone?value1=1.2345')) params = h.values(ctx) self.assertEqual(params, {'value1': 1.2345}) api.executeRequest(ctx) self.assertEqual( json.loads(bytes(ctx.response().data()))['value1'], 1.2345) # Test path fragments extraction ctx.request().setUrl( QtCore.QUrl( 'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345' )) params = h2.values(ctx) self.assertEqual(params, { 'code1': '00', 'value1': 1.2345, 'value2': None }) # Test string encoding ctx.request().setUrl( QtCore.QUrl( 'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345&value2=a%2Fstring%20some' )) params = h2.values(ctx) self.assertEqual(params, { 'code1': '00', 'value1': 1.2345, 'value2': 'a/string some' }) # Test links self.assertEqual( h2.href(ctx), 'http://www.qgis.org/services/api2/handlertwo/00/555?value1=1.2345&value2=a%2Fstring%20some' ) self.assertEqual( h2.href(ctx, '/extra'), 'http://www.qgis.org/services/api2/handlertwo/00/555/extra?value1=1.2345&value2=a%2Fstring%20some' ) self.assertEqual( h2.href(ctx, '/extra', 'json'), 'http://www.qgis.org/services/api2/handlertwo/00/555/extra.json?value1=1.2345&value2=a%2Fstring%20some' ) # Test template path self.assertTrue( h2.templatePath(ctx).endswith( '/resources/server/api/ogc/templates/services/api2/handlerTwo.html' ))
def testOgcApi(self): """Test OGC API""" api = QgsServerOgcApi(self.server.serverInterface(), '/api1', 'apione', 'an api', '1.1') self.assertEqual(api.name(), 'apione') self.assertEqual(api.description(), 'an api') self.assertEqual(api.version(), '1.1') self.assertEqual(api.rootPath(), '/api1') url = 'http://server.qgis.org/wfs3/collections/testlayer%20èé/items?limit=-1' self.assertEqual( api.sanitizeUrl(QtCore.QUrl(url)).toString(), 'http://server.qgis.org/wfs3/collections/testlayer \xe8\xe9/items?limit=-1' ) self.assertEqual( api.sanitizeUrl( QtCore.QUrl('/path//double//slashes//#fr')).toString(), '/path/double/slashes#fr') self.assertEqual(api.relToString(QgsServerOgcApi.data), 'data') self.assertEqual(api.relToString(QgsServerOgcApi.alternate), 'alternate') self.assertEqual(api.contentTypeToString(QgsServerOgcApi.JSON), 'JSON') self.assertEqual(api.contentTypeToStdString(QgsServerOgcApi.JSON), 'JSON') self.assertEqual(api.contentTypeToExtension(QgsServerOgcApi.JSON), 'json') self.assertEqual(api.contentTypeToExtension(QgsServerOgcApi.GEOJSON), 'geojson')
def testOgcApiHandlerContentType(self): """Test OGC API Handler content types""" project = QgsProject() project.read(unitTestDataPath('qgis_server') + '/test_project.qgs') request = QgsBufferServerRequest( 'http://server.qgis.org/api3/handlerthree?value1=9.5') response = QgsBufferServerResponse() # Add handler to API and test for /api3 ctx = QgsServerApiContext('/services/api3', request, response, project, self.server.serverInterface()) api = QgsServerOgcApi(self.server.serverInterface(), '/api3', 'apithree', 'a third api', '1.2') h3 = Handler3() api.registerHandler(h3) ctx = QgsServerApiContext('/services/api3/', request, response, project, self.server.serverInterface()) api.executeRequest(ctx) self.assertEqual( json.loads(bytes(ctx.response().data()))['value1'], 9.5) # Call HTML ctx.request().setUrl( QtCore.QUrl( 'http://server.qgis.org/api3/handlerthree.html?value1=9.5')) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual(str(ex.exception), 'Unsupported Content-Type: HTML') h3.setContentTypes([QgsServerOgcApi.HTML]) with self.assertRaises(QgsServerApiBadRequestException) as ex: api.executeRequest(ctx) self.assertEqual(str(ex.exception), 'Template not found: handlerThree.html') # Define a template path dir = QtCore.QTemporaryDir() with open(dir.path() + '/handlerThree.html', 'w+') as f: f.write("Hello world") h3.templatePathOverride = dir.path() + '/handlerThree.html' ctx.response().clear() api.executeRequest(ctx) self.assertEqual(bytes(ctx.response().data()), b"Hello world")
def __init__(self, serverIface): api = QgsServerOgcApi(serverIface, '/customapi', 'custom api', 'a custom api', '1.1') handler = CustomApiHandler() api.registerHandler(handler) serverIface.serviceRegistry().registerApi(api)