hypermedia in practice - familysearch developers conference 2014

99
© 2013 by Intellectual Reserve, Inc. All rights reserved. Hypermedia in Practice

Upload: ryan-heaton

Post on 14-Jun-2015

81 views

Category:

Technology


0 download

DESCRIPTION

An introduction to how to use Hypermedia to write effective client applications.

TRANSCRIPT

Page 1: Hypermedia In Practice - FamilySearch Developers Conference 2014

1© 2013 by Intellectual Reserve, Inc. All rights reserved.

Hypermedia in Practice

Page 2: Hypermedia In Practice - FamilySearch Developers Conference 2014

2© 2013 by Intellectual Reserve, Inc. All rights reserved.

The Goal

Page 3: Hypermedia In Practice - FamilySearch Developers Conference 2014

3© 2013 by Intellectual Reserve, Inc. All rights reserved.

Learn how to use hypermedia for better Web service APIs.

The Goal

Page 4: Hypermedia In Practice - FamilySearch Developers Conference 2014

4© 2013 by Intellectual Reserve, Inc. All rights reserved.

Better?● More Adaptable● More Robust● More Maintainable● Less Pain

Learn how to use hypermedia for better Web service APIs.

The Goal

Page 5: Hypermedia In Practice - FamilySearch Developers Conference 2014

A Review

● Web service API– A set of digital services and/or resources

made accessible over a computer network.

● HTTP– Hypertext Transfer Protocol

– Defines how to make requests and interpret responses.

Page 6: Hypermedia In Practice - FamilySearch Developers Conference 2014

A Review

● Resources– The units of data making up the “matter” of

the API.

– The “subject” of HTTP operations.

● Uniform Resource Identifier– The locator for a resource.

– Includes a domain, path, query.

Page 7: Hypermedia In Practice - FamilySearch Developers Conference 2014

A Review

● Resource Representations– How resources are serialized to a stream of

bytes.

– Often JSON and XML.

– a.k.a. “media types” or “data formats”

● See “Architecture of the World Wide Web”– http://www.w3.org/TR/webarch/

Page 8: Hypermedia In Practice - FamilySearch Developers Conference 2014

Client

Page 9: Hypermedia In Practice - FamilySearch Developers Conference 2014

WWW

Page 10: Hypermedia In Practice - FamilySearch Developers Conference 2014

API Server

Page 11: Hypermedia In Practice - FamilySearch Developers Conference 2014

ResourceStore

Page 12: Hypermedia In Practice - FamilySearch Developers Conference 2014

URI

Page 13: Hypermedia In Practice - FamilySearch Developers Conference 2014

HTTP“GET /path/to/resource”

Page 14: Hypermedia In Practice - FamilySearch Developers Conference 2014

Resource

HTTP“GET /path/to/resource”

Page 15: Hypermedia In Practice - FamilySearch Developers Conference 2014

“OK"

ResourceRepresentation

<resource> …</resource>

{ “resource” : …}

Page 16: Hypermedia In Practice - FamilySearch Developers Conference 2014

These change.Frequently.

Page 17: Hypermedia In Practice - FamilySearch Developers Conference 2014

What If...

Page 18: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

