Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building an API

Building an API

Following the process of a request in, around, and back out of an example API learning best practices along the way.

Steven Wade Jr

June 19, 2014
Tweet

More Decks by Steven Wade Jr

Other Decks in Technology

Transcript

  1. What We’ll Cover Following the Request to Response path: •

    Resources • RESTful Routing • Input Types • Response Formats • Response Codes
  2. Resource Naming Think of the base form of each “entity”

    /tabby-cats (not preferred) /cats (preferred)
  3. Resource Naming Think of the base form of each “entity”

    /tabby-cats (not preferred) /cats?type=tabby (preferred)
  4. RESTful Routing • CONNECT • DELETE • GET • HEAD

    • OPTIONS • PATCH • POST • PUT • TRACE
  5. RESTful Routing • CONNECT • DELETE • GET • HEAD

    • OPTIONS • PATCH • POST • PUT • TRACE
  6. RESTful Routing GET /organizations - List Organizations GET /organizations/{id} -

    Show specific Organization POST /organizations - Create Organization POST /organizations/{id} - Update Organization (partial) PUT /organizations/{id} - Update Organization (full/overwrite) DELETE /organizations/{id} - Delete specific Organization
  7. <?php Route::group(['prefix' => 'organizations' ], function() { Route::get('/', 'OrganizationsController@showList' );

    Route::get('/{id}', 'OrganizationsController@show' ); Route::post('/', 'OrganizationsController@create' ); Route::post('/{id}', 'OrganizationsController@update' ); Route::put('/{id}', 'OrganizationsController@overwrite' ); Route::delete('/{id}', 'OrganizationsController@delete' ); });
  8. Input: Types, Methods, & Formats 2 main methods of sending

    information: • Query parameters • Request body
  9. Input: Types, Methods, & Formats Query parameters are good for

    READ requests (GET) • /people?per_page=30 When sending data (POST, PUT), a standard structure is best. • JSON in the request body
  10. Input: Types, Methods, & Formats Why not just $_GET and

    $_POST? PHP is weird. • $_GET & $_POST have nothing to do with the HTTP method. • $_GET reads the query string from the URL • $_POST reads a query string from the request body IF the header “application/x-www-form-urlencoded” is present • $_POST - everything is a string
  11. Input: Types, Methods, & Formats string(67) "organization%5Bname%5D=UpstatePHP&organization%5Bis_meeting% 5D=true" array(1) {

    ["organization" ]=> array(2) { ["name"]=> string(10) "UpstatePHP" ["is_meeting"]=> string(4) "true" } }
  12. Input: Types, Methods, & Formats Sent as JSON: { "organization"

    : { "name": "UpstatePHP", "is_meeting": true } }
  13. Input: Types, Methods, & Formats array(1) { ["organization" ]=> array(2)

    { ["name"]=> string(10) "UpstatePHP" ["is_meeting"]=> bool(true) } }
  14. Response Formats What should I choose? How do I choose?

    • Pick one and go with it… or • Let the user choose ◦ Header - Accept: application/json ◦ Extension - /organizations.json
  15. Response Codes 100s - Informational 200s - Success 300s -

    Redirect 400s - Client Error 500s - Server Error
  16. Response Codes 200 - OK 201 - Created 202 -

    Accepted 204 - No Content 400 - Bad Request 401 - Unauthorized 403 - Forbidden 404 - Not Found 500 - Internal Server Error
  17. Custom Error Codes HTTP 400 • Validation errors • No

    activation code • Expired activation code • User already exists
  18. Custom Error Codes Provide sanity where chaos reigns Custom codes

    allow a program to know specifically what type of error occurred Twitter does this well - https://dev.twitter.com/docs/error- codes-responses
  19. { "id":81, "name":"UpstatePHP", "description":"Hey! That's us!" , "url":"http://upstatephp.com" , "meta":null,

    "created_at":"2014-06-18 03:05: 43", "updated_at":"2014-06-18 03:05: 43" } { "id":81, "name":"UpstatePHP", "description":"Hey! That's us!" , "website":"http://upstatephp.com" , "meta":null, "created_at":"2014-06-18 03:05: 43", "updated_at":"2014-06-18 03:05:43" } • Schema changes (“url” -> “website”) can break a Client using our API • Exposes our table structure (BAD!!)
  20. $transformed = array( 'id' => (int) $organization ->id, 'name' =>

    $organization ->name, 'description' => $organization - >description, 'url' => $organization ->website ); return Response::json($transformed); Benefits: • No more breaking changes • Type casting
  21. What if we could combine: • Transformers • Resources •

    Meta Data • Pagination and • Nested Resources