Day 037

AWESOMee·2022년 3월 9일
0

Udemy Python Bootcamp

목록 보기
37/64
post-thumbnail

Udemy Python Bootcamp Day 037

HTTP Requests

  • GET requests.get()
    Whereas a get request is made where we ask an external system for a particular piece of data and they give that to us in the response.

  • POST requests.post()
    A post request is where we give the external system some piece of data and we're nor so interested in the reponse we're getting back other than whether if it was successful or not.

  • PUT requests.put()
    Put is where we simply update a piece of data in the external service.

  • DELETE requests.delete()
    Delete is simply where we want to delete a piece of data in the external service.

HTTP Headers

In a mail, the header is the part that contains some relevant pieces of information, like the phone number of the company or their website of the company or their logo.

And then the body is the actual message part sesentially. It's the part that changes from letter to letter.

headers = {
    "X-USER-TOKEN": TOKEN
}

This is how we can authenticate ourselves using a header.
And the requests module makes passing over headers just as easy as passing over other parameters or other of the message body.

datetime.strftime()

The datetime object has a method for formatting date objects into readable strings.

The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:

date = datetime.today()
print(date.strftime("%Y%m%d"))

#output
20220309

Habit Tracking Project

requests.post()

import requests

pixela_endpoint = "https://pixe.la/v1/users"

user_params = {
    "token": "***********",
    "username": "*****",
    "agreeTermsOfService": "yes",
    "notMinor": "yes"
}

response = requests.post(url=pixela_endpoint, json=user_params)
print(response.text)

#output
{"message":"Success. Let's visit https://pixe.la/@***** , it is your profile page!","isSuccess":true}

오.. 더이상 신기할게 없다고 생각했는데
token, username을 파이썬 내에서 만들어서 페이지 생성하는건 좀 신기하다
어디선가 많이 봤던 잔디심기 만들기 go!

Advanced Authentication

graph_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs"

graph_config = {
    "id": "graph1",
    "name": "Cycling Graph",
    "unit": "Km",
    "type": "float",
    "color": "ajisai"
}

headers = {
    "X-USER-TOKEN": TOKEN
}

response = requests.post(url=graph_endpoint, json=graph_config, headers=headers)
print(response.text)

#output
https://pixe.la/v1/users/******/graphs/graph1.html


Add a Pixel to the Habit Tracker

pixel_creation_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}"

pixel_data = {
    "date": "20220309",
    "quantity": "10.5"
}

response = requests.post(url=pixel_creation_endpoint, json=pixel_data, headers=headers)
print(response.text)

#output

이렇게 하면 조금 덜 익었지만 작고 소중한 나의 블루베리가 생기는겨!!
물론 cycling은 안젤라 취향이고 나는 딱히 할게 없어서..

기존 잔디심기는 사실 어떻게 굴러가는 건지 모르지만..
아마도 자동으로 잔디가 심어지는 시스템일듯 싶은데
이건 수동으로 심는거긴 하지만... 그래도 신기한거야 그냥~~

Autofilling today's date

today = datetime.now()

pixel_data = {
    "date": today.strftime("%Y%m%d"),
    "quantity": "10.5"
}

Update a pixel

update_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

new_pixel_data = {
    "quantity": "4"
}

response = requests.put(url=update_endpoint, json=new_pixel_data, headers=headers)
print(response.text)

Delete a pixel

delete_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

response = requests.delete(url=delete_endpoint, headers=headers)
print(response.text)

FINAL

import requests
from datetime import datetime

USERNAME = "*****"
TOKEN = "*******************"
GRAPH_ID = "graph1"

pixela_endpoint = "https://pixe.la/v1/users"

user_params = {
    "token": TOKEN,
    "username": USERNAME,
    "agreeTermsOfService": "yes",
    "notMinor": "yes"
}

# response = requests.post(url=pixela_endpoint, json=user_params)
# print(response.text)

graph_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs"

graph_config = {
    "id": GRAPH_ID,
    "name": "Cycling Graph",
    "unit": "Km",
    "type": "float",
    "color": "ajisai"
}

headers = {
    "X-USER-TOKEN": TOKEN
}

# response = requests.post(url=graph_endpoint, json=graph_config, headers=headers)
# print(response.text)

pixel_creation_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}"

today = datetime.now()

pixel_data = {
    "date": today.strftime('%Y%m%d'),
    "quantity": input("How many kilometers did you cycle today? ")
}

response = requests.post(url=pixel_creation_endpoint, json=pixel_data, headers=headers)
print(response.text)

update_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

new_pixel_data = {
    "quantity": "4"
}

# response = requests.put(url=update_endpoint, json=new_pixel_data, headers=headers)
# print(response.text)

delete_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

# response = requests.delete(url=delete_endpoint, headers=headers)
# print(response.text)
profile
개발을 배우는 듯 하면서도

0개의 댓글