From d8dc1f2166e7f682acc45e8d2433dee309df2119 Mon Sep 17 00:00:00 2001 From: rama nahawandi Date: Fri, 14 Oct 2022 21:31:04 +0200 Subject: [PATCH] solved the entier excersie --- api_call.py | 26 ++++++++++++++++++++ cleanup.py | 15 ++++++++++++ main.py | 10 ++++++++ requirements.txt | Bin 0 -> 752 bytes save.py | 7 ++++++ users.json | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 api_call.py create mode 100644 cleanup.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 save.py create mode 100644 users.json 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 0000000000000000000000000000000000000000..9a5463ee7d6b3f62f8624f2ef4bce7849e459a50 GIT binary patch literal 752 zcmZ`%(Qbl35S-^G{S=IVTKmv{=r2%1!@F* zphtlY2fXlGVu2J-{LS!)2kcO?9#CP=BSc0_#&3aVL#-n2K$RY6;sX*iX!-BNyPCY4 zsW@BuI#PdU?M%EdZ*%{g^^MilvnIEu+73rUsJi7@M#Mn=z+UCdPp$6$EB0vULcPUw zw+$I5&V03d`N_