-
Notifications
You must be signed in to change notification settings - Fork 5
/
samsa.py
137 lines (119 loc) · 3.99 KB
/
samsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from bottle import Bottle, request, response, route
import subprocess, random, io, string
app = Bottle()
@app.error(404)
def error404(error):
return "This is samsa. Try /swagger"
@app.route('/convert/v2tov3', method='POST')
@app.route('/v2tov3', method='POST')
def convert_v2_to_v3():
try:
syntax = request.query['format']
except:
syntax = "json"
r = str(random.randint(1000,9999))
f = "/tmp/" + r + ".api"
with io.open(f, 'wb') as outfile:
outfile.write(request.body.read())
outfile.close()
converted = "/tmp/" + r + ".api_out"
output = subprocess.check_output(['api-spec-converter', '-s', syntax, '--from=swagger_2', '--to=openapi_3', f])
if syntax == "json":
response.headers['Content-Type'] = 'application/json; charset=UTF-8'
else:
response.headers['Content-Type'] = 'text/yaml; charset=UTF-8'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS, HEAD'
return output
@app.route('/convert/v3tov2', method='POST')
@app.route('/v3tov2', method='POST')
def convert_v3_to_v2():
try:
syntax = request.query['format']
except:
syntax = "json"
r = str(random.randint(1000,9999))
f = "/tmp/" + r + ".api"
with io.open(f, 'wb') as outfile:
outfile.write(request.body.read())
outfile.close()
converted = "/tmp/" + r + ".api_out"
output = subprocess.check_output(['api-spec-converter', '-s', syntax, '--from=openapi_3', '--to=swagger_2', f])
if syntax == "json":
response.headers['Content-Type'] = 'application/json; charset=UTF-8'
else:
response.headers['Content-Type'] = 'text/yaml; charset=UTF-8'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS, HEAD'
return output
@app.get('/convert/swagger')
@app.get('/swagger')
@app.get('/swagger.json')
@app.get('/convert/swagger.json')
def swagger():
swagger = '''{
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "samsa",
"description": "Convert between OpenAPI v2 and v3 formats (YAML/JSON)"
},
"basePath": "/convert",
"paths": {
"/v3tov2": {
"post": {
"operationId": "POST_v3tov2",
"summary": "OpenAPI 3.0 to Swagger",
"parameters": [
{
"name": "format",
"in": "query",
"type": "string",
"enum": [
"json",
"yaml"
]
}
],
"responses": {
"200": {
"description": ""
},
"400": {
"description": ""
}
}
}
},
"/v2tov3": {
"post": {
"operationId": "v2tov3",
"summary": "Swagger to OpenAPI 3.0",
"parameters": [
{
"name": "format",
"in": "query",
"type": "string",
"enum": [
"json",
"yaml"
]
}
],
"responses": {
"200": {
"description": ""
},
"400": {
"description": ""
}
}
}
}
},
"definitions": {}
}'''
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS, HEAD'
response.headers['Content-Type'] = 'application/json; charset=UTF-8'
return swagger