Skip to content

Commit

Permalink
Use RequestHandler.links method in cache manager API
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Sep 3, 2021
1 parent 33d1ae1 commit 4f466fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 47 deletions.
40 changes: 28 additions & 12 deletions tests/test_wmts_cachemngrapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ def test_wmts_cachemngrapi_empty_cache(client):

json_content = json.loads(rv.content)
assert 'links' in json_content
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'Cache collections'
assert len(json_content['links']) == 2
assert json_content['links'][0]['title'] == 'WMTS Cache manager LandingPage as JSON'
assert json_content['links'][0]['rel'] == 'self'
assert json_content['links'][1]['title'] == 'WMTS Cache manager Collections as JSON'
assert json_content['links'][1]['rel'] == 'data'

qs = "/wmtscache/collections"
rv = client.get(qs)
Expand All @@ -47,6 +50,9 @@ def test_wmts_cachemngrapi_empty_cache(client):
assert 'collections' in json_content
assert len(json_content['collections']) == 0
assert 'links' in json_content
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'WMTS Cache manager Collections as JSON'
assert json_content['links'][0]['rel'] == 'self'


def test_wmts_cachemngrapi_cache_info(client):
Expand Down Expand Up @@ -112,7 +118,7 @@ def test_wmts_cachemngrapi_cache_info(client):
"TILEMATRIX": "0",
"TILEROW": "0",
"TILECOL": "0",
"FORMAT": "image/png"
"FORMAT": "image/png"
}

# Get the cached path from the request parameters
Expand Down Expand Up @@ -148,7 +154,9 @@ def test_wmts_cachemngrapi_cache_info(client):
assert len(json_content['collections']) == 1

assert 'links' in json_content
assert len(json_content['links']) == 0
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'WMTS Cache manager Collections as JSON'
assert json_content['links'][0]['rel'] == 'self'

collection = json_content['collections'][0]
assert 'id' in collection
Expand All @@ -169,7 +177,9 @@ def test_wmts_cachemngrapi_cache_info(client):
assert json_content['project'] == client.getprojectpath("france_parts.qgs").strpath

assert 'links' in json_content
assert len(json_content['links']) == 2
assert len(json_content['links']) == 3
assert json_content['links'][0]['title'] == 'WMTS Cache manager ProjectCollection as JSON'
assert json_content['links'][0]['rel'] == 'self'

qs = "/wmtscache/collections/{}/docs".format(collection['id'])
rv = client.get(qs)
Expand All @@ -187,7 +197,9 @@ def test_wmts_cachemngrapi_cache_info(client):
assert json_content['documents'] == 1

assert 'links' in json_content
assert len(json_content['links']) == 0
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'WMTS Cache manager DocumentCollection as JSON'
assert json_content['links'][0]['rel'] == 'self'

qs = "/wmtscache/collections/{}/layers".format(collection['id'])
rv = client.get(qs)
Expand All @@ -205,7 +217,9 @@ def test_wmts_cachemngrapi_cache_info(client):
assert len(json_content['layers']) == 1

assert 'links' in json_content
assert len(json_content['links']) == 0
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'WMTS Cache manager LayerCollection as JSON'
assert json_content['links'][0]['rel'] == 'self'

layer = json_content['layers'][0]
assert 'id' in layer
Expand All @@ -221,7 +235,9 @@ def test_wmts_cachemngrapi_cache_info(client):
assert json_content['id'] == layer['id']

assert 'links' in json_content
assert len(json_content['links']) == 0
assert len(json_content['links']) == 1
assert json_content['links'][0]['title'] == 'WMTS Cache manager LayerCache as JSON'
assert json_content['links'][0]['rel'] == 'self'


def test_wmts_cachemngrapi_delete_docs(client):
Expand Down Expand Up @@ -356,7 +372,7 @@ def test_wmts_cachemngrapi_delete_layer(client):
"TILEMATRIX": "0",
"TILEROW": "0",
"TILECOL": "0",
"FORMAT": "image/png"
"FORMAT": "image/png"
}

# Get the cached path from the request parameters
Expand Down Expand Up @@ -478,7 +494,7 @@ def test_wmts_cachemngrapi_delete_layers(client):
"TILEMATRIX": "0",
"TILEROW": "0",
"TILECOL": "0",
"FORMAT": "image/png"
"FORMAT": "image/png"
}

# Get the cached path from the request parameters
Expand Down Expand Up @@ -610,7 +626,7 @@ def test_wmts_cachemngrapi_delete_collection(client):
"TILEMATRIX": "0",
"TILEROW": "0",
"TILECOL": "0",
"FORMAT": "image/png"
"FORMAT": "image/png"
}

# Get the cached path from the request parameters
Expand Down Expand Up @@ -746,7 +762,7 @@ def test_wmts_cachemngrapi_layerid_error(client):
"TILEMATRIX": "0",
"TILEROW": "0",
"TILECOL": "0",
"FORMAT": "image/png"
"FORMAT": "image/png"
}

# Get the cached path from the request parameters
Expand Down
90 changes: 55 additions & 35 deletions wmtsCacheServer/cachemngrapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def collect():
project = inf.read_text()
# (name, project)
yield (name,project)

return (metadata, collect())


Expand All @@ -58,13 +58,23 @@ class LandingPage(RequestHandler):
""" Project collections listing handler
"""
def get(self) -> None:

