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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

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:

  1. 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”
  2. A method to get the current URL base.
  3. A method to set the current URL base.
  4. A method to test if the URL is responding to GET requests to allow for a simple alive test.
  5. 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.
  6. 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.
  7. 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.
Expert Solution
Step 1: Algorithm :

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.

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education