-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_service.py
More file actions
86 lines (70 loc) · 2.24 KB
/
run_service.py
File metadata and controls
86 lines (70 loc) · 2.24 KB
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
#!/usr/bin/env python
# encoding: utf-8
"""
Service interface for User Segmentation - Licious
"""
import os
import settings
from flask import (abort, Flask, Response, request, jsonify, make_response)
from service.rest_util import *
app = Flask(__name__)
ALLOWED_IPS = ['127.0.0.1']
# @app.route('/validatesingle/', methods=['GET'])
# def get_user_segment():
# try:
# user_id, segment_id = request.args['user_id'], request.args['segment_id']
# res = evaluate_user_segment(user_id, segment_id)
# except:
# return make_response({"success": False, "error_code": 400})
# return make_response({"success": True, "message": res})
#
@app.route('/validate/', methods=['POST'])
def get_user_segments():
res = dict()
query = request.json
try:
res = bulk_identify_segment(query.get('user_id'), query.get('segments'))
except:
return make_response({"success": False, "error_code": 400})
return make_response(jsonify(res))
@app.route('/user/', methods=['POST'])
def insert_user():
user_data = request.json
try:
if isinstance(user_data, list):
bulk_insert_users(user_data)
else:
insert_new_user(user_data)
except:
return make_response({"success": False, "error_code": 400})
return make_response({"success": True})
@app.route('/user/<id>/', methods=['GET'])
def user_detail(id):
try:
res = get_user(id)
except:
return make_response({"success": False, "error_code": 400})
return make_response(jsonify(res))
@app.route('/segment/', methods=['POST'])
def insert_segment():
segment_data = request.json
try:
if isinstance(segment_data, list):
bulk_insert_segments(segment_data)
else:
insert_new_segment(segment_data)
except:
return make_response({"success": False, "error_code": 400})
return make_response({"success": True})
@app.route('/segment/<id>/', methods=['GET'])
def segment_detail(id):
try:
res = get_segment(id)
except:
return make_response({"success": False, "error_code": 400})
return make_response(jsonify(res))
if __name__ == '__main__':
app.run(
host=settings.SERVICE_IP,
port=settings.SERVICE_PORT
)