APIs/abstractions Previously iously Abstractions for - - PowerPoint PPT Presentation

apis abstractions previously iously
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

APIs/abstractions

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Hist storically

  • rically

 From Unix to Windows in 90s/00s

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-5
SLIDE 5

 To this, 20 years later  What happened?

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-6
SLIDE 6

Currently this…on the front-end end

 Mobile apps (Android/iOS SDKs)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-7
SLIDE 7

 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

slide-8
SLIDE 8

Meanwhile…on the backend

 Server-side web and API frameworks  Data and event management

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-9
SLIDE 9

Now this…

 Software as a service via APIs  Comprehensive lists at https://rapidapi.com/

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

 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

slide-13
SLIDE 13

 IoT (LiFX, GCP IOT Core, AWS IOT Core)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-14
SLIDE 14

 Food and nutrition APIs (Nutritionix)

 For your web app?

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-15
SLIDE 15

 Spoonacular

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-16
SLIDE 16

Accessing APIs

slide-17
SLIDE 17

#1: Python packages

(if they exist)

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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)

slide-20
SLIDE 20

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'])

slide-21
SLIDE 21

#1.5: Python automatic generation

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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"])

slide-24
SLIDE 24

 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()

slide-25
SLIDE 25

#2 Python requests to REST API

slide-26
SLIDE 26

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

slide-27
SLIDE 27

 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'])

slide-28
SLIDE 28

 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()

slide-29
SLIDE 29

 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()

slide-30
SLIDE 30

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'])

slide-31
SLIDE 31

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()

slide-32
SLIDE 32

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

slide-33
SLIDE 33

API Lab