| 1 | import time |
| 2 | import jsonschema |
| 3 | try: |
| 4 | import simplejson as json |
| 5 | except ImportError: |
| 6 | import json |
| 7 | try: |
| 8 | from http import HTTPStatus |
| 9 | except ImportError: |
| 10 | import httplib as HTTPStatus |
| 11 | from flask import Flask |
| 12 | from flask import jsonify |
| 13 | from flask import request |
| 14 | from flasgger import Swagger |
| 15 | |
| 16 | |
| 17 | def drop_id_validate(data, schema): |
| 18 | """ |
| 19 | Custom validation function which drops parameter '_id' if present |
| 20 | in data |
| 21 | """ |
| 22 | jsonschema.validate(data, schema) |
| 23 | if data.get('_id') is not None: |
| 24 | del data['_id'] |
| 25 | |
| 26 | |
| 27 | def timestamping_validate(data, schema): |
| 28 | """ |
| 29 | Custom validation function which inserts a timestamp for when the |
| 30 | validation occurred |
| 31 | """ |
| 32 | jsonschema.validate(data, schema) |
| 33 | data['timestamp'] = str(time.time()) |
| 34 | |
| 35 | |
| 36 | def special_validate(data, schema): |
| 37 | """ |
| 38 | Custom validation function which inserts an special flag depending |
| 39 | on the cat's name |
| 40 | """ |
| 41 | jsonschema.validate(data, schema) |
| 42 | data['special'] = str(data['name'] == 'Garfield').lower() |
| 43 | |
| 44 | |
| 45 | def regular_validate(data, schema): |
| 46 | """ |
| 47 | Regular validation function |
| 48 | """ |
| 49 | jsonschema.validate(data, schema) |
| 50 | |
| 51 | |
| 52 | app = Flask(__name__) |
| 53 | swag = Swagger(app, validation_function=drop_id_validate) |
| 54 | |
| 55 | |
| 56 | @app.route('/cat', methods=['POST']) |
| 57 | @swag.validate('Cat') |
| 58 | def create_cat(): |
| 59 | """ |
| 60 | Cat creation endpoint which drops '_id' parameters when present in |
| 61 | request data |
| 62 | --- |
| 63 | tags: |
| 64 | - cat |
| 65 | summary: Creates a new cat |
| 66 | consumes: |
| 67 | - application/json |
| 68 | produces: |
| 69 | - application/json |
| 70 | parameters: |
| 71 | - in: body |
| 72 | name: body |
| 73 | description: |
| 74 | Cat object that needs to be persisted to the database |
| 75 | required: true |
| 76 | schema: |
| 77 | id: Cat |
| 78 | required: |
| 79 | - name |
| 80 | - address |
| 81 | properties: |
| 82 | name: |
| 83 | description: Cat's name |
| 84 | type: string |
| 85 | example: Sylvester |
| 86 | address: |
| 87 | description: Cat's house address |
| 88 | type: string |
| 89 | example: 4000 Warner Blvd., Burbank, CA 91522 |
| 90 | responses: |
| 91 | 200: |
| 92 | description: Successful operation |
| 93 | 400: |
| 94 | description: Invalid input |
| 95 | """ |
| 96 | return jsonify(request.json), HTTPStatus.OK |
| 97 | |
| 98 | |
| 99 | @app.route('/timestamped/cat', methods=['POST']) |
| 100 | @swag.validate('Cat', validation_function=timestamping_validate) |
| 101 | def create_timestamped_cat(): |
| 102 | """ |
| 103 | Cat creation endpoint which timestamps validated data |
| 104 | --- |
| 105 | tags: |
| 106 | - cat |
| 107 | summary: Creates a new cat |
| 108 | consumes: |
| 109 | - application/json |
| 110 | produces: |
| 111 | - application/json |
| 112 | parameters: |
| 113 | - in: body |
| 114 | name: body |
| 115 | description: |
| 116 | Cat object that needs to be persisted to the database |
| 117 | required: true |
| 118 | schema: |
| 119 | $ref: '#/definitions/Cat' |
| 120 | responses: |
| 121 | 200: |
| 122 | description: Successful operation |
| 123 | schema: |
| 124 | $ref: '#/definitions/Cat' |
| 125 | 400: |
| 126 | description: Invalid input |
| 127 | """ |
| 128 | return jsonify(request.json), HTTPStatus.OK |
| 129 | |
| 130 | |
| 131 | @app.route('/special/cat', methods=['POST']) |
| 132 | @swag.validate('Cat', validation_function=special_validate) |
| 133 | def create_special_cat(): |
| 134 | """ |
| 135 | Cat creation endpoint which timestamps validated data |
| 136 | --- |
| 137 | tags: |
| 138 | - cat |
| 139 | summary: Creates a new cat |
| 140 | consumes: |
| 141 | - application/json |
| 142 | produces: |
| 143 | - application/json |
| 144 | parameters: |
| 145 | - in: body |
| 146 | name: body |
| 147 | description: |
| 148 | Cat object that needs to be persisted to the database |
| 149 | required: true |
| 150 | schema: |
| 151 | $ref: '#/definitions/Cat' |
| 152 | responses: |
| 153 | 200: |
| 154 | description: Successful operation |
| 155 | schema: |
| 156 | $ref: '#/definitions/Cat' |
| 157 | 400: |
| 158 | description: Invalid input |
| 159 | """ |
| 160 | return jsonify(request.json), HTTPStatus.OK |
| 161 | |
| 162 | |
| 163 | @app.route('/regular/cat', methods=['POST']) |
| 164 | @swag.validate('Cat', validation_function=regular_validate) |
| 165 | def create_regular_cat(): |
| 166 | """ |
| 167 | Cat creation endpoint |
| 168 | --- |
| 169 | tags: |
| 170 | - cat |
| 171 | summary: Creates a new cat |
| 172 | consumes: |
| 173 | - application/json |
| 174 | produces: |
| 175 | - application/json |
| 176 | parameters: |
| 177 | - in: body |
| 178 | name: body |
| 179 | description: |
| 180 | Cat object that needs to be persisted to the database |
| 181 | required: true |
| 182 | schema: |
| 183 | $ref: '#/definitions/Cat' |
| 184 | responses: |
| 185 | 200: |
| 186 | description: Successful operation |
| 187 | schema: |
| 188 | $ref: '#/definitions/Cat' |
| 189 | 400: |
| 190 | description: Invalid input |
| 191 | """ |
| 192 | return jsonify(request.json), HTTPStatus.OK |
| 193 | |
| 194 | |
| 195 | def test_swag(client, specs_data): |
| 196 | """ |
| 197 | This test is runs automatically in Travis CI |
| 198 | |
| 199 | :param client: Flask app test client |
| 200 | :param specs_data: {'url': {swag_specs}} for every spec in app |
| 201 | """ |
| 202 | cat = { |
| 203 | '_id': "594dba7b2879334e411f3dcc", |
| 204 | 'name': "Tom", |
| 205 | 'address': "MGM, 245 N. Beverly Drive, Beverly Hills, CA 90210" |
| 206 | } |
| 207 | |
| 208 | with client.post( |
| 209 | '/cat', data=json.dumps(cat), content_type='application/json') as response: |
| 210 | assert response.status_code == HTTPStatus.OK |
| 211 | |
| 212 | received = json.loads(response.data.decode('utf-8')) |
| 213 | assert received.get('_id') is None |
| 214 | assert received.get('timestamp') is None |
| 215 | assert received.get('special') is None |
| 216 | try: |
| 217 | assert received.viewitems() < cat.viewitems() |
| 218 | except AttributeError: |
| 219 | assert received.items() < cat.items() |
| 220 | |
| 221 | with client.post( |
| 222 | '/timestamped/cat', data=json.dumps(cat), |
| 223 | content_type='application/json') as response: |
| 224 | assert response.status_code == HTTPStatus.OK |
| 225 | |
| 226 | received = json.loads(response.data.decode('utf-8')) |
| 227 | assert received.get('_id') == cat.get('_id') |
| 228 | assert received.get('timestamp') is not None |
| 229 | assert received.get('special') is None |
| 230 | try: |
| 231 | assert received.viewitems() > cat.viewitems() |
| 232 | except AttributeError: |
| 233 | assert received.items() > cat.items() |
| 234 | |
| 235 | with client.post( |
| 236 | '/special/cat', data=json.dumps(cat), |
| 237 | content_type='application/json') as response: |
| 238 | assert response.status_code == HTTPStatus.OK |
| 239 | |
| 240 | received = json.loads(response.data.decode('utf-8')) |
| 241 | assert received.get('_id') == cat.get('_id') |
| 242 | assert received.get('timestamp') is None |
| 243 | assert received.get('special') is not None |
| 244 | try: |
| 245 | assert received.viewitems() > cat.viewitems() |
| 246 | except AttributeError: |
| 247 | assert received.items() > cat.items() |
| 248 | |
| 249 | with client.post( |
| 250 | '/regular/cat', data=json.dumps(cat), |
| 251 | content_type='application/json') as response: |
| 252 | assert response.status_code == HTTPStatus.OK |
| 253 | |
| 254 | received = json.loads(response.data.decode('utf-8')) |
| 255 | assert received.get('_id') == cat.get('_id') |
| 256 | assert received.get('timestamp') is None |
| 257 | assert received.get('special') is None |
| 258 | try: |
| 259 | assert received.viewitems() == cat.viewitems() |
| 260 | except AttributeError: |
| 261 | assert received.items() == cat.items() |
| 262 | |
| 263 | if __name__ == "__main__": |
| 264 | app.run(debug=True) |