{ … “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

{ … “gender” : { … }, “names” : [ … ], “facts” : [ … ], …}

What If...

Page 19: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

{“names”:[ … ],“facts”:[ … ],“gender”:{ … },…}

{ “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

<person><name>...</name><fact>...</fact><gender>...</gender></person>

<person> <name>...</name> <fact>...</fact> <gender>...</gender></person>

What If...

Page 20: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an XML representationchanges namespace prefixes?

<person xmlns=”...”> <name>...</name> <fact>...</fact> <gender>...</gender></person>

<gx:person xmlns:gx=”...”> <gx:name>...</gx:name> <gx:fact>...</gx:fact> <gx:gender>...</gx:gender></gx:person>

What If...

Page 21: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a caching policy changes?What If...

Page 22: Hypermedia In Practice - FamilySearch Developers Conference 2014

...new resources become available?

Available Resources● Person● Relationship● Person Search● ...

Available Resources● Person● Relationship● Person Search● Records● Record● Record Search● ...

What If...

Page 23: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an API changes domains?

https://familysearch.org/platform

https://familysearch.io/platform

What If...

Page 24: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?

/platform/tree/persons/12345/matches

/platform/tree/persons/12345/duplicates

What If...

Page 25: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

Person

/person/12345

PersonNotes

/person/12345/notes PersonSource

References

/person/12345/sources

Person+

Notes+

Source References

/person/12345

What If...

Page 26: Hypermedia In Practice - FamilySearch Developers Conference 2014

What If...● ...property order changes?● ...some whitespace is added?● ...XML changes namespace prefixes?● ...caching policy changes?● ...new resources are available?● ...a resource moves?● ...subresources are included in their

"parent" resource?

Page 27: Hypermedia In Practice - FamilySearch Developers Conference 2014

● ...property order changes?● ...some whitespace is added?● ...XML changes namespace prefixes?● ...caching policy changes?● ...new resources are available?● ...a resource moves?● ...subresources are included in their

"parent" resource?

What If...

Page 28: Hypermedia In Practice - FamilySearch Developers Conference 2014
Page 29: Hypermedia In Practice - FamilySearch Developers Conference 2014

Speaks HTTP fluently.

Understands the URI.

Uses links to get stuffdone.

Page 30: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

<html> <head> … <script src=”...”></script> … </head> <body> … </body></html>

What If...

Page 31: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

<html> <head> … <script src=”...”></script> … </head> <body> … </body></html>

What If...

<html> <head> … </head> <body> … <script src=”...”></script> … </body></html>

Page 32: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

<html> <head> … <script src=”...”></script> … </head> <body> … </body></html>

What If...

<html> <head> … </head> <body> … <script src=”...”></script> … </body></html>

HTML says...

“OK”

Page 33: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

{ … “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

{ … “gender” : { … }, “names” : [ … ], “facts” : [ … ], …}

What If...

Page 34: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationchanges its property order?

{ … “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

{ … “gender” : { … }, “names” : [ … ], “facts” : [ … ], …}

What If...

GEDCOM X says...

“OK”

Page 35: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

What If...

<html><head>…</head><body>…</body></html>

Page 36: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

What If...

<html><head>…</head><body>…</body></html>

<html> <head> … </head> <body> … </body></html>

Page 37: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

What If...

<html><head>…</head><body>…</body></html>

<html> <head> … </head> <body> … </body></html>

HTML says...

“OK”

Page 38: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

{“names”:[ … ],“facts”:[ … ],“gender”:{ … },…}

{ “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

<person><name>...</name><fact>...</fact><gender>...</gender></person>

<person> <name>...</name> <fact>...</fact> <gender>...</gender></person>

What If...

Page 39: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

{ “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

<person><name>...</name><fact>...</fact><gender>...</gender></person>

<person> <name>...</name> <fact>...</fact> <gender>...</gender></person>

What If...

{“names”:[ … ],“facts”:[ … ],“gender”:{ … },…}

JSON says...

“OK”

Page 40: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource representationadds some whitespace?

{ “names” : [ … ], “facts” : [ … ], “gender” : { … }, …}

<person><name>...</name><fact>...</fact><gender>...</gender></person>

<person> <name>...</name> <fact>...</fact> <gender>...</gender></person>

What If...

{“names”:[ … ],“facts”:[ … ],“gender”:{ … },…}

XML says...

“OK”

Page 41: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an XML representationchanges namespace prefixes?

<person xmlns=”...”> <name>...</name> <fact>...</fact> <gender>...</gender></person>

<gx:person xmlns:gx=”...”> <gx:name>...</gx:name> <gx:fact>...</gx:fact> <gx:gender>...</gx:gender></gx:person>

What If...

Page 42: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an XML representationchanges namespace prefixes?

<person xmlns=”...”> <name>...</name> <fact>...</fact> <gender>...</gender></person>

<gx:person xmlns:gx=”...”> <gx:name>...</gx:name> <gx:fact>...</gx:fact> <gx:gender>...</gx:gender></gx:person>

What If...XML says...

“OK”

Page 43: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a caching policy changes?What If...

HTTP/1.1 200 OK…Cache-Control: no-store…

HTTP/1.1 200 OK…Cache-Control: no-store…

Page 44: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a caching policy changes?What If...

HTTP/1.1 200 OK…Cache-Control: no-store…

HTTP/1.1 200 OK…Cache-Control: no-store…

HTTP/1.1 200 OK…Cache-Control: public, max-age=3600ETag: 1234567890…

Page 45: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a caching policy changes?What If...

HTTP/1.1 200 OK…Cache-Control: no-store…

HTTP/1.1 200 OK…Cache-Control: no-store…

HTTP/1.1 200 OK…Cache-Control: public, max-age=3600ETag: 1234567890…

HTTP says...

“OK”

Page 46: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a caching policy changes?What If...

Page 47: Hypermedia In Practice - FamilySearch Developers Conference 2014

...new resources become available?What If...

Page 48: Hypermedia In Practice - FamilySearch Developers Conference 2014

...new resources become available?What If...

Page 49: Hypermedia In Practice - FamilySearch Developers Conference 2014

...new resources become available?

Available Resources● Person● Relationship● Person Search● ...

Available Resources● Person● Relationship● Person Search● Records● Record● Record Search● ...

What If...

Page 50: Hypermedia In Practice - FamilySearch Developers Conference 2014

...new resources become available?

Available Resources● Person● Relationship● Person Search● ...

Available Resources● Person● Relationship● Person Search● Records● Record● Record Search● ...

What If...

Hypermedia

Page 51: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an API changes domains?What If...

Page 52: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an API changes domains?What If...

HTTP/1.1 301 Moved Permanently…Location: http://www.facebook.com/…

HTTP says...

“Moved”

Page 53: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an API changes domains?What If...

Page 54: Hypermedia In Practice - FamilySearch Developers Conference 2014

...an API changes domains?What If...

HTTP/1.1 301 Moved Permanently…Location: http://familysearch.io/…

Page 55: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?

/platform/tree/persons/12345/matches

/platform/tree/persons/12345/duplicates

What If...

Page 56: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<html> <body> … <form action=”/path/to/resource”> <input …/> </form> </body></html>

Page 57: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<html> <body> … <form action=”/path/to/resource”> <input …/> </form> </body></html>

<html> <body> … <form action=”/NEW/resource/PATH”> <input …/> </form> </body></html>

Page 58: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<html> <body> … <form action=”/path/to/resource”> <input …/> </form> </body></html>

<html> <body> … <form action=”/NEW/resource/PATH”> <input …/> </form> </body></html>

Hypermedia

Page 59: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<gedcomx> <person id=”12345”> … <link rel=”matches” href=”/persons/12345/matches”/> … </person></gedcomx>

Page 60: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<gedcomx> <person id=”12345”> … <link rel=”matches” href=”/persons/12345/matches”/> … </person></gedcomx>

<gedcomx> <person id=”12345”> … <link rel=”matches” href=”/persons/12345/duplicates”/> … </person></gedcomx>

Page 61: Hypermedia In Practice - FamilySearch Developers Conference 2014

...a resource moves?What If...

<gedcomx> <person id=”12345”> … <link rel=”matches” href=”/persons/12345/matches”/> … </person></gedcomx>

<gedcomx> <person id=”12345”> … <link rel=”matches” href=”/persons/12345/duplicates”/> … </person></gedcomx>

Hypermedia

Page 62: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

Person

/person/12345

PersonNotes

/person/12345/notes PersonSource

References

/person/12345/sources

Person+

Notes+

Source References

/person/12345

What If...

Page 63: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<html> <head> … <link rel=”stylesheet” href=”...”/> <script src=”...”></script> … </head> <body> … </body></html>

Page 64: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<html> <head> … <link rel=”stylesheet” href=”...”/> <script src=”...”></script> … </head> <body> … </body></html>

Embedded Links(Hypermedia)

Page 65: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<html> <head> … <link rel=”stylesheet” href=”...”/> <script src=”...”></script> … </head> <body> … </body></html>

<html> <head> … <style> … </style> <script> … </script> … </head> <body> … </body></html>

Page 66: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<html> <head> … <link rel=”stylesheet” href=”...”/> <script src=”...”></script> … </head> <body> … </body></html>

<html> <head> … <style> … </style> <script> … </script> … </head> <body> … </body></html>

HTML says...

“OK”

Page 67: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<gedcomx> <person id=”12345”> … <link rel=”source-references” href=”/persons/12345/sources”/> <link rel=”notes” href=”/persons/12345/notes”/> … </person></gedcomx>

Page 68: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<gedcomx> <person id=”12345”> … <link rel=”source-references” href=”/persons/12345/sources”/> <link rel=”notes” href=”/persons/12345/notes”/> … </person></gedcomx>

Embedded Links(Hypermedia)

Page 69: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<gedcomx> <person id=”12345”> … <link rel=”source-references” href=”/persons/12345/sources”/> <link rel=”notes” href=”/persons/12345/notes”/> … </person></gedcomx>

<gedcomx> <person id=”12345”> … <source description=”...”/> <note id=”...”> … </note> </person></gedcomx>

Page 70: Hypermedia In Practice - FamilySearch Developers Conference 2014

...subresources are includedin their “parent” resource?

What If...

<gedcomx> <person id=”12345”> … <link rel=”source-references” href=”/persons/12345/sources”/> <link rel=”notes” href=”/persons/12345/notes”/> … </person></gedcomx>

<gedcomx> <person id=”12345”> … <source description=”...”/> <note id=”...”> … </note> </person></gedcomx>

GEDCOM X says...

“OK”

Page 71: Hypermedia In Practice - FamilySearch Developers Conference 2014

So.

Page 72: Hypermedia In Practice - FamilySearch Developers Conference 2014

So.● Speak HTTP fluently.● Understand the URI.● Use links to get stuff done.

Page 73: Hypermedia In Practice - FamilySearch Developers Conference 2014

So.

Yeah. Like a screen scraper.

● Speak HTTP fluently.● Understand the URI.● Use links to get stuff done.

Page 74: Hypermedia In Practice - FamilySearch Developers Conference 2014

links

Page 75: Hypermedia In Practice - FamilySearch Developers Conference 2014

linksWhat do

look like?

Page 76: Hypermedia In Practice - FamilySearch Developers Conference 2014

linksWhat do

look like?Depends on the media type.

Page 77: Hypermedia In Practice - FamilySearch Developers Conference 2014

links

<a href=”...”/>

<link rel=”” href=””/>

<img src=”...”/>

<script src=”...”/>

Page 78: Hypermedia In Practice - FamilySearch Developers Conference 2014

linksWhy use

?So APIs can changewithout breaking clients.

Page 79: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...

Page 80: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...● Add a person to a collection.● Search a collection for persons.● Do a “full” person read.● Upload an artifact to a collection.● Edit the birth date of a person.● Read the list of photos on a person.● Delete a person.

Page 81: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Add a person to a collection.

● Read the collection.● Find the link to the “persons” of the

collection.● POST the new person to that URI.

Page 82: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Add a person to a collection.

● Read the collection.● Find the link to the “persons” of the

collection.● POST the new person to that URI.

Page 83: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Add a person to a collection.

GET /platform/collections/tree

<gedcomx xmlns=”http://gedcomx.org/v1”>

<collection> <title>FamilySearch Family Tree</title> ... <link rel=”persons” href=”/path/to/persons/resource”/> ... </collection>

</gedcomx>

Page 84: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Search a collection for persons.

● Read the collection.● Find the link to the “person-search” of the

collection.● Fill in the parameters and issue the

request.

Page 85: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...

GET /platform/collections/tree

<gedcomx xmlns=”http://gedcomx.org/v1”>

<collection> <title>FamilySearch Family Tree</title> ... <link rel=”person-search” template=”/path/to/person/search{?q}”/> ... </collection>

</gedcomx>

Search a collection for persons.

Page 86: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Do a “full” person read.

● Read the person.● For each “embedded” link, get the

resource and embed it.– Source references

– Discussion references

– Notes

– Relationships

Page 87: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Do a “full” person read.

<gedcomx xmlns=”http://gedcomx.org/v1”>

<person id=”P2”> <name>...</name> ... <link rel=”source-references” href=”/path/to/refs”/> <link rel=”notes” href=”/path/to/notes”/> <link rel=”child-relationships” href=”/path/to/rels”/> <link rel=”parent-relationships” href=”/path/to/rels”/> <link rel=”spouse-relationships” href=”/path/to/rels”/> ... </person>

</gedcomx>

Page 88: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Upload an artifact to a collection.

● Read the collection.● Find the link to the “artifacts”.● POST the artifact.

Page 89: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Upload an artifact to a collection.

GET /platform/collections/tree

<gedcomx xmlns=”http://gedcomx.org/v1”>

<collection> <title>FamilySearch Family Tree</title> ... <link rel=”artifacts” href=”/path/to/artifacts/resource”/> ... </collection>

</gedcomx>

Page 90: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Edit the birth date of a person.

● Read the person.● Find the link to the “conclusions” of the

person.● POST the changes.

Page 91: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Edit the birth date of a person.

<gedcomx xmlns=”http://gedcomx.org/v1”>

<person id=”P2”> <name>...</name> ... <link rel=”conclusions” href=”/path/to/conclusions”/> ... </person>

</gedcomx>

Page 92: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Read the list of photos on a person.

● Read the person.● Find the link to the “artifacts” of the

person.

Page 93: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Read the list of photos on a person.

<gedcomx xmlns=”http://gedcomx.org/v1”>

<person id=”P2”> <name>...</name> ... <link rel=”artifacts” href=”/path/to/artifacts”/> ... </person>

</gedcomx>

Page 94: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Delete a person.

● Read the person.● Find the “self” link.● Apply the DELETE.

Page 95: Hypermedia In Practice - FamilySearch Developers Conference 2014

How To...Delete a person.

<gedcomx xmlns=”http://gedcomx.org/v1”>

<person id=”P2”> <name>...</name> ... <link rel=”person” href=”/path/to/self”/> ... </person>

</gedcomx>

Page 96: Hypermedia In Practice - FamilySearch Developers Conference 2014

97© 2013 by Intellectual Reserve, Inc. All rights reserved.

Tools

Page 97: Hypermedia In Practice - FamilySearch Developers Conference 2014

98© 2013 by Intellectual Reserve, Inc. All rights reserved.

Do I have to?

Page 98: Hypermedia In Practice - FamilySearch Developers Conference 2014

99© 2013 by Intellectual Reserve, Inc. All rights reserved.

The Goal

Page 99: Hypermedia In Practice - FamilySearch Developers Conference 2014

100© 2013 by Intellectual Reserve, Inc. All rights reserved.

Better?● More Adaptable● More Robust● More Maintainable● Less Pain

Learn how to use hypermedia for better Web service APIs.

The Goal