diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pydrilla/pydrilla.py | 37 | ||||
-rw-r--r-- | src/test/test_pydrilla.py | 11 |
2 files changed, 37 insertions, 11 deletions
diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py index dc472a4..af4ff1c 100644 --- a/src/pydrilla/pydrilla.py +++ b/src/pydrilla/pydrilla.py @@ -511,27 +511,36 @@ class Content: ver=version_string(mapping['version']) ) - def query(self, url, max=0): + def query(self, url): ''' - Return return registered patterns and mappings (available as - MappingItems) that match url. The maximum number of items yielded may be - limited by using the optional max argument. Its default value, 0, causes - no limit to be imposed. + Return a list of registered mappings that match url. If multiple versions of a mapping are applicable, only the most recent is included in the result. ''' deco = DeconstructedUrl(url) + mappings = {} + domain_tree = self.patterns_by_proto.get(deco.proto) \ or PatternTreeNode() + + def process_item(item): + if url[-1] != '/' and item.pattern[-1] == '/': + return + + identifier = item.mapping['identifier'] + + if identifier not in mappings or \ + item.mapping['version'] > mappings[identifier]['version']: + mappings[identifier] = item.mapping + for path_tree in domain_tree.search(deco.domain): - for item in path_tree.search(deco.path): - if url[-1] == '/' or item.pattern[-1] != '/': - yield item - max -= 1 - if max == 0: - return + for item_list in path_tree.search(deco.path): + for item in item_list: + process_item(item) + + return list(mappings.values()) def load_content_from_subdir(subdir_path, source_name, content): index_path = subdir_path / 'index.json' @@ -683,3 +692,9 @@ for item_type, get_dict, get_item in [ return get_item(identifier, get_dict) bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, _get_item) + +@bp.route('/query') +def query(): + url = request.args['url'] + + return json.dumps(content().query(url)) diff --git a/src/test/test_pydrilla.py b/src/test/test_pydrilla.py index 660c8f3..db9a321 100644 --- a/src/test/test_pydrilla.py +++ b/src/test/test_pydrilla.py @@ -99,6 +99,17 @@ def test_api_basic(client, development_config): response = client.get('/sources/random-bad-identifier') assert response.status_code == 404 + response = client.get('/query?url=https://hachettebugs.koszko.org') + definitions = json.loads(response.data.decode()) + assert type(definitions) is list + assert all([d['type'] == 'mapping' for d in definitions]) + assert any([p['pattern'] == 'https://hachettebugs.koszko.org/***' + for d in definitions for p in d['payloads']]) + + response = client.get('/query?url=https://random_bad_domain.org/something') + definitions = json.loads(response.data.decode()) + assert 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] |