apis abstractions previously iously
play

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


  1. APIs/abstractions

  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

  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

  4. Hist storically orically  From Unix to Windows in 90s/00s Portland State University CS 430P/530 Internet, Web & Cloud Systems

  5.  To this, 20 years later  What happened? Portland State University CS 430P/530 Internet, Web & Cloud Systems

  6. Currently this…on the front -end end  Mobile apps (Android/iOS SDKs) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  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

  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

  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

  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

  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

  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

  13.  IoT (LiFX, GCP IOT Core, AWS IOT Core) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  14.  Food and nutrition APIs (Nutritionix)  For your web app? Portland State University CS 430P/530 Internet, Web & Cloud Systems

  15.  Spoonacular Portland State University CS 430P/530 Internet, Web & Cloud Systems

  16. Accessing APIs

  17. #1: Python packages (if they exist)

  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

  19. Ex Example: ple: Gi Giph phy  pip install giphy_client 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) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  20. Example: Ex ple: Spotip potipy y (Spo potify) tify)  pip install spotipy 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']) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  21. #1.5: Python automatic generation

  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

  23. Example Ex ple  YouTube API via apiclient.discovery  pip install google-api-core, google-api-python- client, grpcio # 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"]) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  24.  Knowledge Graph API via apiclient.discovery # 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() Portland State University CS 430P/530 Internet, Web & Cloud Systems

  25. #2 Python requests to REST API

  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

  27.  Example: Google Places API (REST via Python) 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']) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  28.  Example: Knowledge Graph API (REST via Python) 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() Portland State University CS 430P/530 Internet, Web & Cloud Systems

  29.  Example: Yelp API 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() Portland State University CS 430P/530 Internet, Web & Cloud Systems

Recommend


More recommend