Covid19 RapidAPI Example

To begin the API journey. You need to find an API provider.

  • RapidAPI is a great option. You must setup and account, but there are many free options.
  • Goto this page for starters, the Corona virus World and India data- Under Code Snippets pick Python - Requests

RapidAPI, you will select Python Requests type of code to work with you Notebook.

  • The url is the endpoint to which the API is directed
  • The headers is a dictionary data structure to send special messaging to the endpoint
  • The requests.request() python function is used to send a request and retrieve their responses
  • The response variable receives result of of the request in JSON text

Next step, is to format the response according to your data science needs

"""
Requests is a HTTP library for the Python programming language. 
The goal of the project is to make HTTP requests simpler and more human-friendly. 
"""
import requests

url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"

headers = {
	"X-RapidAPI-Key": "cb84e1853amsh36bd127c21f5c41p12163cjsn5ce359cf2f1a",
	"X-RapidAPI-Host": "corona-virus-world-and-india-data.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers)

# print(response.json())

"""
RapidAPI is the world's largest API Marketplace. 
Developers use Rapid API to discover and connect to thousands of APIs. 
"""
url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"
headers = {
    'x-rapidapi-key': "dec069b877msh0d9d0827664078cp1a18fajsn2afac35ae063",
    'x-rapidapi-host': "corona-virus-world-and-india-data.p.rapidapi.com"
}

# Request Covid Data
response = requests.request("GET", url, headers=headers)
# print(response.text)  # uncomment this line to see raw data

# This code looks for "world data"
print("World Totals")
world = response.json().get('world_total')  # turn response to json() so we can extract "world_total"
for key, value in world.items():  # this finds key, value pairs in country
    print(key, value)

print()

# This code looks for USA in "countries_stats"
print("Country Totals")
countries = response.json().get('countries_stat')
for country in countries:  # countries is a list
    if country["country_name"] == "USA":  # this filters for USA
        for key, value in country.items():  # this finds key, value pairs in country
            print(key, value)
World Totals
total_cases 509,268,964
new_cases 204,268
total_deaths 6,242,509
new_deaths 630
total_recovered 461,827,849
active_cases 41,198,606
serious_critical 42,510
total_cases_per_1m_population 65,334
deaths_per_1m_population 800.9
statistic_taken_at 2022-04-24 11:18:01

Country Totals
country_name USA
cases 82,649,779
deaths 1,018,316
region 
total_recovered 80,434,925
new_deaths 0
new_cases 0
serious_critical 1,465
active_cases 1,196,538
total_cases_per_1m_population 247,080
deaths_per_1m_population 3,044
total_tests 1,000,275,726
tests_per_1m_population 2,990,303

Here is another example of an API being used

import requests

url = "https://motivational-quotes1.p.rapidapi.com/motivation"

payload = {
	"key1": "value",
	"key2": "value"
}
headers = {
	"content-type": "application/json",
	"X-RapidAPI-Key": "cb84e1853amsh36bd127c21f5c41p12163cjsn5ce359cf2f1a",
	"X-RapidAPI-Host": "motivational-quotes1.p.rapidapi.com"
}

response = requests.request("POST", url, json=payload, headers=headers)
ratings_dict = {}
print(response.text)

rating = int(input("What would you rate the quote on a scale of 1-10?"))
status = 0
while status == 0:
	if rating <= 10:
		if rating < 1:
			print("Please type an integer between 1-10")
			rating = int(input("What would you rate the quote on a scale of 1-10, this time?"))
		else: 
			ratings_dict[response.text] = rating
			status += 1
			print("Thank you for rating the quote! We will try to suggest quotes with a smiliar rating of", rating)
			print("Your ratings so far:")
			print(ratings_dict)

	else:
		print("Please type an integer between 1-10")
		rating = int(input("What would you rate the quote on a scale of 1-10, this time?"))
"The conditions of conquest are always easy. We have but to toil awhile, endure awhile, believe always, and never turn back."
-Seneca
Thank you for rating the quote! We will try to suggest quotes with a smiliar rating of 7
Your ratings so far:
{'"The conditions of conquest are always easy. We have but to toil awhile, endure awhile, believe always, and never turn back."\n-Seneca': 7}

Hacks

Find and use an API as part of your project. An API and a little coding logic will be a big step toward getting meaningful data for a project. There are many API providers, find one that might work for your project to complete this hack. When picking an API you are looking for something that will work with either JavaScript Fetch or Python Request. Here are some samples, these are not qualified in any way.

Show API and format results in either Web Page or Jupyter Notebook. Ultimately, I will expect that we do APIs in backend (Python/Flask). However, for this Hack you can pick your preference. We will discuss pros and cons in next API tech talk.