-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_api.py
More file actions
43 lines (30 loc) · 1.13 KB
/
python_api.py
File metadata and controls
43 lines (30 loc) · 1.13 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
import requests
import json
class MakeApiCall:
def get_data(self, api):
response = requests.get(f"{api}")
if response.status_code == 200:
print("sucessfully fetched the data")
self.formatted_print(response.json())
else:
print(
f"Hello person, there's a {response.status_code} error with your request")
def get_user_data(self, api, parameters):
response = requests.get(f"{api}", params=parameters)
if response.status_code == 200:
print("sucessfully fetched the data with parameters provided")
self.formatted_print(response.json())
else:
print(
f"Hello person, there's a {response.status_code} error with your request")
def formatted_print(self, obj):
text = json.dumps(obj, sort_keys=True, indent=4)
print(text)
def __init__(self, api):
# self.get_data(api)
parameters = {
"username": "kedark"
}
self.get_user_data(api, parameters)
if __name__ == "__main__":
api_call = MakeApiCall("https://dev.to/api/articles")