diff --git a/README.md b/README.md index 2129f26..a718a13 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # Class7-Python-Module-Week3 ## Random User Creator App + #### We are going to create an app which will make an API call to `http://jsonplaceholder.typicode.com/users` and get a list of random users. Then our app will clean up the returned list of users and store them in a `.json` file. ## Steps to follow: + #### Create an API function: + - Import necessary library for making an API call. - Make the API call to this URL -> `http://jsonplaceholder.typicode.com/users` - Check the status code, return object (res.json()) @@ -12,21 +15,26 @@ - Return the return object. #### Create a cleanup function: + - Take the return object as parameter and transform the return object. (Before starting with transforming this return object, check its data type as it may not be a dictionary. Plan your code according to its data type) - Create a new Python dictionary containing all 10 users and their details from the return object. (Items to be included: id, name, username, email) - Return the Python dictionary -#### Creata a function to write the users dictionary to a .json file: -- Take a dictionary as the parameter -- Open up a new file (or create if it isn't existing) -- Dump the Python dictionary into this file + +#### Create a function to write the users dictionary to a .json file: + +- Take a dictionary as the parameter +- Open up a new file (or create if it isn't existing) +- Dump the Python dictionary into this file + #### Create a `main.py` file: + - Import all your modules - Create the main app logic here - ## Things to remember: -- Use a virtual environment. + +- Use a virtual environment. - Create separate functions for all the steps. - Modularize your code (either create a separate file for each function or create a file called `utils.py` and store all the functions in this file) - Wrap the necessary parts of your functions in try/except blocks. -- Create a `requirements.txt` file at the end of your project to store all the dependancy information. (`pip freeze > requirements.txt`) +- Create a `requirements.txt` file at the end of your project to store all the dependency information. (`pip freeze > requirements.txt`) diff --git a/main.py b/main.py new file mode 100644 index 0000000..bd17d4a --- /dev/null +++ b/main.py @@ -0,0 +1,5 @@ +from utils import make_api_call, clean_up, save_file + +result_list = make_api_call() +clean_dict = clean_up(result_list) +save_file(clean_dict) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..94b56f0 Binary files /dev/null and b/requirements.txt differ diff --git a/result_20221011-164918.json b/result_20221011-164918.json new file mode 100644 index 0000000..46d4951 --- /dev/null +++ b/result_20221011-164918.json @@ -0,0 +1 @@ +{"1": {"name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz"}, "2": {"name": "Ervin Howell", "username": "Antonette", "email": "Shanna@melissa.tv"}, "3": {"name": "Clementine Bauch", "username": "Samantha", "email": "Nathan@yesenia.net"}, "4": {"name": "Patricia Lebsack", "username": "Karianne", "email": "Julianne.OConner@kory.org"}, "5": {"name": "Chelsey Dietrich", "username": "Kamren", "email": "Lucio_Hettinger@annie.ca"}, "6": {"name": "Mrs. Dennis Schulist", "username": "Leopoldo_Corkery", "email": "Karley_Dach@jasper.info"}, "7": {"name": "Kurtis Weissnat", "username": "Elwyn.Skiles", "email": "Telly.Hoeger@billy.biz"}, "8": {"name": "Nicholas Runolfsdottir V", "username": "Maxime_Nienow", "email": "Sherwood@rosamond.me"}, "9": {"name": "Glenna Reichert", "username": "Delphine", "email": "Chaim_McDermott@dana.io"}, "10": {"name": "Clementina DuBuque", "username": "Moriah.Stanton", "email": "Rey.Padberg@karina.biz"}} \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..2f8d203 --- /dev/null +++ b/utils.py @@ -0,0 +1,40 @@ +import json +import sys +import requests +from datetime import datetime + +def make_api_call(): + """make api call and get the information of the users + return : list - user info + """ + try: + res = requests.get('http://jsonplaceholder.typicode.com/users') + if res.status_code >= 400: + raise requests.exceptions.HTTPError("There is an API error") + + except requests.exceptions.HTTPError as exc: + print(exc) + sys.exit() + else: + result_obj = res.json() + return result_obj + + +def clean_up(lst): + """clean up the result obj + return: list - cleaned up version of the res obj + """ + new_dict = dict() + for item in lst: + new_dict[item["id"]]= {'name': item["name"], #"id" is the key of the new_dict + 'username' : item["username"], + 'email' : item["email"]} + + return new_dict + +def save_file(obj): + """creates a file and saves the users data + """ + filename = "result_" + datetime.now().strftime('%Y%m%d-%H%M%S')+'.json' + with open(filename, "w") as f: + json.dump(obj, f) \ No newline at end of file