/
app.py
104 lines (87 loc) · 3.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
import newrelic.agent
from flask import Flask, request, jsonify, Response
import uuid
from flask_sqlalchemy import SQLAlchemy
from voluptuous import Invalid
from analytics.tasks import (
create_user_visit_helper,
get_visit_by_id_helper,
get_merchant_visit_helper,
)
from constants import ExceptionMessage
from exceptions import InputMalformed
from validator.request_validator import (
validate_extract_create_user_visit_input,
validate_extract_all_user_visit_input,
)
from database import metadata, get_db_session
app = Flask(__name__)
application = app
db = SQLAlchemy(metadata=metadata)
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger("app.py")
@app.route("/healthz")
def healthz():
return "Health Check Complete"
@app.route("/ready")
def ready():
return "Ready Check Complete"
@app.route("/api/v1/users/<user_id>/visits", methods=["POST"])
@newrelic.agent.function_trace()
def create_user_visit(user_id):
"""
Function creates a new visit given the input post data
:param user_id: Input param that represents the user_id
:return: Success: Response with json output with the requested format
"""
request_id = uuid.uuid4()
try:
data = validate_extract_create_user_visit_input(request)
log.info("[{}] User visits requested for user: {}".format(request_id, user_id))
return jsonify(
create_user_visit_helper(
get_db_session(), request_id, user_id, data.get("merchant")
)
)
except Invalid as e:
log.warning("[{}] Malformed input: {}".format(request_id, str(e)))
return Response(ExceptionMessage.BAD_REQUEST, status=406)
@app.route("/api/v1/users/<user_id>/visits", methods=["GET"])
@newrelic.agent.function_trace()
def get_merchant_visit(user_id):
"""
Function that given a search string, returns a list a merchant ids
that match the given threshold
:param user_id: Input param that represents the user_id
:return: Success: Response with list of objects
"""
request_id = uuid.uuid4()
try:
data = validate_extract_all_user_visit_input(request.args)
log.info("[{}] User visits requested for user: {}".format(request_id, user_id))
return jsonify(
get_merchant_visit_helper(
get_db_session(), request_id, user_id, data.get("searchString")
)
)
except Invalid as e:
log.warning(" Malformed input: {}".format(str(e)))
return Response(InputMalformed(ExceptionMessage.BAD_REQUEST), status=406)
@app.route("/api/v1/visit/<visit_id>", methods=["GET"])
@newrelic.agent.function_trace()
def get_single_visit_by_id(visit_id):
"""
Function gets a visit id by key
:param visit_id: Input param that represents the user_id
:return: Success: Response with json output with the requested format
"""
request_id = uuid.uuid4()
try:
log.info(
"[{}] User visits requested for visit ID: {}".format(request_id, visit_id)
)
return jsonify(get_visit_by_id_helper(get_db_session(), request_id, visit_id))
except Invalid as e:
log.warning(" Malformed input: {}".format(str(e)))
return Response(ExceptionMessage.BAD_REQUEST, status=406)