-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
148 lines (130 loc) · 4.58 KB
/
application.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from flask import Flask, jsonify, request, abort, render_template
import requests
import json
import socket
import numpy as np
from sklearn import linear_model, datasets, svm
import csv
import pickle
import sys
TCP_IP = '192.168.18.60'
TCP_PORT = 30
accounts = [
{
'id': 1,
"first_name": u'Rich',
"last_name": u'Fairbanks',
"email": u'[email protected]',
'phone': 12345678,
'wifi_address': 123456789,
'card_number': 5412753456789010,
'rewards': 11567,
'customer_id': u'5938555aa73e4942cdafd84a',
'balance': 8158,
'type': u'Savings',
'id': u'5938555aa73e4942cdafd84a',
'nickname': u'Big Baller'
}
]
cards = []
application = Flask(__name__)
@application.route('/')
def index():
return render_template('purchase.html')
@application.route('/buy')
def buy():
# s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# s.connect((TCP_IP,TCP_PORT))
# s.send("B")
# s.close()
return render_template('buy.html')
@application.route('/accounts', methods=['GET'])
def get_accounts():
return jsonify({'accounts': accounts})
@application.route('/accounts/<int:account_id>', methods = ['GET'])
def get_account(account_id):
account = filter(lambda t: t['id'] == account_id, accounts)
if len(account) == 0:
abort(404)
return jsonify( { 'account': account[0] } )
@application.route('/card_number/<int:account_card_number>', methods = ['GET'])
def get_with_card_number(account_card_number):
account = filter(lambda x: x['card_number'] == account_card_number, accounts)
if len(account) == 0:
abort(404)
return jsonify( { 'account': account[0] } )
@application.route('/card_number/<int:account_wifi_address>', methods = ['GET'])
def get_with_wifi_address(account_wifi_address):
account = filter(lambda x: x['wifi_address'] == account_wifi_address, accounts)
if len(account) == 0:
abort(404)
return jsonify( { 'account': account[0] } )
@application.route('/accounts', methods = ['POST'])
def create_account():
#if not request.json or not 'first_name' in request.json:
#abort(400)
key = '96b793f91ac0b4f2fd94584dbf2e4e8f'
customer_id = '5938555aa73e4942cdafd84a'
customer_url = 'http://api.reimaginebanking.com/customers/'
url = customer_url + customer_id + '/accounts?key=' + key
result = requests.get(url).json()[0] # returns result as json
account = {
#'id': accounts[-1]['id'] + 1,
'first_name': request.json['first_name'],
'last_name': request.json.get('last_name', ""),
'phone': request.json.get('phone', ""),
'wifi_address': request.json.get('wifi_address', ""),
'card_number': request.json.get('card_number', ""),
'email': request.json.get('email', ""),
'rewards': result['rewards'],
'customer_id': result['customer_id'],
'balance': result['balance'],
'type': result['type'],
'balance': result['balance'],
'id': result['_id'],
'nickname': result['nickname']
}
accounts.append(account)
return jsonify( { 'account': account } ), 201
@application.route('/recommended_card', methods = ['GET'])
def get_recommended():
return jsonify( { 'recommended': cards[0]['recommended'] } )
@application.route('/new_card', methods = ['POST'])
def create_card():
#if not request.json or not 'first_name' in request.json:
#abort(400)
json = request.get_json(silent=True)
print json
sys.stdout.flush()
card = {
#'id': accounts[-1]['id'] + 1,
'age': request.json.get('age', ""),
'income': request.json.get('income', ""),
'cost_of_living': request.json.get('cost_of_living', ""),
'dependents': request.json.get('dependents', ""),
'spending/month': request.json.get('spending/month', ""),
'credit_score': request.json.get('credit_score', ""),
'delinquency': request.json.get('delinquency', ""),
'marital_status': request.json.get('marital_status', ""),
}
#print card
sys.stdout.flush()
x = []
for k in card:
print(card[k])
x.append(int(card[k]))
with open('logmodel.pkl', 'rb') as f:
logreg = pickle.load(f)
pkl_file = open('logmodel.pkl', 'rb')
pred = logreg.predict(x)
card['recommended'] = pred[0]
#print card
print pred[0]
sys.stdout.flush()
cards.append(card)
return jsonify( {'card': card} ), 201
@application.route('/cards', methods=['GET'])
def get_cards():
return jsonify({'cards': cards})
if __name__ == '__main__':
application.run(debug=True)