APIs/abstractions Previously iously Abstractions for - - PowerPoint PPT Presentation
APIs/abstractions Previously iously Abstractions for - - PowerPoint PPT Presentation
APIs/abstractions Previously iously Abstractions for infrastructure to ease operations (Ops) Manage complexity in deploying software systems Examples covered VMs, containers Infrastructure as a Service Platform as a
Previously iously
Abstractions for infrastructure to ease operations (Ops)
Manage complexity in deploying software systems
Examples covered…
VMs, containers Infrastructure as a Service Platform as a Service Functions as a Service Infrastructure as Code
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Bu But, t, wh what t about ut th the a e app pps s th them emse selv lves? es?
Emerging software abstractions for building functionality Some examples so far
Your homework (e.g. Flask)
Common web application abstractions as a framework No need to program sockets, HTTP
, cookies, sessions, routing, HTML generation, database communication, etc.
Imagine writing it in C (from scratch!)
Blockchain
Immutable, append-only distributed ledger as a library (HyperLedger Sawtooth) No need to program consensus protocol, public-private key generation, Merkle tree
construction, transaction validation, proof-of-work mechanism, etc.
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Hist storically
- rically
From Unix to Windows in 90s/00s
Portland State University CS 430P/530 Internet, Web & Cloud Systems
To this, 20 years later What happened?
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Currently this…on the front-end end
Mobile apps (Android/iOS SDKs)
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Web apps (client-side JS frameworks) Or both
Progressive web apps
Seamless off-line/on-line experience
Native/hybrid applications
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Meanwhile…on the backend
Server-side web and API frameworks Data and event management
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Now this…
Software as a service via APIs Comprehensive lists at https://rapidapi.com/
Portland State University CS 430P/530 Internet, Web & Cloud Systems
On GCP…
Maps API, Search API, Knowledge Graph API, Analytics, Ads, Voice
transcription, Image analysis, Video analysis, Translation, etc.
See Pokemon Go architecture
Portland State University CS 430P/530 Internet, Web & Cloud Systems
And nd ext xtern ernally ally
Payments (PayPal, Venmo) Social media (Twitter, Facebook, Instagram, Pinterest, Spotify)
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Multi-service instant
messaging and communications (Twilio, Nexmo)
Communicate with
customers using whatever they have!
Portland State University CS 430P/530 Internet, Web & Cloud Systems
IoT (LiFX, GCP IOT Core, AWS IOT Core)
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Food and nutrition APIs (Nutritionix)
For your web app?
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Spoonacular
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Accessing APIs
#1: Python packages
(if they exist)
PyPI PI
Python Package Index
Packages installable via pip (as we have been doing all quarter)
Most popular web APIs come with supported Python packages Many are auto-generated via Swagger Note
Code examples are written for simplicity NEVER specify API keys within source code
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Ex Example: ple: Gi Giph phy
pip install giphy_client
Portland State University CS 430P/530 Internet, Web & Cloud Systems
import giphy_client # Create app and API_KEY at https://developers.giphy.com # Note insecure pattern: Should supply from environment variables API_KEY = os.environ.get('YOUR_GIPHY_API_KEY') api_instance = giphy_client.DefaultApi() api_response = api_instance.gifs_search_get(API_KEY, 'clapback', limit=1, rating='g', lang='en', fmt='json') if len(api_response.data) > 0: print(api_response.data[0].embed_url)
Ex Example: ple: Spotip potipy y (Spo potify) tify)
pip install spotipy
Portland State University CS 430P/530 Internet, Web & Cloud Systems
import spotipy from spotipy.oauth2 import SpotifyClientCredentials # Obtain credentials via https://developer.spotify.com # Pass to client via environment variables that are not committed to git CLIENT_ID=os.environ.get('SPOTIFY_CLIENT_ID') CLIENT_SECRET=os.environ.get('SPOTIFY_CLIENT_SECRET') # Return a spotify playlist given query credentials = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) spotify = spotipy.Spotify(client_credentials_manager=credentials) results = spotify.search('Foo Fighters', limit=1, type='playlist') if len(results['playlists']['items']) > 0: print(results['playlists']['items'][0]['external_urls']['spotify'])
#1.5: Python automatic generation
API I bui uilding lding pa pack ckages ages
Parses a REST API specification Automatically constructs package to support calling into API
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Ex Example ple
YouTube API via apiclient.discovery
pip install google-api-core, google-api-python-
client, grpcio
Portland State University CS 430P/530 Internet, Web & Cloud Systems
# https://developers.google.com/youtube/v3/docs/search/list from apiclient.discovery import build DEVELOPER_KEY = os.environ.get("DEVELOPER_KEY") YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" # Build the request for YouTube search content youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q = "Grumpy Cat", part = "id, snippet", maxResults = 1, ).execute() for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": print("%s" % search_result["snippet"]["title"]) print("%s" % search_result["id"]["videoId"])
Knowledge Graph API via apiclient.discovery
Portland State University CS 430P/530 Internet, Web & Cloud Systems
# Assumes pip install of google-api-python-client import apiclient DEVELOPER_KEY = os.environ.get('DEVELOPER_KEY') kgsearch = apiclient.discovery.build('kgsearch', 'v1', developerKey=DEVELOPER_KEY) request = kgsearch.entities().search(query='Portland State University', limit=1) # Returns a dict of search result info result = request.execute()
#2 Python requests to REST API
Lang ngua uage ge su supp pport t for HTTP TP
Most languages with native support for sending HTTP requests and
parsing HTTP responses
Typically, GETs and POSTs with JSON being returned Packages are wrappers for the underlying REST API call over HTTP
Python Requests
(My) preferred package for HTTP Most examples will use this
Portland State University CS 430P/530 Internet, Web & Cloud Systems
Example: Google Places API (REST via Python)
Portland State University CS 430P/530 Internet, Web & Cloud Systems
import requests def post(self): """ Sends search box information to Google Places API and returns response """ api_key = 'YOUR_API_KEY' keyword = request.form['search'] # Base URL for Google Places API url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' # Lat/Long of Portland, OR location = 'location=45.5122,-122.6587&radius=1500' # Places type variable place = 'restaurant' # API response response = requests.get(url + location + '&type=' + place + '&keyword=' + keyword + '&key=' + api_key) r = response.json() return render_template('home.html', response=r['results'])
Example: Knowledge Graph API (REST via Python)
Portland State University CS 430P/530 Internet, Web & Cloud Systems
import requests # Enter your Knowledge Graph API Key here # (generate via APIs & Services:Credentials) api_key = 'YOUR_KNOWLEDGE_GRAPH_API_KEY' term = 'Portland State University' # Use term and api_key string to formulate a URL for hitting the # Knowledge Graph API endpoint. Note f-strings are Python 3.6 url_string = f'https://kgsearch.googleapis.com/v1/entities:search?query={term}&key={api_key}&l imit=1&indent=True' # Take given response, parse it as a json object, and return a dict response = requests.get(url_string) result_dict = response.json()
Example:
Yelp API
Portland State University CS 430P/530 Internet, Web & Cloud Systems
import requests def yelpSearch(self, title): # Craft Yelp's API endpoint URL, set location to Portland and limit results url = 'https://api.yelp.com/v3/businesses/search?term=' + title + '&location=portland&limit=3' # Declare the api header with api key (Fill in your own key) headers={'Authorization': "Bearer YOUR_API_KEY"} # Query the API endpoint, parse JSON response and return dict response = requests.get(url, headers=headers) return response.json()
Side de-by by-side side (Go Google gle Maps) ps)
pip install googlemaps pip install requests
Portland State University CS 430P/530 Internet, Web & Cloud Systems
# Requires enabling geocoding API and generation of an API key in Google Cloud import googlemaps import os API_KEY = os.environ.get('API_KEY') gmaps = googlemaps.Client(key=API_KEY) boba_query = 'Mo Cha Tea House, Portland, OR' boba_shops = gmaps.geocode(boba_query) for shop in boba_shops: print(shop['formatted_address']) import requests import json import os API_KEY = os.environ.get('API_KEY') boba_query = 'Mo Cha Tea House, Portland, OR' resp = requests.get( f'''https://maps.googleapis.com/maps/api/geocode/json?address={boba_ query}&key={API_KEY}''') boba_shops = json.loads(resp.text) for shop in boba_shops['results']: print(shop['formatted_address'])
Side de-by by-side side (Nut utrit ritionix ionix)
pip install nutritionix nutritionix-client pip install requests
Portland State University CS 430P/530 Internet, Web & Cloud Systems
from nutritionix import NutritionixClient recipe = 'Pasta Salad' # Get the credentials from environment variables (more secure than in code) APP_ID=os.environ.get('NIX_APP_ID') API_KEY=os.environ.get('NIX_API_KEY') nut_info = NutritionixClient(application_id=APP_ID, api_key=API_KEY) result = nut_info.search(q=recipe, limit=3, offset=0)
def nutritionix(self, ingredient): url = 'https://trackapi.nutritionix.com/v2/natural/nutrients' # Declare the api header with api key (Fill in your own App ID and Key) headers = {"Content-Type":"application/json", "x-app-id":"YOUR_APP_ID", "x-app-key":"YOUR_APP_KEY"} # Declare POST query as a dictionary, set query field to ingredient body = {"query":ingredient,"timezone": "US/Eastern"} # Make request to API endpoint, parse JSON response and return dict response = requests.post(url, headers = headers, json = body) return response.json()
Looking ahead…
Two on the backend
Machine Learning APIs/models as a service (today) Backend as a Service (next class)
Portland State University CS 430P/530 Internet, Web & Cloud Systems