-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
173 lines (136 loc) · 5.27 KB
/
bot.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import requests
username = 'test_user'
email = '[email protected]'
password = 'tuser'
refresh_token = ''
access_token = ''
base_url = 'http://localhost:8000/api/'
def _print_result(response):
print(f'Result of the request:\n'
f'Status: {response.status_code}\n'
f'Body:')
for k, v in response.json().items():
print(f'"{k}": "{v}"')
print('-' * 10)
def register_user():
payload = dict(username=username, email=email, password=password, password_confirm=password)
res = requests.post(base_url + 'jwtauth/register/', data=payload)
if res.status_code == 201:
print(f'User `{username}` registered')
return
def login_user():
payload = dict(username=username, password=password)
res = requests.post(base_url + 'jwtauth/login/', data=payload)
print(f'Login as `{username}`')
if res.status_code == 200:
return res.json()
def refresh_tokens():
payload = dict(refresh=refresh_token)
res = requests.post(f'{base_url}jwtauth/refresh/', data=payload)
_print_result(res)
if res.status_code == 200:
return res.json()['access']
def create_post(title, content):
payload = dict(title=title, content=content)
headers = dict(Authorization=f'Bearer {access_token}')
res = requests.post(base_url + 'posts/', data=payload, headers=headers)
_print_result(res)
if res.status_code == 201:
return res.json()
def list_posts():
headers = dict(Authorization=f'Bearer {access_token}')
res = requests.get(base_url + 'posts/', headers=headers)
print('-' * 10)
if res.status_code == 200:
print(f'Result of the request:\n'
f'Status: {res.status_code}\n'
f'Body:')
for post in res.json():
for k, v in post.items():
print(f'"{k}": "{v}"')
print('-' * 10)
def like_unlike_post(post_id):
headers = dict(Authorization=f'Bearer {access_token}')
res = requests.get(base_url + f'posts/{post_id}/like/', headers=headers)
_print_result(res)
def analytics_all_likes():
headers = dict(Authorization=f'Bearer {access_token}')
res = requests.get(f'{base_url}analytics/likes/', headers=headers)
print(f'Result of the request:\n'
f'Status: {res.status_code}\n'
f'Body:')
for post in res.json():
for k, v in post.items():
print(f'"{k}": "{v}"')
print('-' * 10)
def analytics_user_activity(user_id):
headers = dict(Authorization=f'Bearer {access_token}')
res = requests.get(base_url + f'analytics/user/{user_id}/', headers=headers)
_print_result(res)
if __name__ == '__main__':
# SET UP
print('Hello. This is my short showcase of the test project.\n\n'
'This script will show you all features step by step.\n\n'
'On each step script will tell what it going to do,\n'
'give you `>Endpoint` with http method of next request\n'
'and short description about what should be in response.\n\n'
'Note: make sure to apply all migrations and run server first.\n')
input('Press Enter to continue')
print()
# register -> login
print(f'>Endpoint POST: /api/jwtauth/register/')
print('When reached: register new `User`')
register_user()
print()
print('>Endpoint POST: /api/jwtauth/login/')
print('When reached: return `refresh` & `access` jwt tokens')
refresh_token, access_token = login_user().values()
print()
# create a few posts -> like them
print('/\\' * 10)
print('Now script will create 3 new posts:\n')
print('>Endpoint POST: /api/posts/')
print('When reached: create new `Post`')
input('Press Enter')
print()
create_post('First post', 'Content of first post')
create_post('Another one', 'Object of this task is to create a simple REST API.')
create_post('Requirements', 'Implement token authentication (JWT is preferred). Yes I did it)')
print('/\\' * 10)
print('Like this posts:\n')
print('>Endpoint GET: /api/posts/<int:post_id>/like/')
print('When reached: like or unlike post with passed `post_id`')
input('Press Enter')
print()
[like_unlike_post(n) for n in range(1, 4)]
print()
# show analytics
print('/\\' * 10)
print('Display analytics about amount of likes:\n')
print('>Endpoint GET: /api/analytics/likes/')
print('When reached: show how many likes was made aggregated by day')
input('Press Enter')
print()
analytics_all_likes()
print()
print('/\\' * 10)
print('Lets unlike some posts:\n')
print('>Endpoint GET: /api/posts/<int:post_id>/like/')
print('Same endpoint and http method as for like post above')
input('Press Enter')
print()
[like_unlike_post(n) for n in range(1, 3)]
print()
print('/\\' * 10)
print('Make sure we unlike posts by checking amount of likes:\n')
input('Press Enter')
analytics_all_likes()
print()
print('/\\' * 10)
print('Show analytics about user activity:\n')
print('>Endpoint GET: /api/analytics/user/<int:user_id>/')
print('When reached: show time of last login and request of the user with passed `user_id`')
input('Press Enter')
print()
analytics_user_activity(1)
print('\nAll features from the task was shown.\nThank you for your attention :D\n')