From 126d0a14970cb83f7e6d96dd16676b072c476fae Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Tue, 5 Jan 2021 15:01:31 +0530 Subject: [PATCH 1/5] Add support for POST request method in v1 api for entity detection --- ner_v1/api.py | 132 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 106 insertions(+), 26 deletions(-) diff --git a/ner_v1/api.py b/ner_v1/api.py index 93c4b55e8..639b61678 100644 --- a/ner_v1/api.py +++ b/ner_v1/api.py @@ -258,6 +258,7 @@ def text(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def location(request): """This functionality calls the get_location() functionality to detect location. It is called through api call @@ -281,6 +282,7 @@ def location(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def phone_number(request): """This functionality calls the get_phone_number() functionality to detect phone numbers. It is called through api call @@ -290,8 +292,14 @@ def phone_number(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_phone_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -304,6 +312,7 @@ def phone_number(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def regex(request): """This functionality calls the get_regex() functionality to detect text those abide by the specified regex. It is called through api call @@ -313,8 +322,13 @@ def regex(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_regex(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -328,6 +342,7 @@ def regex(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def email(request): """This functionality calls the get_email() functionality to detect email. It is called through api call @@ -336,8 +351,13 @@ def email(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_email(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -350,6 +370,7 @@ def email(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def person_name(request): """This functionality calls the get_name() functionality to detect name. It is called through api call @@ -358,8 +379,13 @@ def person_name(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_person_name(message=parameters_dict[PARAMETER_MESSAGE], entity_name=parameters_dict[PARAMETER_ENTITY_NAME], structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE], @@ -375,6 +401,7 @@ def person_name(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def city(request): """This functionality calls the get_city() functionality to detect city. It is called through api call @@ -383,8 +410,13 @@ def city(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_city(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -399,6 +431,7 @@ def city(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def pnr(request): """This functionality calls the get_pnr() functionality to detect pnr. It is called through api call @@ -407,8 +440,13 @@ def pnr(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_pnr(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -421,6 +459,7 @@ def pnr(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def shopping_size(request): """This functionality calls the get_shopping_size() functionality to detect size. It is called through api call @@ -429,8 +468,13 @@ def shopping_size(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_shopping_size(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -443,6 +487,7 @@ def shopping_size(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def number(request): """This functionality calls the get_numeric() functionality to detect numbers. It is called through api call @@ -451,8 +496,13 @@ def number(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -468,6 +518,7 @@ def number(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def passenger_count(request): """This functionality calls the get_passenger_count() functionality to detect passenger count. It is called through api call @@ -480,8 +531,13 @@ def passenger_count(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_passenger_count(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -495,6 +551,7 @@ def passenger_count(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def time(request): """This functionality calls the get_time() functionality to detect time. It is called through api call @@ -504,8 +561,13 @@ def time(request): response (django.http.response.HttpResponse): HttpResponse object """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_time(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -519,6 +581,7 @@ def time(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def time_with_range(request): """This functionality calls the get_time_with_range() functionality to detect time. It is called through api call @@ -528,8 +591,13 @@ def time_with_range(request): response (django.http.response.HttpResponse): HttpResponse object """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_time_with_range(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -543,6 +611,7 @@ def time_with_range(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def date(request): """This functionality calls the get_date() functionality to detect date. It is called through api call @@ -551,8 +620,13 @@ def date(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_date(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -566,6 +640,7 @@ def date(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@csrf_exempt def budget(request): """This functionality calls the get_budget() functionality to detect budget. It is called through api call @@ -574,8 +649,13 @@ def budget(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) entity_output = get_budget(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], From 43c0b025ac0a82591c7cacb2dcdf2ff731479474 Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Wed, 6 Jan 2021 17:33:01 +0530 Subject: [PATCH 2/5] Add parameter `date_past_reference` in api_v2 parse POST request --- ner_v1/api.py | 2 ++ ner_v2/api.py | 1 + 2 files changed, 3 insertions(+) diff --git a/ner_v1/api.py b/ner_v1/api.py index 639b61678..5ba52d104 100644 --- a/ner_v1/api.py +++ b/ner_v1/api.py @@ -108,6 +108,8 @@ def parse_post_request(request): PARAMETER_PRIOR_RESULTS: request_data.get("predetected_values", []) } + ner_logger.info("parameters dict - {}".format(parameters_dict)) + return parameters_dict diff --git a/ner_v2/api.py b/ner_v2/api.py index ae9e61dc6..c863c44ae 100644 --- a/ner_v2/api.py +++ b/ner_v2/api.py @@ -72,6 +72,7 @@ def parse_post_request(request): PARAMETER_TIMEZONE: request_data.get('timezone'), PARAMETER_LANGUAGE_SCRIPT: request_data.get('language_script', ENGLISH_LANG), PARAMETER_SOURCE_LANGUAGE: request_data.get('source_language', ENGLISH_LANG), + PARAMETER_PAST_DATE_REFERENCED: request_data.get('date_past_reference', 'False'), PARAMETER_MIN_DIGITS: request_data.get('min_number_digits'), PARAMETER_MAX_DIGITS: request_data.get('max_number_digits'), PARAMETER_NUMBER_UNIT_TYPE: request_data.get('unit_type'), From c2c8dfe5d129aa427b1e38d950b24ad3ac674fbd Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Thu, 7 Jan 2021 10:06:27 +0530 Subject: [PATCH 3/5] - Add function to parse parameters from request object based on method to remov duplicate code - Add decoractor to only allow POST and GET method --- ner_v1/api.py | 155 ++++++++++++++++++-------------------------------- 1 file changed, 55 insertions(+), 100 deletions(-) diff --git a/ner_v1/api.py b/ner_v1/api.py index 5ba52d104..33f868c59 100644 --- a/ner_v1/api.py +++ b/ner_v1/api.py @@ -23,6 +23,7 @@ PARAMETER_READ_EMBEDDINGS_FROM_REMOTE_URL, PARAMETER_LIVE_CRF_MODEL_PATH) from django.views.decorators.csrf import csrf_exempt +from django.views.decorators.http import require_http_methods def to_bool(value): @@ -113,6 +114,29 @@ def parse_post_request(request): return parameters_dict +def parse_parameters_from_request(request): + """ + Parse parameters from request based on the method type. + It will return a parameters dict. + + Args: + request (django.http.HttpRequest): HTTP response from url + + Returns: + dict: parameters from the request + """ + parameters_dict = {} + if request.method == "POST": + parameters_dict = parse_post_request(request) + ner_logger.debug('Start POST : %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + elif request.method == "GET": + parameters_dict = get_parameters_dictionary(request) + ner_logger.debug('Start GET : %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + + return parameters_dict + + +@require_http_methods(["GET", "POST"]) @csrf_exempt def text(request): """ @@ -232,13 +256,7 @@ def text(request): ] """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start Bulk Detection: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_text( message=parameters_dict[PARAMETER_MESSAGE], entity_name=parameters_dict[PARAMETER_ENTITY_NAME], @@ -260,6 +278,7 @@ def text(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def location(request): """This functionality calls the get_location() functionality to detect location. It is called through api call @@ -269,8 +288,7 @@ def location(request): """ try: - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parameters_dict = parse_parameters_from_request(request) entity_output = get_location(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -284,6 +302,7 @@ def location(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def phone_number(request): """This functionality calls the get_phone_number() functionality to detect phone numbers. It is called through @@ -295,13 +314,7 @@ def phone_number(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_phone_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -314,6 +327,7 @@ def phone_number(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def regex(request): """This functionality calls the get_regex() functionality to detect text those abide by the specified regex. @@ -324,13 +338,7 @@ def regex(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_regex(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -344,6 +352,7 @@ def regex(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def email(request): """This functionality calls the get_email() functionality to detect email. It is called through api call @@ -353,13 +362,7 @@ def email(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_email(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -372,6 +375,7 @@ def email(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def person_name(request): """This functionality calls the get_name() functionality to detect name. It is called through api call @@ -381,13 +385,7 @@ def person_name(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_person_name(message=parameters_dict[PARAMETER_MESSAGE], entity_name=parameters_dict[PARAMETER_ENTITY_NAME], structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE], @@ -403,6 +401,7 @@ def person_name(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def city(request): """This functionality calls the get_city() functionality to detect city. It is called through api call @@ -412,13 +411,7 @@ def city(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_city(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -433,6 +426,7 @@ def city(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def pnr(request): """This functionality calls the get_pnr() functionality to detect pnr. It is called through api call @@ -442,13 +436,7 @@ def pnr(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_pnr(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -461,6 +449,7 @@ def pnr(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def shopping_size(request): """This functionality calls the get_shopping_size() functionality to detect size. It is called through api call @@ -470,13 +459,7 @@ def shopping_size(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_shopping_size(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -489,6 +472,7 @@ def shopping_size(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def number(request): """This functionality calls the get_numeric() functionality to detect numbers. It is called through api call @@ -498,13 +482,7 @@ def number(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -520,6 +498,7 @@ def number(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def passenger_count(request): """This functionality calls the get_passenger_count() functionality to detect passenger count. @@ -533,13 +512,7 @@ def passenger_count(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_passenger_count(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -553,6 +526,7 @@ def passenger_count(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def time(request): """This functionality calls the get_time() functionality to detect time. It is called through api call @@ -563,13 +537,7 @@ def time(request): response (django.http.response.HttpResponse): HttpResponse object """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_time(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -583,6 +551,7 @@ def time(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def time_with_range(request): """This functionality calls the get_time_with_range() functionality to detect time. It is called through api call @@ -593,13 +562,7 @@ def time_with_range(request): response (django.http.response.HttpResponse): HttpResponse object """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_time_with_range(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -613,6 +576,7 @@ def time_with_range(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def date(request): """This functionality calls the get_date() functionality to detect date. It is called through api call @@ -622,13 +586,7 @@ def date(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_date(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -642,6 +600,7 @@ def date(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) @csrf_exempt def budget(request): """This functionality calls the get_budget() functionality to detect budget. It is called through api call @@ -651,13 +610,7 @@ def budget(request): """ try: - parameters_dict = {} - if request.method == "POST": - parameters_dict = parse_post_request(request) - ner_logger.debug('Start POST %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) - elif request.method == "GET": - parameters_dict = get_parameters_dictionary(request) - ner_logger.debug('Start: GET %s ' % parameters_dict[PARAMETER_ENTITY_NAME]) + parameters_dict = parse_parameters_from_request(request) entity_output = get_budget(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], @@ -672,6 +625,7 @@ def budget(request): return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) def ner(request): """This functionality calls the run_ner() functionality to tag the message . It is called through api call @@ -691,6 +645,7 @@ def ner(request): return HttpResponse(json.dumps({'data': output}), content_type='application/json') +@require_http_methods(["GET", "POST"]) def combine_output(request): """This functionality calls the combine_output_of_detection_logic_and_tag() through api call From 3703a4764c8de7b53fcc536a2545676187377292 Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Thu, 7 Jan 2021 10:26:55 +0530 Subject: [PATCH 4/5] - Fixed typo for parameters dict --- ner_v1/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ner_v1/api.py b/ner_v1/api.py index 33f868c59..48ffabc26 100644 --- a/ner_v1/api.py +++ b/ner_v1/api.py @@ -288,7 +288,7 @@ def location(request): """ try: - parameters_dict = parameters_dict = parse_parameters_from_request(request) + parameters_dict = parse_parameters_from_request(request) entity_output = get_location(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME], parameters_dict[PARAMETER_STRUCTURED_VALUE], parameters_dict[PARAMETER_FALLBACK_VALUE], From c5339ced51f80827464e9ce47835ca1ac7f623a5 Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Fri, 8 Jan 2021 10:55:57 +0530 Subject: [PATCH 5/5] Change log level for parameter dict to debug from info when parsing request --- ner_v1/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ner_v1/api.py b/ner_v1/api.py index 48ffabc26..598165871 100644 --- a/ner_v1/api.py +++ b/ner_v1/api.py @@ -73,7 +73,7 @@ def get_parameters_dictionary(request): PARAMETER_LIVE_CRF_MODEL_PATH: request.GET.get('live_crf_model_path'), PARAMETER_PRIOR_RESULTS: json.loads(request.GET.get("predetected_values", '[]')) } - ner_logger.info("parameters dict - {}".format(parameters_dict)) + ner_logger.debug("parameters dict - {}".format(parameters_dict)) return parameters_dict @@ -109,7 +109,7 @@ def parse_post_request(request): PARAMETER_PRIOR_RESULTS: request_data.get("predetected_values", []) } - ner_logger.info("parameters dict - {}".format(parameters_dict)) + ner_logger.debug("parameters dict - {}".format(parameters_dict)) return parameters_dict