-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5315 from hypothesis/api-index
Factor out new `h.views.api.index` module
- Loading branch information
Showing
4 changed files
with
96 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from h.views.api.config import api_config, AngularRouteTemplater | ||
|
||
|
||
@api_config(route_name='api.index') | ||
def index(context, request): | ||
"""Return the API descriptor document. | ||
Clients may use this to discover endpoints for the API. | ||
""" | ||
|
||
api_links = request.registry.api_links | ||
|
||
# We currently need to keep a list of the parameter names we use in our API | ||
# paths and pass these explicitly into the templater. As and when new | ||
# parameter names are added, we'll need to add them here, or this view will | ||
# break (and get caught by the `test_api_index` functional test). | ||
templater = AngularRouteTemplater(request.route_url, | ||
params=['id', 'pubid', 'user', 'userid']) | ||
|
||
links = {} | ||
for link in api_links: | ||
method_info = { | ||
'method': link['method'], | ||
'url': templater.route_template(link['route_name']), | ||
'desc': link['description'], | ||
} | ||
_set_at_path(links, link['name'].split('.'), method_info) | ||
|
||
return { | ||
'links': links, | ||
} | ||
|
||
|
||
def _set_at_path(dict_, path, value): | ||
""" | ||
Set the value at a given `path` within a nested `dict`. | ||
:param dict_: The root `dict` to update | ||
:param path: List of path components | ||
:param value: Value to assign | ||
""" | ||
key = path[0] | ||
if key not in dict_: | ||
dict_[key] = {} | ||
|
||
if len(path) == 1: | ||
dict_[key] = value | ||
else: | ||
_set_at_path(dict_[key], path[1:], value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from pyramid import testing | ||
from pyramid.config import Configurator | ||
|
||
from h.views.api import index as views | ||
|
||
|
||
class TestIndex(object): | ||
|
||
def test_it_returns_the_right_links(self, pyramid_config, pyramid_request): | ||
|
||
# Scan `h.views.api_annotations` for API link metadata specified in @api_config | ||
# declarations. | ||
config = Configurator() | ||
config.scan('h.views.api.annotations') | ||
pyramid_request.registry.api_links = config.registry.api_links | ||
|
||
pyramid_config.add_route('api.search', '/dummy/search') | ||
pyramid_config.add_route('api.annotations', '/dummy/annotations') | ||
pyramid_config.add_route('api.annotation', '/dummy/annotations/:id') | ||
pyramid_config.add_route('api.links', '/dummy/links') | ||
|
||
result = views.index(testing.DummyResource(), pyramid_request) | ||
|
||
host = 'http://example.com' # Pyramid's default host URL' | ||
links = result['links'] | ||
assert links['annotation']['create']['method'] == 'POST' | ||
assert links['annotation']['create']['url'] == ( | ||
host + '/dummy/annotations') | ||
assert links['annotation']['delete']['method'] == 'DELETE' | ||
assert links['annotation']['delete']['url'] == ( | ||
host + '/dummy/annotations/:id') | ||
assert links['annotation']['read']['method'] == 'GET' | ||
assert links['annotation']['read']['url'] == ( | ||
host + '/dummy/annotations/:id') | ||
assert links['annotation']['update']['method'] == 'PATCH' | ||
assert links['annotation']['update']['url'] == ( | ||
host + '/dummy/annotations/:id') | ||
assert links['search']['method'] == 'GET' | ||
assert links['search']['url'] == host + '/dummy/search' |