forked from networkdynamics/BattleBots-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_requests.py
More file actions
81 lines (68 loc) · 3.1 KB
/
api_requests.py
File metadata and controls
81 lines (68 loc) · 3.1 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
import os
import requests
import json
# Competition Environment Variables
base_url = os.getenv('BASE_URL')
authentication_token = os.getenv('AUTH_TOKEN')
session_id = os.getenv('SESSION_ID')
# Testing Environment Variables
# base_url = 'http://52.207.242.165:3000/api/test/10'
# authentication_token = ""
# session_id = 5
header = {'Authorization': 'bearer ' + authentication_token, 'Content-Type': 'application/json'}
#BOT SECTION
class SessionInfo:
def __init__(self, data):
self.session_id = data["session_id"]
self.lang = data["lang"]
self.metadata = data["metadata"]
self.influence_target = data["influence_target"]
self.start_time = data["start_time"]
self.end_time = data["end_time"]
self.sub_sessions_info = data["sub_sessions_info"]
self.sub_sessions_id = [sub_session["sub_session_id"] for sub_session in data["sub_sessions_info"]]
self.users = data["users"]
self.usernames = set([user["username"] for user in data["users"]])
class SubSessionDataset:
def __init__(self, data):
self.session_id = data["session_id"]
self.sub_session_id = data["sub_session_id"]
self.posts = data["posts"]
self.users = data["users"]
def get_session_info():
response = requests.get(base_url + '/bot/session/' + str(session_id) + '/info', headers=header)
if response.status_code >= 400:
return response, []
else:
return response, SessionInfo(response.json())
def create_user_id(number_users):
response = requests.post(base_url + '/bot/session/' + str(session_id) + '/createuser', headers=header, data=json.dumps({"num_of_users": number_users}))
users_id_list = []
for user in response.json()['users']:
users_id_list.append(user['id'])
return response, users_id_list
def get_sub_session(sub_session):
response = requests.get(base_url + '/bot/session/' + str(session_id) + '/' + str(sub_session), headers=header)
if response.status_code >= 400:
return response, []
else:
return response, SubSessionDataset(response.json())
def submit_injection(sub_session, posts_submission, users_submission):
print(json.dumps({"posts": posts_submission, "users": users_submission}, indent=4))
return requests.post(base_url + '/bot/session/' + str(session_id) + '/' + str(sub_session), headers=header, data=json.dumps({"posts": posts_submission, "users": users_submission}))
# DETECTOR SECTION
class SessionDataset:
def __init__(self, data):
self.session_id = data["id"]
self.lang = data["lang"]
self.metadata = data["metadata"]
self.posts = data["posts"]
self.users = data["users"]
def get_session_data():
response = requests.get(base_url + '/detector/session/' + str(session_id), headers=header)
if response.status_code >= 400:
return response, []
else:
return response, SessionDataset(response.json())
def submit_detection(detections_submission):
return requests.post(base_url + '/detector/session/' + str(session_id), headers=header, data=json.dumps({"users": detections_submission}))