Hypermedia for the iOS developer - Swipe 2012

56
PlayUp Pty Ltd Kevin O’Neill Network Connected hypermedia for the iOS developer

description

What is Hypermedia and how does it help developers create network connected applications.

Transcript of Hypermedia for the iOS developer - Swipe 2012

Page 1: Hypermedia for the iOS developer - Swipe  2012

PlayUp Pty Ltd

Kevin O’Neill

Network Connectedhypermedia for the iOS developer

Page 2: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Roadmap

• Some background

• Introduction to hypermedia

• Consuming a hypermedia API

• Bit’s and Pieces

Page 3: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

CTO PlayUp

Page 4: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Blood

Page 5: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

This will be an introduction

Page 6: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Opinionated

Page 7: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

ReST

Page 8: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Page 9: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

It’s been mutated to the point of being meaningless

Page 10: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

CRUD = ReST

Page 11: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

URL Patterns = REST

Page 12: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

http://example.com/products/<id>

Page 13: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Well No

Page 14: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Hypermedia API’s

Page 15: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

The Good

Page 16: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Loose Coupling

Page 17: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Simplicity / Uniformity

Page 18: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Scale better

Page 19: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

The Bad

Page 20: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Not the most succinct form

Page 21: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Cache invalidation is hard

Page 22: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

It takes more thought

Page 23: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

What is a hypermedia API

Page 24: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Use HTTP Properly*

Page 25: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Use links to guide clients through processes

Page 26: Hypermedia for the iOS developer - Swipe  2012

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

Page 27: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Consuming a Hypermedia API

Page 28: Hypermedia for the iOS developer - Swipe  2012

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    },

   ...

Page 29: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Content-­‐Type

Page 30: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Content-­‐Type

application/json

Page 31: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Content-­‐Type

application/vnd.example.contest.rugby_league+json

Page 32: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Strategy

Page 33: Hypermedia for the iOS developer - Swipe  2012

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

Page 34: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Errors

Page 35: Hypermedia for the iOS developer - Swipe  2012

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"                    }

Page 36: Hypermedia for the iOS developer - Swipe  2012

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"                    }

Page 37: Hypermedia for the iOS developer - Swipe  2012

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"                    }

Page 38: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Versioning and Alternates

Page 39: Hypermedia for the iOS developer - Swipe  2012

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"                    }

Page 40: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Accept

application/vnd.example.sport.team+xml

Page 41: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Accept

application/vnd.example.sport.team+xml,*/*

Page 42: Hypermedia for the iOS developer - Swipe  2012

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"                    }

Page 43: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

   NSMutableURLRequest  *request  =        [[NSMutableURLRequest  alloc]  initWithURL:url];    [request            setHTTPMethod:@"GET"];    [request            addValue:@"text/html,*/*"  forHTTPHeaderField:@"Accept"];

Page 44: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Caching

Page 45: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

NSURLCache  *sharedCache  =      [[NSURLCache  alloc]        initWithMemoryCapacity:kMemoryCacheSize        diskCapacity:kDiskCacheSize          diskPath:cache_path];        [NSURLCache  setSharedURLCache:sharedCache];

Page 46: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

ETag"9461cf5ecc0f4df6e880ce76479aba90"

Cache-­‐Controlmax-­‐age=15,  public

VaryAccept-­‐Language

Page 47: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

NSMutableURLRequest  *request  =          ...

[request  setValue:etag  forHTTPHeaderField:@"If-­‐None-­‐Match"];

Page 48: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

NSMutableURLRequest  *request  =          ...

[request  setValue:etag  forHTTPHeaderField:@"If-­‐None-­‐Match"];

[request  setCachePolicy:    NSURLRequestReloadIgnoringLocalCacheData];

Page 49: Hypermedia for the iOS developer - Swipe  2012

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;}

Page 50: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Bits and Pieces

Page 51: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Test from other countries

Page 52: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Charles is your friend

Page 53: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

HTTPShouldUsePipelining

Page 54: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

gzip encoding is your friend

Page 55: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Proxies Screw You

Page 56: Hypermedia for the iOS developer - Swipe  2012

SWIPE CONFERENCE 2012

Kevin O’NeillCTO - PlayUp

[email protected]@kevinoneill

Questions