Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
import requests


def make_api_call():

"""make api call and get a list of random users. """

try:
result_response = requests.get('http://jsonplaceholder.typicode.com/users')
# print("result type is ", type(result))
if result_response.status_code >= 400:
raise requests.exceptions.HTTPError("Un error is raised, status code above")

except requests.exceptions.HTTPError as raisedException:
print(raisedException)
sys.exit()

else:
result_obj_lst = result_response.json()
return result_obj_lst





15 changes: 15 additions & 0 deletions cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def clean_result(result_lst):
"""clean up the result obj - takes a list returns a dic
return: dict - cleaned up version of the res obj. """
global i
i=1
result_dict = {}

for item in result_lst:
result_dict[f'PERSON{i}'] = {}
result_dict[f'PERSON{i}']['id'] =item["id"]
result_dict[f'PERSON{i}']['name'] = item["name"]
result_dict[f'PERSON{i}']['username'] = item["username"]
result_dict[f'PERSON{i}']['email'] = item["email"]
i=i+1
return result_dict
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

from operator import imod
import api_call
import cleanup
import save

result_lst = api_call.make_api_call()
clean_dct = cleanup.clean_result(result_lst)
save.save_file(clean_dct)

Binary file added requirements.txt
Binary file not shown.
7 changes: 7 additions & 0 deletions save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json


def save_file(obj):

with open("users.json", "w") as f:
json.dump(obj, f)
62 changes: 62 additions & 0 deletions users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"PERSON1": {
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz"
},
"PERSON2": {
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv"
},
"PERSON3": {
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"email": "Nathan@yesenia.net"
},
"PERSON4": {
"id": 4,
"name": "Patricia Lebsack",
"username": "Karianne",
"email": "Julianne.OConner@kory.org"
},
"PERSON5": {
"id": 5,
"name": "Chelsey Dietrich",
"username": "Kamren",
"email": "Lucio_Hettinger@annie.ca"
},
"PERSON6": {
"id": 6,
"name": "Mrs. Dennis Schulist",
"username": "Leopoldo_Corkery",
"email": "Karley_Dach@jasper.info"
},
"PERSON7": {
"id": 7,
"name": "Kurtis Weissnat",
"username": "Elwyn.Skiles",
"email": "Telly.Hoeger@billy.biz"
},
"PERSON8": {
"id": 8,
"name": "Nicholas Runolfsdottir V",
"username": "Maxime_Nienow",
"email": "Sherwood@rosamond.me"
},
"PERSON9": {
"id": 9,
"name": "Glenna Reichert",
"username": "Delphine",
"email": "Chaim_McDermott@dana.io"
},
"PERSON10": {
"id": 10,
"name": "Clementina DuBuque",
"username": "Moriah.Stanton",
"email": "Rey.Padberg@karina.biz"
}
}