Implement script to post-process OpenAPI generated schema
The script can be used to: - Expand aliases and anchors - Set API version - Set server URL - Enable API key authentication scheme Signed-off-by: Vasilis Tsiligiannis <acinonyx@openwrt.gr>spacecruft
parent
2b714a754e
commit
31320f739c
|
@ -43,23 +43,16 @@ schema:
|
||||||
image: ${GITLAB_CI_IMAGE_PYTHON}
|
image: ${GITLAB_CI_IMAGE_PYTHON}
|
||||||
script:
|
script:
|
||||||
- pip install --no-cache-dir --no-deps -r "requirements.txt" --force-reinstall .
|
- pip install --no-cache-dir --no-deps -r "requirements.txt" --force-reinstall .
|
||||||
- |
|
- >-
|
||||||
./manage.py generateschema | python -c "
|
./manage.py generateschema
|
||||||
# XXX: Workaround for https://github.com/encode/django-rest-framework/issues/7479
|
--title "SatNOGS DB"
|
||||||
# XXX: Remove after upgrade to DRF 3.12
|
--description "SatNOGS DB is a transmitter suggestions and crowd-sourcing app." |
|
||||||
import yaml
|
./contrib/postprocess-openapi-schema.py
|
||||||
import sys
|
--expand-aliases
|
||||||
class Dumper(yaml.Dumper):
|
--api-version '1'
|
||||||
def ignore_aliases(self, data):
|
--server-url 'https://db.satnogs.org'
|
||||||
return True
|
--enable-apikey-auth -
|
||||||
sys.stdout.buffer.write(
|
> satnogs-db-api-client/api-schema.yml
|
||||||
yaml.dump(
|
|
||||||
yaml.safe_load(sys.stdin),
|
|
||||||
default_flow_style=False,
|
|
||||||
sort_keys=False, Dumper=Dumper
|
|
||||||
).encode('utf-8')
|
|
||||||
)
|
|
||||||
" > satnogs-db-api-client/api-schema.yml
|
|
||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
when: always
|
when: always
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Script to postprocess OpenAPI generated schema
|
||||||
|
#
|
||||||
|
# Copyright (C) 2020 Libre Space Foundation <https://libre.space/>
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
class Dumper(yaml.Dumper):
|
||||||
|
def ignore_aliases(self, data):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="Postprocess OpenAPI generated schema")
|
||||||
|
parser.add_argument('--api-version', type=str, help='Set API version')
|
||||||
|
parser.add_argument('--server-url', type=str, help='Set API server base URL')
|
||||||
|
parser.add_argument('--expand-aliases', action='store_true', help='Expand anchors and aliases')
|
||||||
|
parser.add_argument(
|
||||||
|
'--enable-apikey-auth', action='store_true', help='Enable DRF API key authentication'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'input_file',
|
||||||
|
type=argparse.FileType('r', encoding='UTF-8'),
|
||||||
|
help='OpenAPI schema file or \'-\' for stdin'
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
schema = yaml.safe_load(args.input_file)
|
||||||
|
if args.api_version is not None:
|
||||||
|
schema['info']['version'] = args.api_version
|
||||||
|
|
||||||
|
if args.server_url is not None:
|
||||||
|
schema['servers'] = [{'url': args.server_url}]
|
||||||
|
|
||||||
|
if args.enable_apikey_auth:
|
||||||
|
schema.update(
|
||||||
|
{
|
||||||
|
'components': {
|
||||||
|
'securitySchemes': {
|
||||||
|
'ApiKeyAuth': {
|
||||||
|
'type': 'apiKey',
|
||||||
|
'in': 'header',
|
||||||
|
'name': 'Authorization'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'security': [
|
||||||
|
{
|
||||||
|
'ApiKeyAuth': []
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if args.expand_aliases:
|
||||||
|
schema_dump = yaml.dump(
|
||||||
|
schema, default_flow_style=False, sort_keys=False, Dumper=Dumper
|
||||||
|
).encode('utf-8')
|
||||||
|
else:
|
||||||
|
schema_dump = yaml.dump(schema, default_flow_style=False, sort_keys=False).encode('utf-8')
|
||||||
|
|
||||||
|
sys.stdout.buffer.write(schema_dump)
|
Loading…
Reference in New Issue