Skip to content

Commit

Permalink
update AsyncAPI document generation (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis authored Feb 13, 2023
1 parent ea0be36 commit e521731
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
10 changes: 10 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def tearDown(self):

pass

def test_asyncapi(self):
url = f'{self.endpoint}/asyncapi'
content = requests.get(url).json()

self.assertTrue('asyncapi' in content)
url = content['servers']['production']['url']
self.assertTrue(url == 'mqtt://mosquitto:1883')

self.assertTrue('origin/a/wis2' in content['channels'])

def test_admin(self):

url = f'{self.admin_endpoint}'
Expand Down
2 changes: 1 addition & 1 deletion wis2box_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
app = Flask(__name__, static_url_path='/static')
app.url_map.strict_slashes = False

app.register_blueprint(ASYNCAPI_BLUEPRINT, url_prefix='/asyncapi')
app.register_blueprint(ASYNCAPI_BLUEPRINT, url_prefix='/oapi')
app.register_blueprint(pygeoapi_blueprint, url_prefix='/oapi')
app.register_blueprint(ADMIN_BLUEPRINT, url_prefix='/oapi')

Expand Down
36 changes: 31 additions & 5 deletions wis2box_api/asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import logging
from typing import Any, Tuple, Union
from urllib.parse import urlparse

from pygeoapi.api import API, APIRequest, F_HTML, pre_process
from pygeoapi import l10n
Expand Down Expand Up @@ -64,6 +65,7 @@ def get_asyncapi(self, request: Union[APIRequest, Any]) -> Tuple[dict, int, str]
return self.get_format_exception(request)

headers = request.get_response_headers()
headers['Content-Type'] = 'application/json'

content = to_json(generate_asyncapi(self.config, request.locale),
self.pretty_print)
Expand Down Expand Up @@ -96,9 +98,15 @@ def generate_asyncapi(config: dict, locale: str) -> dict:
description = l10n.translate(config['metadata']['identification']['description'], locale) # noqa
tags = l10n.translate(config['metadata']['identification']['keywords'], locale) # noqa

url = wis2box_mqtt_url = os.environ.get('WIS2BOX_BROKER_PUBLIC')
u = urlparse(wis2box_mqtt_url)
auth = f'{u.username}:{u.password}@'
url = wis2box_mqtt_url.replace(auth, '')

a = {
'asyncapi': '2.4.0',
'asyncapi': '2.6.0',
'id': 'https://github.com/wmo-im/wis2box',
'defaultContentType': 'application/json',
'info': {
'version': __version__,
'title': title,
Expand All @@ -113,25 +121,43 @@ def generate_asyncapi(config: dict, locale: str) -> dict:
'email': config['metadata']['contact']['email']
}
},
'components': {
'operationTraits': {
'mqtt': {
'bindings': {
'mqtt': {
'qos': 1
}
}
}
}
},
'servers': {
'production': {
'url': os.environ.get('WIS2BOX_MQTT_URL'),
'url': url,
'protocol': 'mqtt',
'protocolVersion': '5.0',
'description': description
}
},
'channels': {
'origin/a/wis2': {
'description': 'Data notifications',
'subscribe': {
'operationId': 'ClientSubscribed',
'operationId': 'Notify',
'traits': [
{'$ref': '#/components/operationTraits/mqtt'}
],
'message': {
'$ref': 'https://raw.githubusercontent.com/wmo-im/wis2-notification-message/main/WIS2_Message_Format_Schema.yaml' # noqa
'$ref': 'https://raw.githubusercontent.com/wmo-im/wis2-notification-message/main/schemas/notificationMessageGeoJSON.yaml' # noqa
}
}
}
},
'tags': [{'name': tag} for tag in tags]
'tags': [{'name': tag} for tag in tags],
'externalDocs': {
'url': 'https://docs.wis2box.wis.wmo.int'
}
}

return a
2 changes: 1 addition & 1 deletion wis2box_api/flask_asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
)


@ASYNCAPI_BLUEPRINT.route('/')
@ASYNCAPI_BLUEPRINT.route('/asyncapi')
def home():
"""
AsyncAPI endpoint
Expand Down

0 comments on commit e521731

Please sign in to comment.