Working Towards RESTful Web Servies

462 views

Published on

Published in: Technology, Design
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
462
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Working Towards RESTful Web Servies

  1. 1. working towards RESTful Web Services Friday, November 8, 13
  2. 2. GET /presenter/jessica { “name” : “Jessica Zehavi”, “title” : “Software Engineer”, “job” : { “company” : “Clockwork Active Media”, “href” : “http://www.clockwork.net” }, “contacts” : { “email” : “jessica@clockwork.net”, “twitter” : “@yiska”, “github” : “github.com/yiska”, } } Friday, November 8, 13
  3. 3. API? Friday, November 8, 13
  4. 4. standards Friday, November 8, 13
  5. 5. ??? Friday, November 8, 13
  6. 6. working towards RESTful Web Services Friday, November 8, 13
  7. 7. turns out, it’s kind of hard Friday, November 8, 13
  8. 8. “A RESTful web service is an API implemented using HTTP and REST design principles.” - Wikipedia (edited by me) Friday, November 8, 13
  9. 9. Friday, November 8, 13
  10. 10. • Identification of resources Friday, November 8, 13
  11. 11. • Identification of resources • Manipulation of resources through these representations Friday, November 8, 13
  12. 12. • Identification of resources • Manipulation of resources through these representations • Self-descriptive messaging Friday, November 8, 13
  13. 13. • Identification of resources • Manipulation of resources through these representations • Self-descriptive messaging • Hypermedia as the engine of application state (HATEOAS) Friday, November 8, 13
  14. 14. Identification of Resources Friday, November 8, 13
  15. 15. Identification of Resources • Resources can be anything! Friday, November 8, 13
  16. 16. Identification of Resources • Resources can be anything! • URLs identify resources http://www.example.com/projects/ http://www.example.com/projects/1 http://www.example.com/deliverables/42 Friday, November 8, 13
  17. 17. Identification of Resources • Resources can be anything! • URLs identify resources http://www.example.com/projects/ http://www.example.com/projects/1 http://www.example.com/deliverables/42 • The representation describes the resource’s state Friday, November 8, 13
  18. 18. Manipulation of Resources through Representations Project Represented in JSON { } Friday, November 8, 13 "id" : 1, "name" : "Focus Media Group Project", "start_date" : "2013-01-01", "end_date" : "2014-06-31", "status" : "complete", "deliverables" : [ 1, 2, 3 ], "departments" : [ 3, 8, 14 ], "created_on" : "2013-09-20" "modified_on" : "2013-10-01"
  19. 19. Manipulation of Resources through Representations Project Represented in XML <?xml version="1.0" encoding="UTF-8"?> <project> <id>1</id> <name>Focus Media Group Project</name> <start_date>2013-01-01</start_date> <end_date>2013-01-01</end_date> <status>complete</status> <deliverables> <item>1</item> <item>2</item> <item>3</item> </deliverables> <departments> <item>1</item> <item>2</item> <item>3</item> </departments> <created_on>2013-09-20</created_on> <modified_on>2013-10-01</modified_on> </project> Friday, November 8, 13
  20. 20. Self Descriptive Messaging Operation HTTP Create INSERT POST Read SELECT GET Update UPDATE PUT Delete Friday, November 8, 13 Database DELETE DELETE
  21. 21. Create / POST Create a New Project Response POST /projects/ HTTP/1.1 HTTP/1.1 201 Created Content-Type: application/json { } "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ] Friday, November 8, 13 { } "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ], "created_on" : "2013-09-20", "modified_on" : "2013-09-20"
  22. 22. Read / GET Request a Resource Response GET /projects/1 HTTP/1.1 HTTP/1.1 200 OK Content-Type: application/json { "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ], "created_on" : "2013-09-20", "modified_on" : "2013-09-20" } Friday, November 8, 13
  23. 23. Update / PUT Update a Resource PUT /projects/1 HTTP/1.1 { } Friday, November 8, 13 "start_date" : "2013-11-01", "end_date" : "2014-02-28", Response HTTP/1.1 200 OK Content-Type: application/json { } "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-02-28", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ]
  24. 24. Delete / DELETE Delete a Resource Response DELETE /projects/1 HTTP/1.1 HTTP/1.1 204 No Content Friday, November 8, 13
  25. 25. Hypermedia as the Engine of Application State ? Friday, November 8, 13
  26. 26. HATEOAS! Friday, November 8, 13
  27. 27. You Know This “The principle of connectedness: each web page tells you how to get to the adjoining pages. The Web as a whole works on the principle of connectedness, which is better known as ‘hypermedia as the engine of application state,’ sometimes abbreviated HATEOAS.” - Leonard Richardson & Mike Amundsen RESTful Web APIs Friday, November 8, 13
  28. 28. Hypermedia! GET /projects/1 HTTP/1.1 Response HTTP/1.1 200 OK Content-Type: application/json { } Friday, November 8, 13 "href" : "http://www.example.com/projects/1", "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : { "href" : "http://www.example.com/deliverables", "items" : [ 4, 5, 6 ] }, "departments" : { "href" : "http://www.example.com/departments", "items" : [ 3, 8, 14 ] }, "created_on" : "2013-09-20", "modified_on" : "2013-09-20"
  29. 29. Common Mistakes • Ignore hypermedia • Using the wrong HTTP method • Return the wrong response codes Friday, November 8, 13
  30. 30. Friday, November 8, 13
  31. 31. NO Friday, November 8, 13
  32. 32. APIs for Public Consumption • Follow accepted standards/practices • Consume your own API • Versioning? Friday, November 8, 13
  33. 33. Implementation • Resources • Relationships • Endpoints Friday, November 8, 13
  34. 34. Project Viz Application • Tracks projects, resources and phases • Complex and evolving requirements • Wanted to host it themselves • Super fast deadline! Friday, November 8, 13
  35. 35. // Get all projects Route::get( 'projects', function( ) { $projects = Project::all( ); return Response::json( $projects ); }); // Get a project by id Route::get( 'projects/{id}', function( $id ) { $project = Project::find( $id ); if ( ! $project ) { App::abort( 404 ); } return Response::json( $project ); } )->where( 'id', 'd+' ); Friday, November 8, 13
  36. 36. Response HTTP/1.1 200 OK Content-Type: application/json { } Friday, November 8, 13 "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ], "created_on" : "2013-09-20", "modified_on" : "2013-09-20"
  37. 37. // Create new project Route::post( 'projects', function( ) { $project = new Project( Input::get( ) ); $project->validate( ); if ( ! $project->save() ) { App::abort( 500 ); } return Response::json( $project, 201 ); }); Friday, November 8, 13
  38. 38. Response HTTP/1.1 201 Created Content-Type: application/json { } Friday, November 8, 13 "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2014-01-31", "status" : "new", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ], "created_on" : "2013-09-20", "modified_on" : "2013-09-20"
  39. 39. // Update project by id Route::put('projects/{id}', function( $id ) { $project = Project::find( $id ); $project->fill( Input::get( ) ); $project->validate( ); if ( ! $project->save( ) ) { App::abort( 500 ); } return Response::json( $project ); })->where( 'id', 'd+' ); Friday, November 8, 13
  40. 40. Send { "end_date" : "2013-12-31", status : "active" } Response HTTP/1.1 200 OK Content-Type: application/json { } Friday, November 8, 13 "id" : 1, "name" : "Museum Site Redesign", "start_date" : "2013-10-01", "end_date" : "2013-12-31", "status" : "active", "deliverables" : [ 4, 5, 6 ], "departments" : [ 2, 8, 11 ], "created_on" : "2013-09-20", "modified_on" : "2013-10-26"
  41. 41. // Delete a project by id Route::delete( 'projects/{id}', function( $id ) { $project = Project::find( $id ); $project->delete( ); return Response::json( null, 204 ); })->where( 'id', 'd+' ); Response HTTP/1.1 204 No Content Friday, November 8, 13
  42. 42. Do These Things • Try to be RESTful • Consume your own APIs if possible • Write Unit Tests Friday, November 8, 13
  43. 43. Future! • Rate limiting • Authentication • Hypermedia • Versioning Friday, November 8, 13
  44. 44. ? Friday, November 8, 13
  45. 45. kthxbye Friday, November 8, 13

×