diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pydrilla/pydrilla.py | 57 | ||||
-rw-r--r-- | src/test/test_pydrilla.py | 13 |
2 files changed, 52 insertions, 18 deletions
diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py index 2c4ad0d..2e91b89 100644 --- a/src/pydrilla/pydrilla.py +++ b/src/pydrilla/pydrilla.py @@ -494,6 +494,10 @@ class Content: ver=version_string(mapping['version']) ) + def item_dict(self, type): + '''Obtain self.resources or self.mappings, depending on type.''' + return self.resources if type == 'resource' else self.mappings + def find_item(self, type, identifier, ver=None): ''' Find and return definition of the newest version of resource/mapping @@ -502,8 +506,7 @@ class Content: If ver is specified, instead find and return definition of that version of the item (or None is absent). ''' - dict = self.resources if type == 'resource' else self.mappings - versioned_item = dict.get(identifier) + versioned_item = self.item_dict(type).get(identifier) if not versioned_item: return None @@ -511,6 +514,20 @@ class Content: return versioned_item.by_version.get(ver) + def get_item_all_versions(self, type, identifier): + ''' + Return a list of all definitions of given resource or mapping, ordered + by version. + + If no item of given type with such identifier exists, return []. + ''' + versioned_item = self.item_dict(type).get(identifier) + if not versioned_item: + return [] + + return [versioned_item.by_version[version_string(ver)] + for ver in versioned_item.known_versions] + def query(self, url, max=0): ''' Return return registered patterns and mappings (available as @@ -604,6 +621,9 @@ def escaping_gettext(text_key): return str(escape(_(text_key))) +def content(): + return current_app._pydrilla_content + class MyEnvironment(Environment): ''' A wrapper class around jinja2.Environment that causes GNU gettext function @@ -639,20 +659,25 @@ indexpage = j2env.get_template('index.html') def index(): return indexpage.render() -for item_type in ['mapping', 'resource']: - def item(identifier, item_type=item_type): - ver = request.args.get('ver') - if ver is not None: - try: - ver = normalize_version(parse_version(ver)) - except: - abort(400) +def get_item(identifier, item_type): + ver = request.args.get('ver') + if ver == 'all': + definitions = content().get_item_all_versions(item_type, identifier) + return json.dumps(definitions) + if ver is not None: + try: + ver = normalize_version(parse_version(ver)) + except: + abort(400) + + definition = content().find_item(item_type, identifier, ver) + if definition is None: + abort(404) - definition = current_app._pydrilla_content\ - .find_item(item_type, identifier, ver) - if definition is None: - abort(404) + return json.dumps(definition) - return json.dumps(definition) +for item_type in ['mapping', 'resource']: + def _get_item(identifier, item_type=item_type): + return get_item(identifier, item_type) - bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, item) + bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, _get_item) diff --git a/src/test/test_pydrilla.py b/src/test/test_pydrilla.py index ed98f13..9e87704 100644 --- a/src/test/test_pydrilla.py +++ b/src/test/test_pydrilla.py @@ -51,9 +51,9 @@ def test_api_basic(client): for item_type in ['mapping', 'resource']: response = client.get(f'/{item_type}s/helloapple') definition = json.loads(response.data.decode()) - assert definition['type'] == item_type + assert definition['type'] == item_type assert definition['source_name'] == 'hello' - assert definition['version'] == [2021, 11, 10] + assert definition['version'] == [2021, 11, 10] response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.0') assert definition == json.loads(response.data.decode()) @@ -61,6 +61,15 @@ def test_api_basic(client): response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.999') assert response.status_code == 404 + response = client.get(f'/{item_type}s/helloapple?ver=random_input') + assert response.status_code == 400 + + response = client.get(f'/{item_type}s/helloapple?ver=all') + definitions = json.loads(response.data.decode()) + assert type(definitions) is list + assert all([d['type'] == item_type for d in definitions]) + assert any([d['version'] == [2021, 11, 10] for d in definitions]) + def test_normalize_version(): assert pydrilla.normalize_version([4, 5, 3, 0, 0]) == [4, 5, 3] assert pydrilla.normalize_version([1, 0, 5, 0]) == [1, 0, 5] |