-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
63 lines (51 loc) · 1.71 KB
/
core.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
from random import randint, choice
from typing import List
import requests
import logging
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def run_query(query=None, variables=None, headers=None):
if not query:
raise Exception("Query must be provided")
request = requests.post(
'http://127.0.0.1:8000/felicity-gql',
json={'query': query, 'variables': variables} if query else {
'query': self.query, 'variables': variables},
headers=headers,
)
if request.status_code == 200:
logger.info(request.json())
return request.json()
else:
raise Exception(
f"({request.status_code}): Query Failed: {request.text}")
def authenticate(credentials: dict):
qry = """
mutation AuthenticateUser($username: String!, $password: String!) {
authenticateUser(password: $password, username: $username) {
... on AuthenticatedData {
token
tokenType
}
}
}
"""
variables = credentials
auth = run_query(query=qry, variables=variables)
token = auth["data"]["authenticateUser"]['token']
return {"Authorization": f"bearer {token}"}
def do_work(query: str = None, var_list: list = None, usernames: List[str] = None):
# time.sleep(randint(1,5))
username = choice(usernames)
credentials = {
'username': username,
'password': "!Felicity#100",
}
auth_headers = authenticate(credentials)
fin = []
for variables in var_list:
# time.sleep(randint(1, 10))
val = run_query(query=query, variables=variables, headers=auth_headers)
fin.append(val)
return fin