Skip to content
Permalink
Browse files

Wrap into an OMG service.

  • Loading branch information...
Jude Pereira
Jude Pereira committed Aug 20, 2019
1 parent b8357ea commit 50c7d24f48feadfd300bfd4477e0ad5ff79379e5
Showing with 96 additions and 15 deletions.
  1. +11 −0 Dockerfile
  2. +5 −0 README.md
  3. +45 −0 microservice.yaml
  4. +0 −14 openapi2omg/Converter.py
  5. +33 −0 openapi2omg/Service.py
  6. +2 −1 requirements.txt
@@ -0,0 +1,11 @@
FROM python:3.7-alpine

RUN mkdir /app
ADD requirements.txt /app
RUN pip install -r /app/requirements.txt
RUN mkdir /app/openapi2omg
ADD . /app
WORKDIR /app
RUN python setup.py install

ENTRYPOINT ["python", "-m", "openapi2omg.Service"]
@@ -4,6 +4,11 @@ equivalent. Not all features are supported, and some assumptions have
been made. Please read the caveats section to understand these
assumptions.

## Storyscript
```coffee
result = openapi2omg convert spec: openApiSpec properties: {"serverIndex": 0}
```

## Caveats
### Assumptions
1. If there are multiple content types available for a given path, the content type
@@ -0,0 +1,45 @@
omg: 1
info:
version: 1.0.0
title: openapi2omg
description: |
This tool aims to convert an OpenAPI 3 spec to its corresponding OMG
equivalent. Not all features are supported, and some assumptions have
been made.
license:
name: MIT
url: https://opensource.org/licenses/MIT
lifecycle:
startup:
command: ["python", "-m", "openapi2omg.Service"]
actions:
convert:
help: Convert an OpenAPI document to OMG document
http:
path: /convert
port: 9000
method: post
contentType: application/json
arguments:
spec:
help: The OpenAPI spec, as a map
type: map
in: requestBody
required: true
properties:
help: Properties to be used for conversion
type: object
properties:
serverIndex:
help: |
If multiple servers are present in the OpenAPI spec,
this must be specified. The server selected for the OMG
spec will be based on this index.
type: int
output:
type: object
contentType: application/json
properties:
spec:
help: The converted OpenAPI document
type: map
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
from collections import namedtuple
import io
import json
import typing
import yaml

from prance import ResolvingParser
from urllib.parse import urlparse
@@ -310,15 +308,3 @@ def convert(self) -> dict:
phase.function()

return self.omg

def print(self):
# out = yaml.dump(self.convert(), allow_unicode=True)
print(json.dumps(self.convert(), indent=True))


if __name__ == '__main__':
with io.open('samples/petstore.json') as f:
converter = Converter(json.load(f), properties={
Properties.SERVER_INDEX: 0
})
converter.print()
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
import json

from flask import Flask, make_response, request

from openapi2omg.Converter import Converter


class Service:
app = Flask(__name__)

def convert(self):
req = request.get_json()
spec = req.get('spec')
props = req.get('properties')
try:
c = Converter(contents=spec, properties=props)
return self.end({'spec': c.convert()})
except BaseException as e:
return self.end({'error': str(e)}, 400)

@staticmethod
def end(res, response_code=200):
resp = make_response(json.dumps(res), response_code)
resp.headers['Content-Type'] = 'application/json; charset=utf-8'
return resp


if __name__ == '__main__':
service = Service()
service.app.add_url_rule('/convert', 'convert', service.convert,
methods=['post'])
service.app.run(host='0.0.0.0', port=9000)
@@ -1 +1,2 @@
prance[osv]==0.15.0
prance[osv]==0.15.0
Flask==1.0.2

0 comments on commit 50c7d24

Please sign in to comment.
You can’t perform that action at this time.