-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make api docs route optional #82
Changes from all commits
025ea5b
ebda77a
201064d
a72a1f4
32fa174
f67e593
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,93 +21,80 @@ class ApiDeclarationNotFoundError(Exception): | |
|
||
|
||
def find_resource_names(api_docs_json): | ||
return [ | ||
api['path'].lstrip('/') | ||
for api in api_docs_json['apis'] | ||
] | ||
return [api['path'].lstrip('/') for api in api_docs_json['apis']] | ||
|
||
|
||
def build_schema_mapping(schema_dir): | ||
def build_schema_mapping(schema_dir, listing_json): | ||
"""Discovers schema file locations and relations. | ||
|
||
:param schema_dir: the directory schema files live inside | ||
:type schema_dir: string | ||
:returns: A tuple of (resource listing filepath, mapping) where the mapping | ||
is between resource name and file path | ||
:rtype: (string, dict) | ||
:param listing_json: the contents of the listing document | ||
:type listing_json: string | ||
:returns: a mapping from resource name to file path | ||
:rtype: dict | ||
""" | ||
def resource_name_to_filepath(name): | ||
return os.path.join(schema_dir, '{0}.json'.format(name)) | ||
|
||
listing, listing_json = _load_resource_listing(schema_dir) | ||
|
||
return ( | ||
listing, | ||
dict( | ||
(resource, resource_name_to_filepath(resource)) | ||
for resource in find_resource_names(listing_json) | ||
) | ||
return dict( | ||
(resource, resource_name_to_filepath(resource)) | ||
for resource in find_resource_names(listing_json) | ||
) | ||
|
||
|
||
def _load_resource_listing(schema_dir): | ||
def _load_resource_listing(resource_listing): | ||
"""Load the resource listing from file, handling errors. | ||
|
||
:param schema_dir: the directory schema files live inside | ||
:type schema_dir: string | ||
:returns: (resource listing filepath, resource listing json) | ||
:param resource_listing: path to the api-docs resource listing file | ||
:type resource_listing: string | ||
:returns: contents of the resource listing file | ||
:rtype: dict | ||
""" | ||
resource_listing = os.path.join(schema_dir, API_DOCS_FILENAME) | ||
try: | ||
with open(resource_listing) as resource_listing_file: | ||
resource_listing_json = simplejson.load(resource_listing_file) | ||
return simplejson.load(resource_listing_file) | ||
# If not found, raise a more user-friendly error. | ||
except IOError: | ||
raise ResourceListingNotFoundError( | ||
'No resource listing found at {0}. Note that your json file ' | ||
'must be named {1}'.format(resource_listing, API_DOCS_FILENAME) | ||
) | ||
return resource_listing, resource_listing_json | ||
|
||
|
||
def compile_swagger_schema(schema_dir, should_validate_schemas): | ||
def compile_swagger_schema(schema_dir): | ||
"""Build a SwaggerSchema from various files. | ||
|
||
:param schema_dir: the directory schema files live inside | ||
:type schema_dir: string | ||
:param should_validate_schemas: if True, check schemas for correctness | ||
:type should_validate_schemas: boolean | ||
:returns: a SwaggerSchema object | ||
""" | ||
listing, mapping = build_schema_mapping(schema_dir) | ||
schema_resolvers = ingest_resources( | ||
listing, | ||
mapping, | ||
schema_dir, | ||
should_validate_schemas, | ||
) | ||
return SwaggerSchema( | ||
listing, | ||
mapping, | ||
schema_resolvers, | ||
) | ||
listing_filename = os.path.join(schema_dir, API_DOCS_FILENAME) | ||
listing_json = _load_resource_listing(listing_filename) | ||
mapping = build_schema_mapping(schema_dir, listing_json) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I don't like that as much is because it makes the validation kind of implicit instead of being it's own call it's just a side-effect of building a schema. Does ssv need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, i missed that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to not exposing the schema mapping data structure to the outside world. I've explicitly avoided making it public so we have refactoring flexibility going forward. In the event pyramid_swagger is ever 2.0-only, I bet we could (and would want to) do a major refactoring of the data structure itself. |
||
schema_resolvers = ingest_resources(mapping, schema_dir) | ||
return SwaggerSchema(listing_filename, mapping, schema_resolvers) | ||
|
||
|
||
def ingest_resources(listing, mapping, schema_dir, should_validate_schemas): | ||
def validate_swagger_schema(schema_dir): | ||
"""Add the swagger_schema to the registry.settings """ | ||
listing_filename = os.path.join(schema_dir, API_DOCS_FILENAME) | ||
# TODO: this will be replaced by ssv shortly | ||
listing_json = _load_resource_listing(listing_filename) | ||
mapping = build_schema_mapping(schema_dir, listing_json) | ||
validate_swagger_schemas(listing_filename, mapping.values()) | ||
|
||
|
||
def ingest_resources(mapping, schema_dir): | ||
"""Consume the Swagger schemas and produce a queryable datastructure. | ||
|
||
:param listing: Filepath to a resource listing | ||
:type listing: string | ||
:param mapping: Map from resource name to filepath of its api declaration | ||
:type mapping: dict | ||
:param schema_dir: the directory schema files live inside | ||
:type schema_dir: string | ||
:param should_validate_schemas: if True, check schemas for correctness | ||
:type should_validate_schemas: boolean | ||
:returns: A list of SchemaAndResolver objects | ||
:returns: A list of :class:`pyramid_swagger.load_schema.SchemaAndResolver` | ||
objects | ||
""" | ||
resource_filepaths = mapping.values() | ||
|
||
ingested_resources = [] | ||
for name, filepath in mapping.items(): | ||
try: | ||
|
@@ -121,11 +108,4 @@ def ingest_resources(listing, mapping, schema_dir, should_validate_schemas): | |
'your resource name and API declaration file do not ' | ||
'match?'.format(filepath, name, schema_dir) | ||
) | ||
|
||
if should_validate_schemas: | ||
validate_swagger_schemas( | ||
listing, | ||
resource_filepaths | ||
) | ||
|
||
return ingested_resources |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put a comment explaining the purpose of stuffing this in here.