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

API Driven Development

API Driven Development

Presented at PHP North East 2013 in Newcastle, UK

Alex Bilbie

March 19, 2013
Tweet

More Decks by Alex Bilbie

Other Decks in Technology

Transcript

  1. Ad-hoc sharing has to change with the business (which can

    lead to more errors) Tuesday, 19 March 13
  2. There will be no other form of inter-process communication allowed:

    no direct linking, no direct reads of another team’s data store, no shared-memory model, no back-doors whatsoever. The only communication allowed is via service interface calls over the network. Tuesday, 19 March 13
  3. All service interfaces, without exception, must be designed from the

    ground up to be externalizable. That is to say, the team must plan and design to be able to expose the interface to developers in the outside world. No exceptions. Tuesday, 19 March 13
  4. Anyone who doesn’t do this will be fired...have a nice

    day” - Jeff Bezos, Amazon CEO Tuesday, 19 March 13
  5. Amazon once just sold books. Now they make billions every

    year selling infrastructure as a service Tuesday, 19 March 13
  6. They got there because they learnt how to effectively share

    data internally and externally Tuesday, 19 March 13
  7. Facade pattern Used to present an easier or simpler interface

    to an underlying implementation concept Tuesday, 19 March 13
  8. ALL THE DATA student records library items employee details research

    facilities energy usage events financial timetables journals inventory potential students Tuesday, 19 March 13
  9. 100+ databases Sybase MySQL SQL Server Excel spreadsheets MS Access

    MySQL SQL Server SQL Server SQL Server SQL Server SQL Server SQL Server SQL Server SharePoint SQL Serve SQL MySQL MySQL My Excel spreadsheets SQL Server SQL Server ver Tuesday, 19 March 13
  10. Today’s agenda •What is an API? •How can APIs help

    me? •How can my APIs help others? •How can others’ APIs help me? •How can others’ APIs help me help others? •How can my APIs help me help others? Tuesday, 19 March 13
  11. I have a web app that allows businesses to manage

    employee data Tuesday, 19 March 13
  12. My customers ask me for a mobile version, so I

    make iOS, Android and Blackberry apps Tuesday, 19 March 13
  13. Later on I want to fix something. I also want

    to change the underlying data source. Tuesday, 19 March 13
  14. Every time I want to make a change: 10 Want

    to change something 20 Change it 30 Update web app 40 Update iOS app 50 Update Android app 60 Update Blackberry app 70 goto 10 Tuesday, 19 March 13
  15. Every time I want to make a change: 10 Want

    to change something 20 Change it 30 Update web app 40 Update iOS app 50 Update Android app 60 Update Blackberry app 70 goto 10 Tuesday, 19 March 13
  16. Every time I want to make a change: 10 Want

    to change something 20 Change it 30 Test web app 40 Test iOS app 50 Test Android app 60 Test Blackberry app 70 goto 10 Tuesday, 19 March 13
  17. Assuming your API doesn’t change it’s response you can easily

    change your underlying business processes as you please Tuesday, 19 March 13
  18. Early public web APIs •Salesforce (February 2000) •Access to data

    •eBay (November 2000) •Mass uploading of listings •Amazon (~2002) •Access to data Tuesday, 19 March 13
  19. Questions you should be asking before writing a line of

    code: •What functionality do my clients need? Tuesday, 19 March 13
  20. Questions you should be asking before writing a line of

    code: •What functionality do my clients need? •What are appropriate responses? Tuesday, 19 March 13
  21. Questions you should be asking before writing a line of

    code: •What functionality do my clients need? •What are appropriate responses? •What implications does this have for (any?) existing clients? Tuesday, 19 March 13
  22. Questions you should be asking before writing a line of

    code: •What functionality do my clients need? •What are appropriate responses? •What implications does this have for (any?) existing clients? •Has anyone else made APIs like this? Can I learn anything from them? Tuesday, 19 March 13
  23. GET something PUT something new POST an update to something

    DELETE something PATCH Tuesday, 19 March 13
  24. GET something PUT something new POST an update to something

    DELETE something PATCH something Tuesday, 19 March 13
  25. use Swagger\Annotations as SWG; /** * @SWG\Operation( * httpMethod="GET", summary="Find

    pet by ID", notes="Returns a pet based on ID", * responseClass="Pet", nickname="getPetById" * ) */ function get_pet($id) { ! ... } Tuesday, 19 March 13
  26. API web server Database server New stack Web App Memcache

    server Solr server Mobile App Queue server Tuesday, 19 March 13
  27. 3rd party clients should never ever ever be allowed near

    your users’ credentials Tuesday, 19 March 13
  28. github.com/lncd/oauth2 // Include the storage models include 'model_scope.php'; include 'model_session.php';

    // Initiate the Request handler $request = new \OAuth2\Util\Request(); // Initiate the auth server with the models $server = new \OAuth2\ResourceServer(new SessionModel, new ScopeModel); Tuesday, 19 March 13
  29. $checkToken = function () use ($server) { return function() use

    ($server) { // Test for token existance and validity try { $server->isValid(); } // The access token is missing or invalid... catch (\OAuth2\Exception\InvalidAccessTokenException $e) { $app = \Slim\Slim::getInstance(); $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->status(403); $res->body(json_encode(array( 'error' => $e->getMessage() ))); } }; }; Tuesday, 19 March 13
  30. $app->get('/user/:id', $checkToken(), function ($id) use ($server, $app) { $user_model =

    new UserModel(); $user = $user_model->getUser($id); // Basic response $response = array( 'error' => null, 'result' => array( 'user_id' => $user['id'], 'firstname' => $user['firstname'], 'lastname' => $user['lastname'] ) ); // If the acess token has the "user.contact" access token include // an email address and phone number if ($server->hasScope('user.contact')) { $response['result']['email'] = $user['email']; $response['result']['phone'] = $user['phone']; } // Respond $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->body(json_encode($response)); }); Tuesday, 19 March 13
  31. Authorisation and resource server github.com/lncd/oauth2 OAuth 2 client code (nearly

    finished) github.com/lncd/oauth2-client Tuesday, 19 March 13
  32. Github developer.github.com (see also: How Github use Github to build

    Github) bit.ly/github-use-github Tuesday, 19 March 13
  33. PREFIX abc: <http://example.com/exampleOntology#> SELECT ?capital ?country WHERE { ?x abc:cityname

    ?capital ; abc:isCapitalOf ?y . ?y abc:countryname ?country ; abc:isInContinent abc:Africa . } What are all the country capitals in Africa? Tuesday, 19 March 13