In: Computer Science
Must be coded in python.
Consuming A Web Service with Python
Consuming data from programmable web based API’s is growing rapidly. According to ProgrammableWeb, in just six years (2010-2016), web API counts increased 758 percent with the majority being REST based. This is driven by a myriad of factors but growth of cloud based applications, (distributed computing by another name), and the resulting need to integrate the different categories is a major factor.
Description
Write a client to consume a REST web service of you choice using Python. There are many listings of such services but an excellent resource is ProgrammableWeb. Your information should produce useful information. I.e. a dump of cryptic data is not the desired product. Examples could be a weather forecast, stock quote, Bible verse, etc.,
Your solution should present the user with a question with the answer contributing to the data used in the REST web service call. For example, a weather forecast would prompt the user to enter a zip code or other location information. Once the web transaction is complete, the program should prompt again with the same question to complete another transaction. This should continue until the user enters a specified termination condition.
You output must be presented in an understandable manner. Dumps of XML, JSON, etc., are substandard and will be graded accordingly.
Hint:
Be sure to click on the examples and look at the address bar of your browser. That is what you will need to generate with the Python requests module.
You are free to use any REST web service you find but I recommend this one if you are looking for something straightforward. One word of caution, some services require you to request access via an API key. The approval may not be immediate so you will want to find something sooner rather than later. The example I listed above requires no approval.
import requests
URL = "http://labs.bible.org/api/?passage="
print("To terminate the program, use 'exit'")
while True:
print("-"*30)
inp = input("For random facts, use 'random': ")
print("-"*30)
if inp.lower()=="random":
#to fetch the result from the API
#requests.get needs a url
response = requests.get(URL+"random")
proper_text ="-->".join(
response.text[3:].split('</b>'))
print(proper_text)
elif inp.lower()=="exit":
print("TERMINATING...")
break
else:
print("Use proper input")
OUTPUT: