-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_swagger.sh
executable file
·221 lines (218 loc) · 10.7 KB
/
generate_swagger.sh
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
echo '{
"basePath": "https://beta.openphacts.org/2.0",
"apiVersion": "v2.0",
"apis": ['
lastfile=`ls *.ttl | tail -1`
for file in ./*.ttl
do
echo ' {'
path=`sed -n 's,[[:space:]]*api:uriTemplate[[:space:]]*, "path": ,p' $file | sed 's/;/,/' | sed 's/[?{][[:print:]]*"/"/'`
if [[ "$path" == */units/* ]]
then
echo '"path": "/pharmacology/filters/units/{act_type}" ,'
else
echo $path
fi
echo ' "operations": [
{
"httpMethod": "GET",'
sed -n '/a [[:print:]]*Endpoint/,/api:name/s/[[:space:]]*api:name/ "summary": /p' $file | sed 's/;/,/'
sed -n '/a api:ExternalHTTPService/,/api:name/s/[[:space:]]*api:name/ "summary": /p' $file | sed 's/;/,/'
sed -n -e '/a [[:print:]]*Endpoint/,/api:description/s/[[:space:]]*api:description/ "description": /p' $file | sed 's/"[[:space:]]*[,.;][[:space:]]*$/<br><hr><br>Response template: <br><br><hr><br> /'| tr '\n' ' '
sed -n -e '/a api:ExternalHTTPService/,/api:description/s/[[:space:]]*api:description/ "description": /p' $file | sed 's/"[[:space:]]*[,.;][[:space:]]*$/<br><hr><br>Response template: <br><br><hr><br> /' | tr '\n' ' '
cat docs/`basename $file .ttl`.doc | sed -e 's,<,\<,g' -e 's,>,\>,g' -e 's,<span,<span,' -e 's,">,">,' -e 's,</span>,</span>,' -e 's,$,<br>,' -e 's,\t,\ \ \ \ ,g' | tr '\n' ' ' | sed 's/$/",\
/'
sed -n '/a api:API/,/rdfs:label/s/[[:space:]]*rdfs:label \([[:print:]]*\)"[[:print:]]*/ "group": \1"/p' $file | sed 's/$/ ,/'
echo ' "parameters": ['
inputLine=`sed -n '/<#input>/p' $file`
if [[ "$inputLine" != "" ]]
then
echo ' {'
sed -n "/^[[:space:]]*<#input>/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/'
sed -n "/^[[:space:]]*<#input>/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/'
echo ' "dataType": "string",'
echo ' "required": true,'
echo ' "paramType": "query"'
echo ' },'
fi
echo ' {
"name": "app_id",
"description": "Your access application id",
"dataType": "string",
"paramType": "query",
"threescale_name": "app_ids"
},
{
"name": "app_key",
"description": "Your access application key",
"dataType": "string",
"paramType": "query",
"threescale_name": "app_keys"
},'
vars=`sed -n 's/[[:space:]]*api:variable[[:space:]]*//p' $file | sed 's,[[:space:]]*;[[:space:]]*$,,'`
for var in `sed -n 's/[[:space:]]*api:variable[[:space:]]*//p' $file | sed 's,[[:space:]]*;[[:space:]]*$,,' | grep -v '/'`
do
if [[ `echo $var | sed 's,:[[:print:]]*$,,'` == ops_api ]]
then
for sub_var in `sed -n "/^[[:space:]]*$var/,/^[[:space:]]*$/p" $file | sed -n 's,[[:space:]]*api:subType[[:space:]]*,,p' | sed 's,[[:space:]]*.[[:space:]]*$,,'`
do
echo ' {'
echo ' "name": "'`sed -n "/$var[[:space:]]*api:name/p" $file | sed 's/[[:print:]]*[[:space:]][[:space:]]*"//' | sed 's/"[[:print:]]*//'`.`echo $sub_var | sed 's,^[[:print:]]*:,,'`'",'
sed -n "/^[[:space:]]*$sub_var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/'
echo ' "paramType": "query",'
if [[ "$sub_var" == "ops_api_search:Molecule" ]]
then
echo ' "required": true,'
fi
echo ' "dataType": "string"'
echo ' },'
done
elif [[ "$var" != "<#input>" ]]
then
echo ' {'
sed -n "/^[[:space:]]*$var/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/'
sed -n "/^[[:space:]]*$var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/'
if [[ "$var" == "_:act_type" ]]
then
echo ' "required": true,'
echo ' "paramType": "path",'
echo ' "dataType": "string"'
elif [[ "$var" == "chembl:standardValue" || "$var" == "chembl:pChembl" || "$var" == "schembl:SCCO_000028" || "$var" == "schembl:SCCO_000038" ]]
then
echo ' "dataType": "double",'
echo ' "paramType": "query"'
echo ' },'
echo ' {'
sed -n "/^[[:space:]]*$var/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/' | sed 's,\"name\":[[:space:]]*\",&min-,'
sed -n "/^[[:space:]]*$var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/' | sed 's/equal/greater than or &/'
echo ' "dataType": "double",'
echo ' "paramType": "query"'
echo ' },'
echo ' {'
sed -n "/^[[:space:]]*$var/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/' | sed 's,\"name\":[[:space:]]*\",&minEx-,'
sed -n "/^[[:space:]]*$var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/' | sed 's/equal to/greater than/'
echo ' "dataType": "double",'
echo ' "paramType": "query"'
echo ' },'
echo ' {'
sed -n "/^[[:space:]]*$var/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/' | sed 's,\"name\":[[:space:]]*\",&max-,'
sed -n "/^[[:space:]]*$var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/' | sed 's/equal/less than or &/'
echo ' "dataType": "double",'
echo ' "paramType": "query"'
echo ' },'
echo ' {'
sed -n "/^[[:space:]]*$var/s/[[:space:]]*[[:print:]]*api:name/ *name*: /p" $file | sed 's,*,",g' | sed 's/.$/,/' | sed 's,\"name\":[[:space:]]*\",&maxEx-,'
sed -n "/^[[:space:]]*$var/,/api:value/s/[[:space:]]*[[:print:]]*api:value/ *description*: /p" $file | sed 's,*,",g' | sed 's/[[:space:]]*.[[:space:]]*$/ ,/' | sed 's/equal to/less than/'
echo ' "dataType": "double",'
echo ' "paramType": "query"'
else
echo ' "paramType": "query",'
if [[ "$var" == "<#smiles>" || "$var" == "<#uuid>" || "$var" == "<#q>" || "$var" == "<#URL>" || "$var" == "chemspider:inchikey" || "$var" == "chemspider:inchi" ]]
then
echo ' "required": true,'
fi
echo ' "dataType": "string"'
fi
echo ' },'
fi
done
if [[ `sed -n '/api:ListEndpoint/p' $file` ]] || [[ `sed -n '/api:IntermediateExpansionEndpoint/p' $file` ]]
then
echo ' {
"name": "_page",
"description": "A number; the page that should be viewed",
"dataType": "integer",
"paramType": "query"
},
{
"name": "_pageSize",
"description": "The desired page size. Set to 'all' to retrieve all results in a single page.",
"dataType": "integer",
"paramType": "query"
},'
echo ' {
"name": "_orderBy",
"description": "The desired variable to sort by. Multiple values can be specified seperated by spaces. Direction of sort can be specified with ASC(?var) and DESC(?var). Default is ascending",
"allowableValues": {
"values": ['
for sparql_var in `sed 's,[[:space:]],\n,g' $file | sed 's/[(){}]//g' |sed -n '/^?/p' | sed 's/[\.;,]$//' | grep -v '</span>' | sort | uniq`
do
echo ' "'$sparql_var'"',
if [[ "$sparql_var" == `sed 's,[[:space:]],\n,g' $file | sed 's/[(){}]//g' |sed -n '/^?/p' | sed 's/[\.;,]$//' | grep -v '</span>' | sort | uniq | tail -n 1` ]]
then
echo ' "DESC('$sparql_var')"'
else
echo ' "DESC('$sparql_var')"',
fi
done
echo ' ],
"valueType": "LIST"
},
"dataType": "string",
"required": false,
"paramType": "query"
},'
fi
if [[ ! `sed -n '/api:ExternalHTTPService/p' $file` ]]
then
echo ' {
"name": "_lens",
"description": "The Lens name",
"dataType": "string",
"paramType": "query"
},'
fi
echo ' {
"name": "_format",
"description": "The desired result format.",
"allowableValues": {
"values": [
"json",
"tsv",
"ttl",
"xml",
"rdf",
"rdfjson",
"html"
],
"valueType": "LIST"
},
"dataType": "string",
"required": false,
"paramType": "query"
},
{
"name": "_callback",
"description": "For JSONP",
"dataType": "string",
"paramType": "query"
},
{
"name": "_metadata",
"description": "Additional metadata to be included with response.",
"allowableValues": {
"values": [
"execution",
"site",
"formats",
"views",
"all"
],
"valueType": "LIST"
},
"dataType": "string",
"required": false,
"paramType": "query"
}
]
}
]'
if [[ $file == *$lastfile* ]]
then
echo ' }'
else
echo ' },'
fi
done
echo ' ]
}'