-
Notifications
You must be signed in to change notification settings - Fork 0
/
aml-server.py
111 lines (89 loc) · 3.26 KB
/
aml-server.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
105
106
107
108
109
110
111
#!/usr/bin/env python3
import os
import json
import yaml
import config
from flask import Flask, request, abort, jsonify
from flask_restful import Resource, Api
from pymongo import MongoClient
from bson.json_util import dumps
from bson import json_util
from bson import ObjectId
#import from the 21 Developer Library
from two1.lib.wallet import Wallet
from two1.lib.bitserv.flask import Payment
# set up server side wallet
app = Flask(__name__)
api = Api(app)
wallet = Wallet()
payment = Payment(app, wallet)
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
class ManifestService(Resource):
def get(self):
# Serves the app manifest to the 21 crawler.
with open('manifest.yaml', 'r') as f:
manifest_yaml = yaml.load(f)
return json.dumps(manifest_yaml)
class DbService():
def mongodb_conn():
try:
conn = MongoClient(config.mongo['connection'])
except pymongo.errors.ConnectionFailure as e:
print ("Could not connect to server: %s" % e)
return conn
class AmlClient(Resource):
def get(self):
return send_from_directory('static', 'aml-client.py')
class AmlService(Resource):
@payment.required(config.payment['fee'])
def get(self):
# Get parameters
if not request.args:
return {"status": "error", "message": "please provide at least one filter"}
_filter = {}
data = []
_first_name = request.args.get("first_name")
_last_name = request.args.get("last_name")
_dob = request.args.get("dob")
_nationality = request.args.get("nationality")
_group_type = request.args.get("group_type")
if _first_name is not None and _first_name != "":
_filter["first_name"] = _first_name
if _last_name is not None and _last_name != "":
_filter["last_name"] = _last_name
if _dob is not None and _dob != "":
_filter["dob"] = _dob
if _nationality is not None and _nationality != "":
_filter["nationality"] = _nationality
if _group_type is not None and _group_type != "":
_filter["group_type"] = _group_type
if len(_filter) == 0:
return {"status": "error", "message": "please provide at least one valid filter"}
try:
client = DbService.mongodb_conn()
except pymongo.errors.PyMongoError as msg:
return {"status": "error", "message": "could not connect to the aml database"}
print(client)
if client is None:
return {"status": "error", "message": "could not connect to the aml database"}
try:
db = client.aml
cursor = db.targets.find(_filter)[0:10]
except:
return {"status": "error", "message": "could not retrieve data"}
if cursor:
for target in cursor:
del target["_id"]
data.append(target)
return {"status": "ok", "data": data}
else:
return {"status": "error", "message": "error retrieving data"}
api.add_resource(AmlService, '/aml')
api.add_resource(AmlClient, '/client')
api.add_resource(ManifestService, '/manifest')
if __name__ == '__main__':
app.run(debug=config.debug)