Comments (3)
Ah crap, I should've checked GitHub first, because I started on my own script.
With the flex library you can actually avoid having to convert the Swagger file to JSON schema, so that should be a bit more efficient.
The code is very simple:
import requests
from flex.core import load, validate_api_call
schema = load('https://raw.githubusercontent.com/HUPO-PSI/proxi-schemas/master/specs/swagger.yaml')
url = 'http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250'
response = requests.get(url)
validate_api_call(schema, raw_request=response.request, raw_response=response)
See also the flex documentation on API call validation.
There seem to be several issues with the Swagger definition file and the API response preventing successful validation. So I guess it's good to have a validator. 😉
from proxi-schemas.
I guess some of these might come in handy: https://github.com/json-schema-org/JSON-Schema-Test-Suite#python
from proxi-schemas.
Here's a simple Python script that does exactly what we want:
"""
Validate API response with OpenAPI YAML schema.
For example:
$ python proxi_validator.py --openapi-yaml "specs/swagger.yaml" --request-url "http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250"
"""
import click
import requests
import yaml
from jsonschema import validate
from openapi_schema_to_json_schema import to_json_schema
def jsonschema_from_yaml_file(path_to_yaml):
"""Read OpenAPI schema yaml file and return as JSON Schema dictionary."""
with open(path_to_yaml, 'r') as stream:
open_api_schema = yaml.safe_load(stream)
jsonschema = to_json_schema(open_api_schema)
return jsonschema
def get_api_response(url):
"""Get API response JSON object."""
response = requests.get(url)
return response.json()
@click.command()
@click.argument("openapi-yaml", type=str)
@click.argument("request-url", type=str)
def main(**kwargs):
"""Validate API response with OpenAPI YAML schema."""
validate(
get_api_response(kwargs["request_url"]),
schema=jsonschema_from_yaml_file(kwargs["openapi_yaml"])
)
if __name__ == "__main__":
main()
A ValidationError is thrown if the API response is invalid for the given swagger.yaml file.
Do I open a PR to add the script to the repository?
from proxi-schemas.
Related Issues (20)
- do we really stipulate a max pageSize? HOT 1
- Massive data files do not exist
- /peptidoforms still labeled getPeptides
- inconsistent count* in Peptidoform
- What should the compact form of /peptidoforms return? HOT 2
- Add 500 to the list?
- msrun and filename HOT 8
- species as query parameter? HOT 2
- Error status should contain only error code HOT 1
- In the PSM object the score should be plural HOT 7
- Error status for multiple providers HOT 1
- PRIDE PROXI spectra returning mzs not in order HOT 3
- PRIDE USI without interpretation not working? HOT 1
- No attributes for spectra at PRIDE? HOT 2
- How should a spectrum status be used? HOT 2
- update specification and schemas
- Validator of the PROXI APIs implementations
- Improve documentation of the entry points in the swagger.yaml file
- Precursor m/z
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proxi-schemas.