diff --git a/src/ign-api/run.py b/src/ign-api/run.py index 717f24b..de0fda2 100755 --- a/src/ign-api/run.py +++ b/src/ign-api/run.py @@ -85,42 +85,48 @@ def post(self): return response -@app.route(API_VERSION_URL + "/quantize", methods=["POST"]) -def quantize(): - go_nord = setup_instance(request) - output_path = '' - response = {'success': True} +@api.route('/quantize', doc={'description': 'Quantize image using pillow'}) +class ConvertEndpoint(Resource): - if request.files.get('file') != None: - image = go_nord.open_image(request.files.get('file').stream) - elif request.form.get('file_path') != None: - image = go_nord.open_image(request.form.get('file_path')) - elif request.form.get('b64_input') != None: - image = go_nord.base64_to_image(request.form.get('b64_input')) - else: - abort(400, 'You need to provide at least a valid image or image path') + @api.expect(parser) + @api.response(200, 'Success, image quantized', converted_image_model) + @api.response(400, 'Validation Error, something go wrong') + def post(self): + + go_nord = setup_instance(request) + output_path = '' + response = {'success': True} + + if request.files.get('file'): + image = go_nord.open_image(request.files.get('file').stream) + elif request.form.get('file_path'): + image = go_nord.open_image(request.form.get('file_path')) + elif request.form.get('b64_input'): + image = go_nord.base64_to_image(request.form.get('b64_input')) + else: + abort(400, 'You need to provide at least a valid image or image path') - if request.form.get('width') and request.form.get('height'): - image = go_nord.resize_image(image) + if request.form.get('width') and request.form.get('height'): + image = go_nord.resize_image(image) - if request.form.get('output_path') != None: - output_path = request.form.get('output_path') + if request.form.get('output_path'): + output_path = request.form.get('output_path') - image = go_nord.quantize_image(image, save_path=output_path) + image = go_nord.quantize_image(image, save_path=output_path) - if request.form.get('b64_output') != None: - b64_image = go_nord.image_to_base64(image, 'jpeg') - base64_img_string = b64_image.decode('UTF-8') - response['b64_img'] = base64_img_string + if request.form.get('b64_output'): + b64_image = go_nord.image_to_base64(image, 'jpeg') + base64_img_string = b64_image.decode('UTF-8') + response['b64_img'] = base64_img_string - return jsonify(response) + return response def setup_instance(req): go_nord = GoNord() hex_colors = [] - if req.form.get('colors') != None: + if req.form.get('colors'): hex_colors = req.form.get('colors').split(',') if len(hex_colors) > 0: @@ -128,17 +134,17 @@ def setup_instance(req): for hex_color in hex_colors: go_nord.add_color_to_palette(hex_color) - if req.form.get('is_avg') != None: + if req.form.get('is_avg'): go_nord.enable_avg_algorithm() - if req.form.get('avg_box_width') != None and req.form.get('avg_box_height') != None: + if req.form.get('avg_box_width') and req.form.get('avg_box_height'): go_nord.set_avg_box_data(int(req.form.get('avg_box_width')), int(req.form.get('avg_box_height'))) - if req.form.get('blur') != None: + if req.form.get('blur'): go_nord.enable_gaussian_blur() return go_nord if __name__ == '__main__': - app.run(port=8000, host='0.0.0.0', threaded=True, debug=True) + app.run(port=8000, threaded=True) diff --git a/src/ign-api/run_test.py b/src/ign-api/run_test.py index cf14f9d..37398a7 100644 --- a/src/ign-api/run_test.py +++ b/src/ign-api/run_test.py @@ -49,3 +49,22 @@ def test_convert_api_no_data_provided(client: FlaskClient): data = {'b64_output': True} response = client.post(URL, data=data, content_type='multipart/form-data') assert response.status_code == 400 + + +def test_quantize_api(client: FlaskClient, base64_pixel): + URL = API_VERSION + '/quantize' + data = {'b64_input': base64_pixel, 'b64_output': True} + response = client.post(URL, data=data, content_type='multipart/form-data') + assert response.status_code == 200 + + json = response.json + assert 'success' in json.keys() + assert json['success'] + assert 'b64_img' in json.keys() + + +def test_quantize_api_no_data_provided(client: FlaskClient): + URL = API_VERSION + '/quantize' + data = {'b64_output': True} + response = client.post(URL, data=data, content_type='multipart/form-data') + assert response.status_code == 400