def extra_links():
""" Build links to collections
"""
for ct in self._parent.contentTypes():
if ct != QgsServerOgcApi.JSON:
# Collections only implement JSON
continue
yield {
"href": self.href("/collections", QgsServerOgcApi.contentTypeToExtension(ct) if ct != QgsServerOgcApi.JSON else ''),
"rel": QgsServerOgcApi.relToString(QgsServerOgcApi.data),
"type": QgsServerOgcApi.mimeType(ct),
"title": 'WMTS Cache manager Collections as '+QgsServerOgcApi.contentTypeToString(ct),
}

data = {
'links': [{
"href": self.href("/collections"),
"rel": QgsServerOgcApi.relToString(QgsServerOgcApi.data),
"type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON),
"title": "Cache collections",
}]
'links': self.links() + list(extra_links())
}
self.write(data)

Expand All @@ -77,21 +87,31 @@ def get(self) -> None:
"""
metadata, coll = read_metadata_collection(self.rootdir)

def links():
def collection_links(name):
""" Build links to collection
"""
for ct in self._parent.contentTypes():
if ct != QgsServerOgcApi.JSON:
# ProjectCollection only implement JSON
continue
yield {
"href": self.href(f"/{name}", QgsServerOgcApi.contentTypeToExtension(ct) if ct != QgsServerOgcApi.JSON else ''),
"rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item),
"type": QgsServerOgcApi.mimeType(ct),
"title": 'WMTS Cache manager ProjectCollection as '+QgsServerOgcApi.contentTypeToString(ct),
}

def collections():
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",
}]}
'links': list(collection_links(name))
}

data = {
"cache_layout": metadata['layout'],
"collections": list(links()),
"links": [], # self.links(context)
"collections": list(collections()),
"links": self.links(),
}

self.write(data)
Expand All @@ -100,7 +120,7 @@ def links():
class MetadataMixIn:

def get_metadata(self, collectionid: str):
""" Return project metadata
""" Return project metadata
"""
try:
project, layers = read_project_metadata(self.rootdir, collectionid)
Expand All @@ -121,14 +141,14 @@ class ProjectCollection(RequestHandler,MetadataMixIn):
"""

def get(self, collectionid: str):
""" Return project metadata
""" Return project metadata
"""
metadata, project, layers = self.get_metadata(collectionid)

def links():
def layer_collections():
for layer in layers:
yield { 'id': layer,
'links': [{
'links': [{
'href': self.href(f"/layers/{layer}"),
'rel': QgsServerOgcApi.relToString(QgsServerOgcApi.item),
'type': QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON),
Expand All @@ -138,8 +158,8 @@ def links():
data = {
'id': collectionid,
'project': project,
'layers' : list(links()),
'links' : [
'layers' : list(layer_collections()),
'links' : self.links() + [
{
"href": self.href("/docs"),
"rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item),
Expand All @@ -151,9 +171,9 @@ def links():
"rel": QgsServerOgcApi.relToString(QgsServerOgcApi.item),
"type": QgsServerOgcApi.mimeType(QgsServerOgcApi.JSON),
"title": "Cache collection layers",
},
},
],
}
}

self.write(data)

Expand All @@ -178,7 +198,7 @@ def delete(self, collectionid) -> None:


class DocumentCollection(RequestHandler,MetadataMixIn):
""" Return documentation about project
""" Return documentation about project
"""

def get(self, collectionid):
Expand All @@ -192,7 +212,7 @@ def get(self, collectionid):
'id': collectionid,
'project': project,
'documents': sum( 1 for _ in docroot.glob('*.xml')),
'links': [], # self.links(context)
'links': self.links(),
}

self.write(data)
Expand All @@ -210,15 +230,15 @@ def delete(self, collectionid ) -> None:


class LayerCollection(RequestHandler,MetadataMixIn):
"""
"""
"""

def get(self, collectionid):
""" Layer info
"""
metadata,project,layers = self.get_metadata(collectionid)

def links():
def layer_collections():
for layer in layers:
yield { 'id': layer,
'links': [{
Expand All @@ -231,8 +251,8 @@ def links():
data = {
'id': collectionid,
'project': project,
'layers' : list(links()),
'links' : [], # self.links(context)
'layers' : list(layer_collections()),
'links' : self.links(),
}

self.write(data)
Expand All @@ -254,27 +274,27 @@ class LayerCache(RequestHandler,MetadataMixIn):
"""

def get( self, collectionid: str, layerid: str) -> None:
"""
"""
"""
metadata, project, layers = self.get_metadata(collectionid)
if layerid not in layers:
raise HTTPError(404,reason=f"Layer '{layerid}' not found")

data = {
'id': layerid,
'links':[],
'links':self.links(),
}
self.write(data)


def delete( self, collectionid: str, layerid: str) -> None:
""" List projects
"""
metadata, project, layers = self.get_metadata(collectionid)
if layerid not in layers:
raise HTTPError(404,reason=f"Layer '{layerid}' not found")

cache = self.cache_helper(metadata)
cache = self.cache_helper(metadata)
cache = CacheHelper(self.rootdir, metadata['layout'])

# Remove tiles
Expand Down

0 comments on commit 4f466fc

Please sign in to comment.