Web APIs
Object Modeling
We frequently use Java objects to model real objects or entities.
Objects allow us to design software that deals with things in realistic and natural ways.
> Click or hit Control-Enter to run Example.main above
What Did You Model?
Note that one of the free response questions on the exam might be an object modeling question.
HTTP: GET
and POST
HTTP defines many other types of requests, but GET
and POST
are by far the
most common.
-
Every time you load a web page it starts with a
GET
, and usually that’s followed by many otherGET
requests to fetch others parts of the page: style sheets, images, JavaScript code. -
Every time you submit a form it uses a
POST
to send data to the server, at which point your Facebook comment is recorded, or your credit card is charged and an package starts its way toward your house
Web Evolution
The web has gone through many design changes over the years.
-
Static Sites: the web server returns a file from the disk that contains a complete web document
-
Example: most of
cs125.cs.illinois.edu
is a static website
-
-
Dynamic Sites: the web server runs code to produce an HTML document and respond to
POST
requests created by forms-
Example: sites like
my.cs.illinois.edu
are dynamic sites
-
-
Web Apps: most of the site is generated by JavaScript that runs in the user’s browser, with the server providing data as needed
-
Examples:
cs125.cs.illinois.edu/m/grades
, Discourse, Gmail, Google Docs
-
What is the World Wide Web?
-
A protocol: the Hypertext Transfer Protocol (HTTP)
-
A markup language: the Hypertext Markup Language (HTML)
-
A styling language: Cascading Style Sheets (CSS)
-
A programming language: JavaScript
So What’s a Web API?
What’s An API?
In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application software.
In English, an API is a set of functions that perform a set of related and useful tasks.
Example API
Let’s say we wanted to find out the weather at a particular location:
// Get the current weather a particular location
static WeatherInfo getAtLocation(WeatherLocation location)
// Get the current weather a particular location and a particular time
static WeatherInfo getAtLocation(WeatherLocation location, Date date)
// Get a list of possible WeatherInfo objects for a given location string
static WeatherLocation[] searchLocations(String query)
Web APIs
A web API is just an API that you access over the web. Consider that:
-
We can send data to a web server using
POST
and also using URL parameters in aGET
request -
The web server can run code in response
-
And return a response, which does not have to be an HTML document
-
And in many cases custom internet protocols are blocked by firewalls, making it attractive to run APIs over HTTP
Web APIs: Sending Arguments
// Get the current weather a particular location
static WeatherInfo getAtLocation(WeatherLocation location)
To send the location
argument to the getAtLocation
function over the web we
have several options:
-
Stick it the URL:
/api/getAtLocation/(location)/
, which can be mapped to a function call -
Add it as a query parameter:
/api/getAtLocation?location=(location)
-
Use a
POST
request and put it in the body, possibly as JSON:
POST /api/getAtLocation/
{
"location": (location)
}
Web APIs: Returning Results
// Get the current weather a particular location
static WeatherInfo getAtLocation(WeatherLocation location)
In many cases web APIs return results using JSON (JavaScript Object Notation):
{
"consolidated_weather": [
{
"id": 6511056423747584,
"weather_state_name": "Thunder",
"weather_state_abbr": "t",
"wind_direction_compass": "E",
"created": "2018-04-09T02:37:19.655990Z",
"applicable_date": "2018-04-08",
"min_temp": -2.6099999999999999,
"max_temp": 2.2149999999999999,
"the_temp": 2.4950000000000001,
"wind_speed": 2.8707529204565336,
...
Let’s Do An Example
We’ll continue our weather example using the free MetaWeather API and PostMan
What’s Awesome…
Is that there are a gazillion public APIs out there. So go have fun!
What is REST?
You’ll often hear of REST or RESTful web APIs.
-
REST is a design pattern for creating web APIs.
-
URLs map to resources: so
GET
/products
returns a list of all products, whileGET
/products/10
get information about product with ID 10 -
HTTP verbs are meaningful:
GET
gets something,POST
creates a new entity,DELETE
removes one, etc. -
HTTP response codes are meaningful: 200 is
OK
, 405 is not authorized, etc. -
The bodies of requests and responses are in
JSON
REST Examples
Request | Meaning | Java-Like Function |
---|---|---|
|
Retrieve a list of all items |
|
|
Retrieve information about item 81 |
|
|
Retrieve a list of all items that are frisbees |
|
|
Create a new item |
|
More REST Examples
With two additional useful HTTP verbs: PUT
and DELETE
Request | Meaning | Java-Like Function |
---|---|---|
|
Update information about item 81 |
|
|
Delete item 81 |
|
Questions About Internet, Web, or Web APIs?
Internet Design Principles
The internet established many powerful and important design principles. One of the most important is the end-to-end principle:
In networks designed according to the end-to-end principle, application-specific features reside in the communicating end nodes of the network, rather than in intermediary nodes, such as gateways and routers, that exist to establish the network.
End-to-End Example: Reliable Delivery
Reliable delivery is not guaranteed by the core Internet Protocol.
-
Not every application needs it!
-
Moved to the endpoints: that is, implemented on your device and whatever computer you want to communicate with reliably.
End-to-End Principle: Consequences
The end-to-end principles has had powerful implications for internet design and evolution.
-
The core network stays simple
-
The core network doesn’t choose winners and losers
Net Neutrality
Net neutrality is essentially enshrining the end-to-end principle in law.
-
Internet service providers should not discriminate against traffic based on where it comes from, where it is going, or other features
-
This keeps the internet available for the kinds of transformative innovation it has supported since its creation.
This Will Be Your Problem Soon
Please do the right thing.
Announcements
-
We’ve posted some MP6 errata. Please follow these screencasts if you are having a hard time getting your environment set up.
-
The anonymous feedback form remains available on the course website. Use it to give us feedback!
-
My office hours continue today at 11AM in the lounge outside of Siebel 0226.