-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_server.py
More file actions
148 lines (111 loc) · 4.17 KB
/
rest_server.py
File metadata and controls
148 lines (111 loc) · 4.17 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
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
app = Flask(__name__)
api = None
from functools import wraps
from flask import request, Response
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
# Get username from config file or so
if username == 'admin' and password == 'secret':
return True
else:
return False
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@app.route("/inventory", methods=["GET"])
@requires_auth
def get_inventory():
inventory_items = api.get_inventory().call()['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']
return jsonify(inventory_items)
@app.route("/evolve", methods=["POST"])
def evolve():
try:
poke_id = request.form.get('pokeid', type=int)
response = api.evolve_pokemon(pokemon_id=poke_id).call()
print(response.get('responses').get('EVOLVE_POKEMON'))
return jsonify(response.get('responses').get('EVOLVE_POKEMON'))
except Exception as e:
print(e)
return jsonify({"status": "error"})
@app.route("/nickname", methods=["POST"])
def nickname():
nickname = request.form.get('nickname', type=str)
response = api.claim_codename(codename=nickname).call()
return jsonify(response)
@app.route("/release", methods=["POST"])
def release():
try:
poke_id = request.form.get('pokeid', type=int)
response = api.release_pokemon(pokemon_id=poke_id).call()
print(response.get('responses').get('RELEASE_POKEMON',99))
return jsonify(response.get('responses').get('RELEASE_POKEMON',99))
except Exception as e:
print(e)
return jsonify({"status": "error"})
@app.route("/position", methods=["GET"])
def get_position():
lat, lng, alt = api.get_position()
pos = {"lat": lat, "lng": lng, "alt": alt}
return jsonify(pos)
@app.route("/position", methods=["POST"])
def set_position():
# data=lat=0.0&lng=0.0&alt=0.0
try:
lat = request.form.get('lat', type=float)
lng = request.form.get('lng', type=float)
alt = request.form.get('alt', type=float)
api.set_position(lat, lng, alt)
return jsonify({"status": "ok"})
except:
return jsonify({"status": "error"})
@app.route("/pokemon_count", methods=["GET"])
def get_pokemon_count():
inventory_items = api.get_inventory().call()['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']
count = 0
for item in inventory_items:
if item.has_key("inventory_item_data") and item["inventory_item_data"].has_key("pokemon_data"):
count += 1
return jsonify({"count": count})
@app.route("/ball_count", methods=["GET"])
def get_ball_count():
pass
@app.route("/login_account", methods=["POST"])
def login_account():
# data=auth_service=ptc&username=lalal&password=sicher&cp=100&cached=True
try:
auth_service = request.form.get("auth_service", type=str)
username = request.form.get("username", type=str)
password = request.form.get("password", type=str)
cp = request.form.get("cp", type=int)
cached = request.form.get("cached", type=bool)
print auth_service, username, password, cp, cached, type(cached)
if api.login(auth_service, username, password, cp, cached):
state = "ok"
else:
state = "fail"
except:
state = "error"
return jsonify({"status": state})
#@app.route("/cleanup_inventory", methods=["GET"])
#def cleanup_inventory():
# try:
# api.cleanup_inventory()
# return jsonify({"status": "ok"})
# except:
# return jsonify({"status": "error"})
if __name__ == "__main__":
app.run()