Friday, December 2, 2022

Python - API Requests and Headers (Day 37)

This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp 

I will post some notes to motivate myself to finish this challenge.


Goal



Using Pixela to practice all HTTP Methods and headers for authentication.


Ex: Create an account (POST request)
import requests
import os
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
TOKEN = os.environ.get("TOKEN")

# Prepare Request Payload for API
payload = {
    "username": USERNAME,
    "token": TOKEN,
    "agreeTermsOfService": "yes",
    "notMinor": "yes"
}

# Sends a POST request.
response = requests.post(f"{PIXELA_ENDPOINT}users", json=payload)
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)


Ex: Create a graph (POST request)
import requests
import os
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
TOKEN = os.environ.get("TOKEN")
GRAPH_ID = os.environ.get("GRAPH_ID")
GRAPH_NAME = os.environ.get("GRAPH_NAME")

# Prepare headers for API
headers = {
    "X-USER-TOKEN": TOKEN
}

# Prepare Request Payload for API
payload = {
    "id": GRAPH_ID,
    "name": GRAPH_NAME,
    "unit": "kilogram",
    "type": "float",
    "color": "ajisai",
}

# Sends a POST request.
response = requests.post(f"{PIXELA_ENDPOINT}users/{USERNAME}/graphs",
json=payload, headers=headers)
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)


Ex: Get the graph (GET request)
import requests
import os
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
GRAPH_ID = os.environ.get("GRAPH_ID")

# Sends a GET request.
response = requests.get(f"{PIXELA_ENDPOINT}users/{USERNAME}/graphs/{GRAPH_ID}")
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)

# https://pixe.la/v1/users/frank-exp/graphs/graph-exp.html


Ex: Add a pixel to the graph (POST request)
import requests
import os
import datetime
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
TOKEN = os.environ.get("TOKEN")
GRAPH_ID = os.environ.get("GRAPH_ID")

# Prepare headers for API
headers = {
    "X-USER-TOKEN": TOKEN
}

# Prepare Request Payload for API
payload = {
    "date":  datetime.datetime.now().strftime("%Y%m%d"),
    "quantity": "71.5"
}

# Sends a POST request.
response = requests.post(
    f"{PIXELA_ENDPOINT}users/{USERNAME}/graphs/{GRAPH_ID}",
json=payload, headers=headers)
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)


Ex: Update the existing pixel of the graph (PUT request)
import requests
import os
import datetime
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
TOKEN = os.environ.get("TOKEN")
GRAPH_ID = os.environ.get("GRAPH_ID")

# Prepare headers for API
headers = {
    "X-USER-TOKEN": TOKEN
}

# Prepare Request Payload for API
payload = {
    "quantity": "100.0"
}

now_in_pixela_format = datetime.datetime.now().strftime("%Y%m%d")

# Sends a PUT request.
response = requests.put(
    f"{PIXELA_ENDPOINT}users/{USERNAME}/graphs/{GRAPH_ID}/{now_in_pixela_format}",
json=payload, headers=headers)
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)


Ex: Delete the existing pixel of the graph (DELETE request)
import requests
import os
import datetime
from dotenv import load_dotenv

# Load Environmental Variables from .env
load_dotenv()

PIXELA_ENDPOINT = "https://pixe.la/v1/"
USERNAME = os.environ.get("USERNAME")
TOKEN = os.environ.get("TOKEN")
GRAPH_ID = os.environ.get("GRAPH_ID")

# Prepare headers for API
headers = {
    "X-USER-TOKEN": TOKEN
}

now_in_pixela_format = datetime.datetime.now().strftime("%Y%m%d")

# Sends a DELETE request.
response = requests.delete(
    f"{PIXELA_ENDPOINT}users/{USERNAME}/graphs/{GRAPH_ID}/20221202",
headers=headers)
# Raises HTTPError, if one occurred.
response.raise_for_status()
# Print the Content of Response
print(response.text)


No comments:

Post a Comment