diff --git a/api_call.py b/api_call.py new file mode 100644 index 0000000..997c17f --- /dev/null +++ b/api_call.py @@ -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 + + + + + diff --git a/cleanup.py b/cleanup.py new file mode 100644 index 0000000..d83d8b2 --- /dev/null +++ b/cleanup.py @@ -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 diff --git a/main.py b/main.py new file mode 100644 index 0000000..022e08e --- /dev/null +++ b/main.py @@ -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) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9a5463e Binary files /dev/null and b/requirements.txt differ diff --git a/save.py b/save.py new file mode 100644 index 0000000..0728e4a --- /dev/null +++ b/save.py @@ -0,0 +1,7 @@ +import json + + +def save_file(obj): + + with open("users.json", "w") as f: + json.dump(obj, f) \ No newline at end of file diff --git a/users.json b/users.json new file mode 100644 index 0000000..b560295 --- /dev/null +++ b/users.json @@ -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" + } +} \ No newline at end of file