hypermedia for the ios developer - swipe 2012
DESCRIPTION
What is Hypermedia and how does it help developers create network connected applications.TRANSCRIPT
PlayUp Pty Ltd
Kevin O’Neill
Network Connectedhypermedia for the iOS developer
SWIPE CONFERENCE 2012
Roadmap
• Some background
• Introduction to hypermedia
• Consuming a hypermedia API
• Bit’s and Pieces
SWIPE CONFERENCE 2012
CTO PlayUp
SWIPE CONFERENCE 2012
Blood
SWIPE CONFERENCE 2012
This will be an introduction
SWIPE CONFERENCE 2012
Opinionated
SWIPE CONFERENCE 2012
ReST
SWIPE CONFERENCE 2012
SWIPE CONFERENCE 2012
It’s been mutated to the point of being meaningless
SWIPE CONFERENCE 2012
CRUD = ReST
SWIPE CONFERENCE 2012
URL Patterns = REST
SWIPE CONFERENCE 2012
http://example.com/products/<id>
SWIPE CONFERENCE 2012
Well No
SWIPE CONFERENCE 2012
Hypermedia API’s
SWIPE CONFERENCE 2012
The Good
SWIPE CONFERENCE 2012
Loose Coupling
SWIPE CONFERENCE 2012
Simplicity / Uniformity
SWIPE CONFERENCE 2012
Scale better
SWIPE CONFERENCE 2012
The Bad
SWIPE CONFERENCE 2012
Not the most succinct form
SWIPE CONFERENCE 2012
Cache invalidation is hard
SWIPE CONFERENCE 2012
It takes more thought
SWIPE CONFERENCE 2012
What is a hypermedia API
SWIPE CONFERENCE 2012
Use HTTP Properly*
SWIPE CONFERENCE 2012
Use links to guide clients through processes
SWIPE CONFERENCE 2012
"REST is software design on the scale of decades: every detail is intended to promote software longevity and independent evolution.
Many of the constraints are directly opposed to short-term ef!ciency. Unfortunately, people are
fairly good at short-term design, and usually awful at long-term design." - Roy Fielding
SWIPE CONFERENCE 2012
Consuming a Hypermedia API
SWIPE CONFERENCE 2012
{ "title": "Raiders vs Bulldogs", "short_title": "RAI vs BUL",
"scheduled_start_time": "2012-‐08-‐24T09:35:00Z", "start_time": "2012-‐08-‐24T09:35:00Z", "end_time": "2012-‐08-‐24T12:24:54Z", "clock": { "summary": "2ND 61:56", "minutes": 61, "seconds": 56, "period": 2 },
...
SWIPE CONFERENCE 2012
Content-‐Type
SWIPE CONFERENCE 2012
Content-‐Type
application/json
SWIPE CONFERENCE 2012
Content-‐Type
application/vnd.example.contest.rugby_league+json
SWIPE CONFERENCE 2012
Strategy
SWIPE CONFERENCE 2012
@protocol HTTPResponseHandler <NSObject>
-‐ (void)handle:(NSHTTPURLResponse *)response forRequest:(id <RequestToken>)request;
@end
@interface RegistryHandler : NSObject <HTTPResponseHandler>
-‐ (void)registerHandler:(id<HTTPResponseHandler>)handler for:(id<TypeMatcher>)matcher;
@end
SWIPE CONFERENCE 2012
Errors
SWIPE CONFERENCE 2012
"scores": [ { "total": 34, "summary": "34", "team": { ":href": "http://example.com/teams/569", ":type": "application/vnd.example.sport.team+json" ":links" [ { "rel": "alternate", "href": "http://example.com/teams/569.xml" "type": "application/vnd.example.sport.team+xml" }, { "rel": "alternate", "href": "http://example.com/teams/569.html" "type": "text/html" }
SWIPE CONFERENCE 2012
"scores": [ { "total": 34, "summary": "34", "team": { ":href": "http://example.com/teams/569", ":type": "application/vnd.example.sport.team+json" ":links" [ { "rel": "alternate", "href": "http://example.com/teams/569.xml" "type": "application/vnd.example.sport.team+xml" }, { "rel": "alternate", "href": "http://example.com/teams/569.html" "type": "text/html" }
SWIPE CONFERENCE 2012
"scores": [ { "total": 34, "summary": "34", "team": { ":href": "http://example.co/t/d/dogs.json", ":type": "application/vnd.example.sport.team+json", ":links" [ { "rel": "alternate", "href": "http://example.com/teams/569.xml" "type": "application/vnd.example.sport.team+xml" }, { "rel": "alternate", "href": "http://example.com/teams/569.html" "type": "text/html" }
SWIPE CONFERENCE 2012
Versioning and Alternates
SWIPE CONFERENCE 2012
"scores": [ { "total": 34, "summary": "34", "team": { ":href": "http://example.com/teams/569", ":type": "application/vnd.example.sport.team+json", ":links" [ { "rel": "alternate", "href": "http://example.com/teams/569.xml" "type": "application/vnd.example.sport.team+xml" }, { "rel": "alternate", "href": "http://example.com/teams/569.html" "type": "text/html" }
SWIPE CONFERENCE 2012
Accept
application/vnd.example.sport.team+xml
SWIPE CONFERENCE 2012
Accept
application/vnd.example.sport.team+xml,*/*
SWIPE CONFERENCE 2012
"scores": [ { "total": 34, "summary": "34", "team": { ":href": "http://example.com/teams/569", ":type": "application/vnd.example.sport.team+json", ":links" [ { "rel": "alternate", "href": "http://example.com/teams/569.xml" "type": "application/vnd.example.sport.team+xml" }, { "rel": "alternate", "href": "http://example.com/teams/569.html" "type": "text/html" }
SWIPE CONFERENCE 2012
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"GET"]; [request addValue:@"text/html,*/*" forHTTPHeaderField:@"Accept"];
SWIPE CONFERENCE 2012
Caching
SWIPE CONFERENCE 2012
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:kMemoryCacheSize diskCapacity:kDiskCacheSize diskPath:cache_path]; [NSURLCache setSharedURLCache:sharedCache];
SWIPE CONFERENCE 2012
ETag"9461cf5ecc0f4df6e880ce76479aba90"
Cache-‐Controlmax-‐age=15, public
VaryAccept-‐Language
SWIPE CONFERENCE 2012
NSMutableURLRequest *request = ...
[request setValue:etag forHTTPHeaderField:@"If-‐None-‐Match"];
SWIPE CONFERENCE 2012
NSMutableURLRequest *request = ...
[request setValue:etag forHTTPHeaderField:@"If-‐None-‐Match"];
[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
SWIPE CONFERENCE 2012
static NSUInteger calc_timeout(NSHTTPURLResponse *response) { NSUInteger new_timeout = k_default_expires; NSString *cache_control = [[response allHeaderFields] valueForKey:@"Cache-‐Control"]; if (nil != cache_control) { NSRange maxage_range = [cache_control rangeOfString:@"max-‐age="]; if (maxage_range.location != NSNotFound) { NSString *timeout_str = [cache_control substringFromIndex:NSMaxRange(maxage_range)]; NSRange comma_range = [timeout_str rangeOfString:@","]; if (NSNotFound != comma_range.location) { timeout_str = [timeout_str substringToIndex:comma_range.location]; } new_timeout = (NSUInteger)[timeout_str integerValue]; } } return new_timeout;}
SWIPE CONFERENCE 2012
Bits and Pieces
SWIPE CONFERENCE 2012
Test from other countries
SWIPE CONFERENCE 2012
Charles is your friend
SWIPE CONFERENCE 2012
HTTPShouldUsePipelining
SWIPE CONFERENCE 2012
gzip encoding is your friend
SWIPE CONFERENCE 2012
Proxies Screw You