-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApi_handling.py
More file actions
25 lines (20 loc) · 843 Bytes
/
Api_handling.py
File metadata and controls
25 lines (20 loc) · 843 Bytes
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
import requests
def fetch_random_user_freeapi():
url = "https://api.freeapi.app/api/v1/public/randomusers/user/random"
respone = requests.get(url)
data = respone.json()
if data ["success"] and "data" in data:
user_data = data["data"]
user_name = user_data["login"] ["username"]
country = user_data["location"] ["country"]
return user_name,country
else:
raise Exception("Failed to fetch user data")
def main():
try:
user_name,country = fetch_random_user_freeapi()
print(f"Username: {user_name}\ncountry:{country}")
except Exception as e:
print(str(e))
if __name__ == "__main__":
main()