-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (85 loc) · 3.21 KB
/
Copy pathmain.py
File metadata and controls
110 lines (85 loc) · 3.21 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
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
import json
import logging
import requests
api_url: str = "https://dummy-json.mock.beeceptor.com"
def get_posts(url: str) -> dict | None:
"""
Fetch all posts from API
Args:
url (str): API URL
Returns:
dict: Dictionary of Posts
Raises:
requests.exceptions.ConnectionError: Connection error
"""
logging.info("Start fetching posts from API.")
response = requests.get(
url=f"{url}/posts"
)
logging.debug("API return code {}.".format(response.status_code))
if response.status_code == 200:
return response.json()
else:
logging.error("Failed to fetch posts from API.\n{}".format(response.text))
# Explict return None statement for best practice when expecting a return from this function
return None
def get_users(url: str) -> dict | None:
"""
Fetch all users information from API
Args:
url (str): API URL
Returns:
dict: Dictionary of Users
Raises:
requests.exceptions.ConnectionError: Connection error
"""
logging.info("Start fetching user information from API.")
response = requests.get(
url=f"{url}/users"
)
logging.debug("API return code {}.".format(response.status_code))
if response.status_code == 200:
return response.json()
else:
logging.error("Failed to fetch user information from API.\n{}".format(response.text))
return None
def get_user(user_id: int, api_response: dict) -> dict | None:
for user in api_response:
if user['id'] == user_id:
return user
return None
if __name__ == "__main__":
# We use logger to record things that happen during our program execution
# More info on Python logging: https://docs.python.org/3/library/logging.html
# Configure the root logger
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# Create a logger for the current module
logger = logging.getLogger(__name__)
logging.info("Finish logging setup.")
# It is not require to do type annotation `dict | None`, but it is a good practice.
# get_posts() may return None if the API call was unsuccessful
posts: dict | None = get_posts(api_url)
# If posts is not null can be written like this instead of `if x is not None:`
# HOWEVER, if posts is 0, an empty string "" it will return false.
if posts:
for post in posts:
print(f"User {post['userId']} posted: {post['title']}.")
users = get_users(url= api_url)
if users:
# Get User 1 data
user_data = get_user(1, users)
# Using json.dumps() for prettier print
print(json.dumps(user_data, indent=4))
# Get user 5 data
# The order of argument matters
# However it can override if arguments are specific like below
user_data = get_user(api_response=users, user_id=5)
print(user_data['email'])
# For best practices in Python, PEP8 is the style guide in Python
# https://peps.python.org/pep-0008/
# If interested in web scraping, BeautifulSoup is the standard library for it in Python
# https://beautiful-soup-4.readthedocs.io/en/latest/