From c2bf5aebdfe2af7f0a93692776d52248d51e54d0 Mon Sep 17 00:00:00 2001 From: TonyJabbour Date: Mon, 4 Oct 2021 15:01:14 +0200 Subject: [PATCH] Add new API endpoint that return only content encoded in base64 --- bin/packages/Item.py | 54 +++++++++++++++++++++--- var/www/modules/restApi/Flask_restApi.py | 11 +++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/bin/packages/Item.py b/bin/packages/Item.py index 6ca7ba43..8331a33a 100755 --- a/bin/packages/Item.py +++ b/bin/packages/Item.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*-coding:UTF-8 -* - +import base64 import os import re import sys @@ -165,13 +165,13 @@ def get_item_languages(item_id, min_len=600, num_langs=3, min_proportion=0.2, mi # API def get_item(request_dict): if not request_dict: - return Response({'status': 'error', 'reason': 'Malformed JSON'}, 400) + return {'status': 'error', 'reason': 'Malformed JSON'}, 400 item_id = request_dict.get('id', None) if not item_id: - return ( {'status': 'error', 'reason': 'Mandatory parameter(s) not provided'}, 400 ) + return {'status': 'error', 'reason': 'Mandatory parameter(s) not provided'}, 400 if not exist_item(item_id): - return ( {'status': 'error', 'reason': 'Item not found'}, 404 ) + return {'status': 'error', 'reason': 'Item not found'}, 404 dict_item = {} dict_item['id'] = item_id @@ -216,7 +216,51 @@ def get_item(request_dict): if request_dict['cryptocurrency'].get('bitcoin'): dict_item['cryptocurrency']['bitcoin'] = get_item_bitcoin(item_id) - return (dict_item, 200) + return dict_item, 200 + + + + + + + +# API as txt +def get_item_as_txt(request_dict): + if not request_dict: + return {'status': 'error', 'reason': 'Malformed JSON'}, 400 + item_id = request_dict.get('id', None) + if not item_id: + return {'status': 'error', 'reason': 'Mandatory parameter(s) not provided'}, 400 + if not exist_item(item_id): + return {'status': 'error', 'reason': 'Item not found'}, 404 + + item_content = get_item_content(item_id) + encoded_item_content = item_content.encode("UTF-8") + base64_output = base64.b64encode(encoded_item_content) + dict_item = {'id': item_id, 'content': base64_output} + + return dict_item, 200 + + + + + + + + + + + + + + + + + + + + + ### diff --git a/var/www/modules/restApi/Flask_restApi.py b/var/www/modules/restApi/Flask_restApi.py index 8e96bf67..b5cc5457 100644 --- a/var/www/modules/restApi/Flask_restApi.py +++ b/var/www/modules/restApi/Flask_restApi.py @@ -313,6 +313,17 @@ def get_item_content(): return Response(json.dumps(res[0], indent=2, sort_keys=True), mimetype='application/json'), res[1] +@restApi.route("api/v1/get/item/content/text", methods=['POST']) +@token_required('read_only') +def get_item_content(): + + data = request.get_json() + item_id = data.get('id', None) + req_data = {'id': item_id} + res = Item.get_item_as_txt(req_data) + return Response(json.dumps(res[0], indent=2, sort_keys=True), mimetype='text/plain'), res[1] + + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # TAGS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #