|
| 1 | +import yaml |
1 | 2 | from aiohttp.web_request import Request |
2 | 3 |
|
3 | 4 | from api_tabular import config |
@@ -59,3 +60,241 @@ def url_for(request: Request, route: str, *args, **kwargs): |
59 | 60 | if kwargs.pop("_external", None): |
60 | 61 | return external_url(router[route].url_for(**kwargs)) |
61 | 62 | return router[route].url_for(**kwargs) |
| 63 | + |
| 64 | + |
| 65 | +def swagger_parameters(resource_columns): |
| 66 | + parameters_list = [ |
| 67 | + { |
| 68 | + 'name': 'rid', |
| 69 | + 'in': 'path', |
| 70 | + 'description': 'ID of resource to return', |
| 71 | + 'required': True, |
| 72 | + 'schema': { |
| 73 | + 'type': 'string' |
| 74 | + } |
| 75 | + }, |
| 76 | + { |
| 77 | + 'name': 'page', |
| 78 | + 'in': 'query', |
| 79 | + 'description': 'Specific page', |
| 80 | + 'required': False, |
| 81 | + 'schema': { |
| 82 | + 'type': 'string' |
| 83 | + } |
| 84 | + }, |
| 85 | + { |
| 86 | + 'name': 'page_size', |
| 87 | + 'in': 'query', |
| 88 | + 'description': 'Number of results per page', |
| 89 | + 'required': False, |
| 90 | + 'schema': { |
| 91 | + 'type': 'string' |
| 92 | + } |
| 93 | + } |
| 94 | + ] |
| 95 | + for key, value in resource_columns.items(): |
| 96 | + parameters_list.extend( |
| 97 | + [ |
| 98 | + { |
| 99 | + 'name': f'sort ascending {key}', |
| 100 | + 'in': 'query', |
| 101 | + 'description': f'{key}__sort=asc.', |
| 102 | + 'required': False, |
| 103 | + 'schema': { |
| 104 | + 'type': 'string' |
| 105 | + } |
| 106 | + }, |
| 107 | + { |
| 108 | + 'name': f'sort descending {key}', |
| 109 | + 'in': 'query', |
| 110 | + 'description': f'{key}__sort=desc.', |
| 111 | + 'required': False, |
| 112 | + 'schema': { |
| 113 | + 'type': 'string' |
| 114 | + } |
| 115 | + } |
| 116 | + ] |
| 117 | + ) |
| 118 | + if value['python_type'] == 'string': |
| 119 | + parameters_list.extend( |
| 120 | + [ |
| 121 | + { |
| 122 | + 'name': f'exact {key}', |
| 123 | + 'in': 'query', |
| 124 | + 'description': f'{key}__exact=value.', |
| 125 | + 'required': False, |
| 126 | + 'schema': { |
| 127 | + 'type': 'string' |
| 128 | + } |
| 129 | + }, |
| 130 | + { |
| 131 | + 'name': f'contains {key}', |
| 132 | + 'in': 'query', |
| 133 | + 'description': f'{key}__contains=value.', |
| 134 | + 'required': False, |
| 135 | + 'schema': { |
| 136 | + 'type': 'string' |
| 137 | + } |
| 138 | + } |
| 139 | + ] |
| 140 | + ) |
| 141 | + elif value['python_type'] == 'float': |
| 142 | + parameters_list.extend( |
| 143 | + [ |
| 144 | + { |
| 145 | + 'name': f'{key} less', |
| 146 | + 'in': 'query', |
| 147 | + 'description': f'{key}__less=value.', |
| 148 | + 'required': False, |
| 149 | + 'schema': { |
| 150 | + 'type': 'string' |
| 151 | + } |
| 152 | + }, |
| 153 | + { |
| 154 | + 'name': f'{key} greater', |
| 155 | + 'in': 'query', |
| 156 | + 'description': f'{key}__greater=value.', |
| 157 | + 'required': False, |
| 158 | + 'schema': { |
| 159 | + 'type': 'string' |
| 160 | + } |
| 161 | + } |
| 162 | + ] |
| 163 | + ) |
| 164 | + return parameters_list |
| 165 | + |
| 166 | + |
| 167 | +def swagger_component(resource_columns): |
| 168 | + resource_prop_dict = {} |
| 169 | + for key, value in resource_columns.items(): |
| 170 | + type = 'string' |
| 171 | + if value['python_type'] == 'float': |
| 172 | + type = 'integer' |
| 173 | + resource_prop_dict.update({ |
| 174 | + f'{key}': { |
| 175 | + 'type': f'{type}' |
| 176 | + } |
| 177 | + }) |
| 178 | + component_dict = { |
| 179 | + 'schemas': { |
| 180 | + 'ResourceData': { |
| 181 | + 'type': 'object', |
| 182 | + 'properties': { |
| 183 | + 'data': { |
| 184 | + 'type': 'array', |
| 185 | + 'items': { |
| 186 | + '$ref': '#/components/schemas/Resource' |
| 187 | + } |
| 188 | + }, |
| 189 | + 'link': { |
| 190 | + 'type': 'object', |
| 191 | + 'properties': { |
| 192 | + 'profile': { |
| 193 | + 'description': 'Link to the profile endpoint of the resource', |
| 194 | + 'type': 'string' |
| 195 | + }, |
| 196 | + 'next': { |
| 197 | + 'description': 'Pagination link to the next page of the resource data', |
| 198 | + 'type': 'string' |
| 199 | + }, |
| 200 | + 'prev': { |
| 201 | + 'description': 'Pagination link to the previous page of the resource data', |
| 202 | + 'type': 'string' |
| 203 | + } |
| 204 | + } |
| 205 | + }, |
| 206 | + 'meta': { |
| 207 | + 'type': 'object', |
| 208 | + 'properties': { |
| 209 | + 'page': { |
| 210 | + 'description': 'Current page', |
| 211 | + 'type': 'integer' |
| 212 | + }, |
| 213 | + 'page_size': { |
| 214 | + 'description': 'Number of results per page', |
| 215 | + 'type': 'integer' |
| 216 | + }, |
| 217 | + 'total': { |
| 218 | + 'description': 'Total number of results', |
| 219 | + 'type': 'integer' |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + }, |
| 225 | + 'Resource': { |
| 226 | + 'type': 'object', |
| 227 | + 'properties': resource_prop_dict |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + return component_dict |
| 232 | + |
| 233 | + |
| 234 | +def build_swagger_file(resource_columns, rid): |
| 235 | + parameters_list = swagger_parameters(resource_columns) |
| 236 | + component_dict = swagger_component(resource_columns) |
| 237 | + swagger_dict = { |
| 238 | + 'openapi': '3.0.3', |
| 239 | + 'info': { |
| 240 | + 'title': 'Resource data API', |
| 241 | + 'description': 'Retrieve data for a specified resource with optional filtering and sorting.', |
| 242 | + 'version': '1.0.0' |
| 243 | + }, |
| 244 | + 'tags': { |
| 245 | + 'name': 'Data retrieval', |
| 246 | + 'description': 'Retrieve data for a specified resource' |
| 247 | + }, |
| 248 | + 'paths': { |
| 249 | + f'/api/resources/{rid}/data/': { |
| 250 | + 'get': { |
| 251 | + 'description': 'Returns resource data based on ID.', |
| 252 | + 'summary': 'Find resource by ID', |
| 253 | + 'operationId': 'getResourceById', |
| 254 | + 'responses': { |
| 255 | + '200': { |
| 256 | + 'description': 'successful operation', |
| 257 | + 'content': { |
| 258 | + 'application/json': { |
| 259 | + 'schema': { |
| 260 | + '$ref': '#/components/schemas/ResourceData' |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + }, |
| 265 | + '400': { |
| 266 | + 'description': 'Invalid query string' |
| 267 | + }, |
| 268 | + '404': { |
| 269 | + 'description': 'Resource not found' |
| 270 | + } |
| 271 | + } |
| 272 | + }, |
| 273 | + 'parameters': parameters_list |
| 274 | + }, |
| 275 | + f'/api/resources/{rid}/data/csv/': { |
| 276 | + 'get': { |
| 277 | + 'description': 'Returns resource data based on ID as a CSV file.', |
| 278 | + 'summary': 'Find resource by ID in CSV', |
| 279 | + 'operationId': 'getResourceByIdCSV', |
| 280 | + 'responses': { |
| 281 | + '200': { |
| 282 | + 'description': 'successful operation', |
| 283 | + 'content': { |
| 284 | + 'text/csv': {} |
| 285 | + } |
| 286 | + }, |
| 287 | + '400': { |
| 288 | + 'description': 'Invalid query string' |
| 289 | + }, |
| 290 | + '404': { |
| 291 | + 'description': 'Resource not found' |
| 292 | + } |
| 293 | + } |
| 294 | + }, |
| 295 | + 'parameters': parameters_list |
| 296 | + } |
| 297 | + }, |
| 298 | + 'components': component_dict |
| 299 | + } |
| 300 | + return yaml.dump(swagger_dict) |
0 commit comments