In order to access external REST APIs, for this assignment, you must use the requests package to communicate in the HTTP protocol. Finish the implementation of the class called AccessApi. There are 7 method stubs that will need to be completed: A constructor that requires the developer to input a base URL as a string that will host the REST API endpoint. Example: “http://google.com” A method to get the current URL base. A method to set the current URL base. A method to test if the URL is responding to GET requests to allow for a simple alive test. A method to input an endpoint, as a string, and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the JSON sent as a list. A method to input an endpoint and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the status code. A method to input an endpoint and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the total elapsed time used for the GET request.
In order to access external REST APIs, for this assignment, you must use the requests package to communicate in the HTTP protocol.
Finish the implementation of the class called AccessApi. There are 7 method stubs that will need to be completed:
- A constructor that requires the developer to input a base URL as a string that will host the REST API endpoint. Example: “http://google.com”
- A method to get the current URL base.
- A method to set the current URL base.
- A method to test if the URL is responding to GET requests to allow for a simple alive test.
- A method to input an endpoint, as a string, and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the JSON sent as a list.
- A method to input an endpoint and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the status code.
- A method to input an endpoint and have that endpoint concatenated to the base URL and then send a GET request using the requests package to the combined URL. Then, return the total elapsed time used for the GET request.
![](/static/compass_v2/shared-icons/check-mark.png)
Algorithm for AccessApi Class:
1. Class AccessApi:
- Initialize with a base_url as a constructor parameter.
2. Method: get_base_url():
- Returns the current base URL of the instance.
3. Method: set_base_url(new_base_url):
- Set the base URL to the specified new_base_url.
4. Method: is_url_alive():
- Try to send a GET request to the current base URL.
- If the response status code is 200, return True (URL is alive).
- Otherwise, return False (URL is not responding).
5. Method: get_data(endpoint):
- Concatenate the endpoint to the current base URL.
- Send a GET request to the combined URL.
- If the response status code is 200:
- Return the JSON data from the response as a list.
- Otherwise, return an empty list.
6. Method: get_status_code(endpoint):
- Concatenate the endpoint to the current base URL.
- Send a GET request to the combined URL.
- Return the status code from the response.
7. Method: get_elapsed_time(endpoint):
- Concatenate the endpoint to the current base URL.
- Record the current time as the start_time.
- Send a GET request to the combined URL.
- Record the current time as the end_time.
- Calculate the elapsed_time as end_time - start_time.
- Return the elapsed_time in seconds.
8. Example usage:
- Create an instance of the AccessApi class with a base URL.
- Test if the URL is alive.
- Set a new base URL if needed.
- Access an endpoint, retrieve data, get status code, or measure elapsed time.
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)