Python Rapid API
Example of implementing API
"""
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.
RapidAPI is the world's largest API Marketplace.
Developers use Rapid API to discover and connect to thousands of APIs.
The api used below are from
https://rapidapi.com/collection/list-of-free-apis
https://rapidapi.com/weatherbit/api/weather/
"""
import requests
url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/3hourly"
querystring = {"lat":"35.5","lon":"-78.5","postal_code":"92127"} # Added zip code to the query string
headers = {
"X-RapidAPI-Key": "e518d32506mshebee640d7001529p1f8128jsnaa52ade28413",
"X-RapidAPI-Host": "weatherbit-v1-mashape.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
# print(response.text) # <- raw data is too long !!!
print("\n------ Summary weather ---- \n")
res = response.json() # convert response to python json object
print(res["city_name"])
# only print the first item in the data
data0 = res["data"][0]
print(data0["timestamp_local"]+" "+data0["weather"]["description"] +" " + str(data0["temp"]))
print("\n------ Detailed weather API response ---- \n")
# here are all the items in data0
for key, value in data0.items(): # print all items in data0
print(key, value)