Fall 21 Lecture9 APIs
Fall 21 Lecture9 APIs
Gregory Gay
TDA 594/DIT 593 - November 30, 2021
Implementing Features
• Features often implemented a standalone services.
• Services can be accessed through an API.
• e.g., web APIs generally use REST interfaces.
• Good API design is crucial to creating a feature
reusable in many clients.
2
REST
• REST is a web-based API format.
• Services provide resources (through URLs) designed to
be consumed by programs rather than by people.
• Design Principles:
• Stateless
• Resource-Based (URI)
• Uniform Interface (GET, PUT, POST, DELETE)
• Links describe relationships
• Cacheable and monitorable using standard internet tools
3
Today’s Goals
• Creating REST APIs.
• Web services with common invocation methods.
• REST API design practices.
• Designing Reusable APIs.
• Features that can be substituted for other features.
4
Hypertext Transfer Protocol (HTTP)
5
HTTP
• Communication protocol for networked systems.
• Defines how to exchange or transfer hypertext between
nodes in a network.
• How your computer accesses a webpage.
• Defines an API based on requests.
• Requests performed using verbs.
• I get a page, post an update, delete a photo, put up
information.
6
Retrieving Information (GET)
9
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Additional Verbs
• HEAD
• “Give me the metadata”
• TRACE
• “Show me what changes have been made”
• OPTIONS
• “What verbs have you implemented for this resource?”
• PATCH
• “Apply partial resource modification”
10
Anatomy of an HTTP Request
VERB URI HTTP Version
Request Header
Request Body
12
Anatomy of an HTTP Response
HTTP Version Response Code
Response Header
Response Body
HTTP/1.1 200 OK
Date: Sat, 23 Aug 2014 18:31:04 GMT, Server: Apache/2, Last-Modified: Wed,
01 Sep 2004 13:24:52 GMT, Accept-Ranges: bytes, Content-Length: 32859,
Cache-Control: max-age=21600, must-revalidate, Expires: Sun, 24 Aug 2014
00:31:04 GMT, Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html
xmlns='http://www.w3.org/1999/xhtml'> <head><title>Hypertext Transfer
Protocol -- HTTP/1.1</title></head> <body> …
14
HTTP Status Codes
• Common Responses:
• 200 Ok (succeeded)
• 201 Created (a new resource)
• 202 Accepted (not completed)
• 204 No Content (fulfilled request, nothing to return)
• 205 Reset content (reload page)
• 301 Redirection: moved permanently
• 400 Bad request
• 401 Unauthorized
• 404 Not found
15
Representational State Transfer
(REST)
16
Representational State Transfer
18
REST - Not a Standard
• Not a standard, but an architectural pattern.
• You can't bottle up a pattern.
• You can only understand it and design your web services
based on it.
• REST does prescribe the use of standards (reuse):
• HTTP, URL
• XML/HTML/JPEG/etc. (Resource Representations)
• Frameworks like Hydra offer advice for designing generic,
reusable REST interfaces.
19
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Verbs in REST
• Verbs describe actions that are applicable to nouns
• Using different verbs for every noun would make
widespread communication impossible.
• Some verbs only apply to a few nouns.
• In REST, we use universal verbs.
• All RESTful services offer the same interface.
• Based on HTTP requests and responses.
20
REST Fundamentals
• Services offer resources.
• All resources have a unique URI.
• URIs tell a client that there's a concept somewhere.
• Clients request a specific representation of the concept
from the representations the server makes available.
• HTTP verbs are used to retrieve or manipulate
resources in a clear, universal manner.
21
Representation Formats
• Client and server should be able to comprehend data.
• Structured content often JSON or XML
• Representation should completely represent resource.
• If partial representation needed, break into subresources.
• Smaller representation = easier to transfer = less time
required to create representation = faster services.
• Representation should be capable of linking resources
to each other via URIs.
22
Verb Guarantees
• GET, OPTIONS, TRACE, and HEAD are safe.
• Should not change the resource in any way.
• Be careful - no technical limitations ensuring safety.
• PUT and DELETE are idempotent.
• Repeated requests have same effect as a single request.
• Safe operations are also idempotent.
• POST is not idempotent.
23
Elements of Web Architecture
• Firewalls decide which HTTP messages get out,
and which get in.
• These components enforce web security.
• Routers decide where to send HTTP messages.
• These components manage web scalability.
• Caches decide if saved copy of resource used.
• These components increase web performance.
24
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Firewalls
25
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Privacy of Content
• Firewalls, routers, caches base decisions only on
HTTP header.
• Should never examine the request body.
• Letter analogy: Postal service doesn’t look inside
your letter (this is illegal), they just act based on
addressing on the outside.
• The content should not matter, just the metadata.
• Protects privacy of data.
26
The Parts Depot Web Store
• Parts Depot, Inc wants to deploy a web service to
enable its customers to:
• Get a list of parts.
• Get detailed information about a particular part.
• Submit a Purchase Order (PO).
• How would you architect this?
• Let’s discuss the RESTful way to design this.
27
The RESTful Way
28
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Retrieving a List of Parts
Service: Get a list of parts
• Web service offers a URL to parts list resource.
• Client posts GET request to URL to get parts list:
• http://www.parts-depot.com/parts
• How the web service generates parts list is
completely transparent to the client.
• Enforces loose coupling.
29
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
REST Fundamentals:
1. Create a resource for every service.
2. Identify each resource using a URI.
30
Data Returned: Parts List
<?xml version="1.0"?>
<Parts>
<Part id="00345" href="http://www.parts-depot.com/parts/00345"/>
<Part id="00346" href="http://www.parts-depot.com/parts/00346"/>
<Part id="00347" href="http://www.parts-depot.com/parts/00347"/>
<Part id="00348" href="http://www.parts-depot.com/parts/00348"/>
</Parts>
32
Retrieving Details on a Part
Service: Get detailed information about a particular
part
• Web service makes available a URL to each part
resource.
• A client can request information on a specific part
by posting GET request to
http://www.parts-depot.com/parts/00345
33
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Data Returned: Part 00345
<?xml version="1.0"?> <Part>
<Part-ID>00345</Part-ID> <Name>Widget-A</Name>
<Description>This part is used within the frap assembly</Description>
<Specification href="http://www.parts-depot.com/parts/00345/specification"/>
<UnitCost currency="USD">0.10</UnitCost>
<Quantity>10</Quantity>
</Part>
35
Designing Services with REST
36
Designing Services With REST
• Destination URL placed in HTTP header.
• Firewalls, routers, caches make decisions based on
information in the HTTP Header.
• Infrastructure will not work if destination not in URL!
• HTTP header should identify final destination, not
intermediate destination.
37
Designing Services With REST
• Client requests should be idempotent – multiple
calls should lead to the “same” response.
• Server responses are “idempotent”, but only in
terms of the meaning of information and not
necessarily the content.
• Think of a URL that always returns the current time…
38
PUT and POST
• PUT is idempotent, POST is not.
• Multiple POSTs may create multiple resources.
• PUT requires a full resource ID path.
• Client creates resource.
• POST does not require full resource ID path.
• Server notifies client of resource location.
• Post can still be used for resource updates.
39
PUT and POST
• PUT http://MyService/Persons/
• Won't work. PUT requires a complete URI.
• PUT http://MyService/Persons/1
• Insert a new person, PersonID=1, if it does not already
exist or update existing resource with the payload.
• POST http://MyService/Persons/
• Insert new person (using the payload), generate new ID.
• POST http://MyService/Persons/1
• Update the existing person where PersonID=1
40
Handling POSTs
• Other methods are idempotent, but POST creates
new resources.
• Multiple POSTs of same data must be harmless.
• Put message ID in a header or in the message body.
• This renders multiple posts harmless.
• Prevents “multiple charge” issue with web stores.
41
Handling POSTs
• Many ways to do this:
• Exact: client or server-side unique transaction ID.
• Heuristic: check and remove “likely duplicates”.
• Wasted IDs are irrelevant.
• Duplicated POSTs are not acted on by the server
• Server must send back same response original
POST got, in case the application is retrying
because it lost the response.
42
Idempotence
• What does this mean, strictly speaking?
• Call to server must return the same thing each time?
• No side effects?
• What about changing data?
• Time-of-day service.
• Each GET call returns a new time. Is this RESTful?
• As long as the resource is constant.
• The value does not need to be constant, just how we access it.
43
Statelessness
• Each request contains all information needed to
service the request.
• No client state is held on the server.
• Benefits in scalability and availability.
• Performance may be worse (multiple requests may be
needed to get information).
44
Well-Structured URIs
• Avoid using spaces. Use _ (underscore) or –
(hyphen) instead.
• Remember that URIs are case insensitive.
• Stay consistent with naming conventions.
• URIs are long lasting.
• If you change the location of a resource, keep old URI.
• Use status code 300 and redirect the client.
45
Well-Structured URIs
• Avoid verbs for resource names unless resource is
actually an operation or a process.
• Bad URIs:
• http://MyService/FetchPerson/Mike
• http://MyService/DeletePerson?id=Mike
• Good URI:
• http://MyService/Persons/Mike
• You can apply verbs to this resource.
46
Food For Thought
What if Parts Depot has a million parts, will there
be a million static pages?
http://www.parts-depot/parts/000000
http://www.parts-depot/parts/000001
...
http://www.parts-depot/parts/999999
47
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Food For Thought
• URLs are logical.
• Express what resource is desired, not physical object.
• Changes to the implementation of the resource will be
transparent to clients (loose coupling!).
• All parts stored in a database. Web service will receive
URL request, parse it for ID, query the database, and
generate the response document at runtime.
48
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Food For Thought
Physical URLs Logical URLs
http://www.parts-depot/parts/000000.html http://www.parts-depot/parts/000000
http://www.parts-depot/parts/000001.html http://www.parts-depot/parts/000001
... ...
http://www.parts-depot/parts/999999.html http://www.parts-depot/parts/999999
49
Slide from: REST by Roger Costello and Timothy Kehoe http://www.xfront.com/REST-full.ppt
Designing Reusable APIs
Adapted from Verborgh, R. and Dumontier, M. (2018), “A Web API ecosystem through
feature-based reuse”, Internet Computing, IEEE, Vol. 22 No. 3, pp. 29–37.
50
Human-based Interaction
• Well-designed websites base user interaction on
common interaction patterns.
• Interaction patterns are reused across the web.
51
Code-based Interaction
• API reuse is difficult, as similar APIs often have
very different interfaces.
• Different number of HTTP requests to perform a task.
• Different JSON bodies.
• A client can’t easily swap an API that posts a photo
to Facebook for one that posts to Twitter.
52
Designing Reusable APIs
• APIs should be reusable in many applications.
• However, they often require custom code, and rarely
share interaction patterns.
• Result: # of APIs grows rapidly.
• API design should center around common
interaction patterns.
• Like human-based interaction.
53
Designing Reusable APIs
• Individual APIs are reusable.
• We can use one to post a photo to Facebook.
• However, APIs are often not substitutable.
• If we want to post photos to Facebook or Twitter, the APIs
can’t be swapped.
• We should be able to choose and swap APIs that perform
the same kind of functionality.
54
Top-Down API Design
• API is monolithic.
• Clients couple to specific
interface to interact with
lower-level parts.
• Only clear invocation
mechanisms are parameter
names and types.
55
Bottom-Up API Design
• A feature offers interface to a
common function type.
• Search text, upload file, update
status, etc.
• Should be simple, self-describing.
• Clients couple to select
features, not entire API.
• APIs reuse features.
• Whole API may not be identical.
56
1: Web APIs Consist of Features with Common Interfaces
57
1: Web APIs Consist of Features with Common Interfaces
• OpenSearch
specifies a common
format for performing
search and publishing
the results.
• Also specifies a
format for
auto-completion of
partial search terms.
58
2: Partition Interface for Feature Reuse
• If a feature is available elsewhere, reuse it in your
API instead of implementing it yourself.
• Clients could perform task with any API offering feature.
• If designing a new feature, make it available
separately for reuse.
• Feature-specific repository, documentation.
• Prioritize reuse when possible, make new
functionality available as features.
59
2: Partition Interface for Feature Reuse
• Atom defines XML format for <?xml version="1.0" encoding="utf-8"?>
<title>Example Feed</title>
• E.g., blog posts <link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
• Could create feature for “post a <author>
<name>John Doe</name>
blog entry” that uses Atom as a </author>
60
3: API Responses Should Advertise Relevant Features
61
3: API Responses Should Advertise Relevant Features
GET /
• Hypermedia: Embed links to {
"version": "1.2.3",
resources within JSON body "description": "Example API to
of HTTP response. manage orders",
"links": [
{ "rel": "orders",
• Ex: GET call to entry point "href": "/orders" },
{ "rel": "customers",
returns links to accessible "href": "/customers"},
resources. { "rel": "customer-by-id",
"href": "/customer/{id}"},
{ "rel": "customer-by-email",
• GitHub API uses hypermedia "href": "/customer{?email}"},
...
to broadcast functionality. ]
}
62
4: Features Describe their Functionality and Invocation
63
4: Features Describe their Functionality and Invocation
• Hydra is a standard
for generic APIs.
• Includes ways to
describe functionality
and invocation.
• Hydra Console infers
details from Hydra
APIs and renders
documentation.
64
We Have Learned
• REST is a web-based API format.
• Services provide resources (through URLs) designed to
be consumed by programs rather than by people.
• Design Principles:
• Stateless
• Resource-Based (URI)
• Uniform Interface (GET, PUT, POST, DELETE)
• Links describe relationships
• Cacheable and monitorable using standard internet tools
65
We Have Learned
• APIs should be designed to be reusable.
• APIs should be split into features.
• Features should have a common interface with
compatible features with separate implementations.
• The overall API should be partitioned into these separate
features with their own interfaces.
• APIs should advertise available features.
• Features should broadcast their functionality and
invocation details.
66
Next Time
• System-Level Test Design
67