class: center, middle, inverse # Web APIs ## Without the jargon ### Martin Schweitzer
--- layout: true --- # Introduction ## What is a web API? -- ### It is a web page that has been optimised to be read by a computer rather than by a human --- # Finding sunrise https://www.yr.no/place/Australia/Victoria/Melbourne/ --- # Finding sunrise ```
Sunrise 06:44
``` --- # Finding sunrise ```python import requests import re url = "https://www.yr.no/place/Australia/" \ "Victoria/Melbourne/" regex = '
'\ 'Sunrise (\d\d:\d\d)
' r = requests.get(url) match = re.search(regex, r.text) if match: print(match.group(1)) ``` --- # Finding sunrise ## Does it work? --- # Finding sunrise ## Use a web API https://api.sunrise-sunset.org/json?lat=-37.7340&lng=144.9210 --- # Finding sunrise ``` { "results": { "sunrise": "8:44:15 PM", "sunset": "7:57:55 AM", "solar_noon": "2:21:05 AM", "day_length": "11:13:40", "civil_twilight_begin": "8:17:54 PM", "civil_twilight_end": "8:24:16 AM", "nautical_twilight_begin": "7:47:28 PM", "nautical_twilight_end": "8:54:42 AM", "astronomical_twilight_begin": "7:17:06 PM", "astronomical_twilight_end": "9:25:03 AM" }, "status": "OK" } ``` --- # Finding sunrise ## Use a web API https://api.sunrise-sunset.org/json?lat=-37.7340&lng=144.9210&formatted=0 --- # Finding sunrise ```python import requests import json url = "https://api.sunrise-sunset.org/json?"\ "lat=-37.7340&lng=144.9210" r = requests.get(url) j = json.loads(r.text) print(j['results']['sunrise']) ``` --- # What is a web API -- ## Is it the software? -- ## Is it a protocol? -- ## Is it the data/format? -- ## It is a contract between a supplier and a consumer that a particular URL will return particular data --- # Returned data Does not need to be JSON ```xml
7:17:06 PM
9:25:03 AM
8:17:54 PM
8:24:16 AM
11:13:40
7:47:28 PM
8:54:42 AM
2:21:05 AM
8:44:15 PM
7:57:55 AM
OK
``` --- # RESTful APIs ## Representational State Transfer * Dissertation written by Roy Fielding (2000) * "Architectural Styles and the Design of Network-based Software Architectures" --- ## RESTful APIs - Architectural Constraints * Client Server architecture * Statelessness * Cacheability * Layered system * Code on Demand * Uniform interface --- # RESTful Interfaces Everything that is needed by the server to fulfill a request is contained in the URL. -- More accurately: Everything that is needed by the server to fulfill a request is contained in the request. --- # Getting data via an API http://schweitzer.id.au/cgi-bin/acorn/get_acorn_data.py?stn_num=9021 --- # Getting data via an API ``` { "data": [{ "date": "2010-01-01", "tmax": 31.3, "stn_num": 9021, "tmin": 13.2, "prcp": 0.0 }, { "date": "2010-01-02", "tmax": 37.4, "stn_num": 9021, "tmin": 16.9, "prcp": 0.0 }, { ... ``` --- # Getting data via an API Add start and end date -- Change the format --- # Getting data via an API Look at the documentation... --- # We can get more than text from an API ``` http://maps.eatlas.org.au/maps/gwc/service/wms? SERVICE=WMS&REQUEST=GetMap&VERSION=1.1.1&LAYERS= ea-be%3AWorld_Bright-Earth-e-Atlas-basemap&STYLES= &FORMAT=image%2Fjpeg&TRANSPARENT=false&HEIGHT=256& WIDTH=256&SRS=EPSG%3A3857&BBOX=15028131.257091932, -5009377.085697311,20037508.342789244, -7.081154551613622e-1 ``` [x](http://maps.eatlas.org.au/maps/gwc/service/wms?SERVICE=WMS&REQUEST=GetMap&VERSION=1.1.1&LAYERS=ea-be%3AWorld_Bright-Earth-e-Atlas-basemap&STYLES=&FORMAT=image%2Fjpeg&TRANSPARENT=false&HEIGHT=256&WIDTH=256&SRS=EPSG%3A3857&BBOX=15028131.257091932,-5009377.085697311,20037508.342789244,-7.081154551613622e-10) --- # Example http://schweitzer.id.au/troismots/ --- # FAIR and APIs Findable, Accessible, Interoperable and Reusable -- F3. (Meta)data are registered or indexed in a searchable resource. -- A1 (meta)data are retrievable by their identifier using a standardised communications protocol. --- # ANDS APIs https://developers.ands.org.au/services/collections-registry-api/ --- # Find metadata from ANDS (Research Data Australia) ``` https://researchdata.ands.org.au/registry/services/ api/getMetadata?q=class:(collection) AND group_search:("Australian Ocean Data Network") &rows=30 ``` [try](https://researchdata.ands.org.au/registry/services/api/getMetadata?q=class:collection AND group_search:"Australian Ocean Data Network" &rows=30) --- # Find metadata from ANDS (Research Data Australia) ``` { "status": "success", "message": { "numFound": "11530", "start": "0", "docs": [{ "id": "690498", "slug": "coral-reef-health-check-australia", "key": "AODN\/e8854605-d169-44ca-9364-aa5c2c87ff67", "display_title": "Coral reef health database" "class": "collection" }, { ... ``` --- # Find metadata from ANDS (Research Data Australia) ``` https://researchdata.ands.org.au/registry/services/ api/getMetadata? q=key:(%22AODN\/e8854605-d169-44ca-9364-aa5c2c87ff67%22) &fl=* ``` [try](https://researchdata.ands.org.au/registry/services/api/getMetadata?q=key:%22AODN\/e8854605-d169-44ca-9364-aa5c2c87ff67%22&fl=* ) --- # Find metadata from ANDS ``` "quality_level": "2", "originating_source": "http:eatlas.org.au", "record_modified_timestamp": "2016-09-02T02:51:09Z", "record_created_timestamp": "1970-01-01T00:00:00Z", "simplified_title": "Coral reef health database", "matching_identifier_count": "2", "description_type": ["brief"], "access_rights": "open", "identifier_value": ["e8854605-d169-44ca-9364-aa5c2c87ff67", "identifier_value_search": ["d169-44ca-9364-aa5c2c87ff67", "identifier_type": ["global", "uri"], "related_info_search": ["http://www.reefcheckaustralia.org "date_from": ["2001-01-01T01:00:00Z"], "date_to": ["2015-01-15T01:00:00Z"], "subject_value_resolved": ["biota"], "subject_type": ["local"], ``` --- # ANDS Vocabulary Service ``` http://vocabs.ands.org.au/repository/api/lda/ ga/geology/v1-0/concept?_page=1 ``` [try](http://vocabs.ands.org.au/repository/api/lda/ga/geology/v1-0/concept?_page=1) --- # ANDS Data API Registry http://www.ands.org.au/online-services/enhancing-data-service-discovery [x](http://www.ands.org.au/online-services/enhancing-data-service-discovery) --- Jeff Bezos' Memo https://plus.google.com/+RipRowan/posts/eVeouesvaVX 1. All teams will henceforth expose their data and functionality through *APIs* (service interfaces). -- 2. Teams must communicate with each other through these interfaces. -- 3. There will be no other form of interprocess communication allowed. -- 4. It doesn't matter what technology they use. -- 5. All *API*s, without exception, must be designed from the ground up to be externalizable. -- 6. Anyone who doesn't do this will be fired. -- 7. Thank you; have a nice day! --- class: center, middle # Thank You ### Slides: `schweitzer.id.au/webapi` ### Questions? --- ---