Skip to content

Commit 373a234

Browse files
author
“minadev”
committed
day40: Http POST, PUT and DELETE apis; steps tracking application
1 parent 0d8f700 commit 373a234

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

  • day40-post-endpoints-tracking-steps
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import requests
2+
import datetime
3+
4+
# Track how many steps we've walked.
5+
6+
pixela_endpoint = "https://pixe.la/v1/users"
7+
8+
# STEP 1: Create your user account
9+
USERNAME = "YOUR_USERNAME"
10+
TOKEN = "YOUR_TOKEN"
11+
create_user_request_body = {
12+
"token": TOKEN,
13+
"username": USERNAME,
14+
"agreeTermsOfService": "yes",
15+
"notMinor": "yes"
16+
}
17+
18+
response = requests.request(method="POST", url=pixela_endpoint, json=create_user_request_body)
19+
print(response.text)
20+
21+
# STEP 2: Create your user account - POST - /v1/users/<username>/graphs
22+
create_graph_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs"
23+
headers = {
24+
"X-USER-TOKEN": TOKEN
25+
}
26+
GRAPH_ID = "graph1"
27+
create_graph_request_body = {
28+
"id": GRAPH_ID,
29+
"name": "Track Steps Graph",
30+
"unit": "steps",
31+
"type": "int",
32+
"color": "sora"
33+
}
34+
graph_response = requests.post(create_graph_endpoint, json=create_graph_request_body, headers=headers)
35+
print(graph_response.text)
36+
37+
# STEP 3: Get the graph - /v1/users/<username>/graphs/<graphID>
38+
response = requests.request(method="GET", url=f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}")
39+
response.raise_for_status()
40+
41+
# STEP 4: Post value to the graph - /v1/users/<username>/graphs/<graphID>
42+
create_a_pixel_value_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}"
43+
current_date = datetime.datetime.now().strftime("%Y%m%d")
44+
create_pixel_request_body = {
45+
"date": current_date,
46+
"quantity": input("How many steps did you walk today?")
47+
}
48+
create_pixel_response = requests.request(method="POST", url=create_a_pixel_value_endpoint,
49+
json=create_pixel_request_body,
50+
headers=headers)
51+
print(create_pixel_response.text)
52+
53+
# STEP 5: update a pixel
54+
# update_a_pixel_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{current_date}"
55+
# current_date = datetime.datetime.now().strftime("%Y%m%d")
56+
# update_pixel_request_body = {
57+
# "date": current_date,
58+
# "quantity": 9000
59+
# }
60+
# update_a_pixel_endpoint = requests.request(method="PUT", url=update_a_pixel_endpoint, json=update_pixel_request_body,
61+
# headers=headers)
62+
# print(update_a_pixel_endpoint.text)
63+
64+
# STEP 6: Delete a pixel
65+
# update_a_pixel_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{current_date}"
66+
#
67+
# requests.request(method="DELETE", url=update_a_pixel_endpoint, headers=headers)

0 commit comments

Comments
 (0)