forked from CodeYourFuture/Module-Legacy-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.py
More file actions
83 lines (61 loc) · 3.07 KB
/
Copy pathpopulate.py
File metadata and controls
83 lines (61 loc) · 3.07 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
import json
import sys
import requests
from typing import Any, Dict, Optional
def fail(message: str) -> None:
print(message, file=sys.stderr)
sys.exit(1)
def post(endpoint: str, data: Dict[str, Any], access_token: Optional[str] = None):
headers = {
"Content-Type": "application/json",
}
if access_token is not None:
headers["Authorization"] = f"Bearer {access_token}"
response = requests.post(
f"http://127.0.0.1:3000{endpoint}", data=json.dumps(data), headers=headers
)
if response.status_code != 200:
fail(
f"Got status code {response.status_code} from request to {endpoint}. Response body: {response.text}"
)
return response.json()
def create_user(username: str, password: str) -> str:
"""create_user creates a user and returns an access token which can be used for future requests."""
response = post("/register", data={"username": username, "password": password})
if not response["success"]:
fail(f"Could not create user: {response}")
return response["token"]
def send_bloom(access_token: str, text: str) -> None:
post("/bloom", data={"content": text}, access_token=access_token)
def follow(*, follower_access_token: str, follow_username: str) -> None:
post(
"/follow",
data={"follow_username": follow_username},
access_token=follower_access_token,
)
def main():
sample_access_token = create_user("sample", "sosecret")
send_bloom(sample_access_token, "Hi there")
tech_influencer_access_token = create_user("TechInfluencer", "Crypto4Lyfe")
send_bloom(tech_influencer_access_token, "Tech is magic #blessed #TechLife")
musician_access_token = create_user("Swiz", "singingalldayeveryday")
send_bloom(
musician_access_token, "New album dropping at midnight! Pre-save now! #SwizBiz"
)
send_bloom(musician_access_token, "Let's get some #SwizBiz love!!")
send_bloom(sample_access_token, "I'm not quite sure how this works")
writer_access_token = create_user("AS", "neverSt0pTalking")
send_bloom(
writer_access_token,
"In this essay I will convince you that my views are correct in ways you have never imagined. If it doesn't change your life, read it again. Marshmallows are magnificent. They have great squish, tasty good, and you can even toast them over a fire. Toast them just right until they have a tiny bit of crunch when you bite into them, and have just started melting in the middle.",
)
justsomeguy_access_token = create_user("JustSomeGuy", "mysterious")
send_bloom(justsomeguy_access_token, "Hello.")
for other_user in ["OtherUser", "Lurker", "SomeQuietMongoose"]:
create_user(other_user, "PleaseDon'tStealMe")
follow(follower_access_token=sample_access_token, follow_username="TechInfluencer")
follow(follower_access_token=sample_access_token, follow_username="Swiz")
follow(follower_access_token=sample_access_token, follow_username="AS")
follow(follower_access_token=musician_access_token, follow_username="sample")
if __name__ == "__main__":
main()