Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

API Over HTTP

11,404 views

Published on

Published in: Technology

API Over HTTP

  1. over
  2. presentation http business logicproblem 1:presentation depends on the interactionflow, and mobile flows are reallydifferent
  3. presentation http business logic presentation httpproblem 2:conversation state shared between clientand server. conversation state onserver leads to “session abuse”
  4. presentation http business logic presentation httpproblem 2:conversation state shared between clientand server. conversation state onserver leads to “session abuse”
  5. presentation http business logic presentation httpproblem 2:conversation state shared between clientand server. conversation state onserver leads to “session abuse”
  6. n io presentation t http a s e business logic r t e a v t presentation n s http c oproblem 2:conversation state shared between clientand server. conversation state onserver leads to “session abuse”
  7. n io presentation t http aap s e pl business logic ic r t e a st at v t presentation at io n s http e n c oproblem 2: conversation state shared between client and server. conversation state on server leads to “session abuse”
  8. n io presentation t http aap s e pl business r t presentation http business logic presentation http e a logic ic st at v t presentation at io n s http e n c oproblem 2: (sessions) could make horizontal scalability harder than it should be
  9. n io presentation t http aap s e pl business logic ic r t e a st at v t presentation at io n s http e n c oproblem 2: (sessions) it’s very hard to migrate conversations from agents (mobile <-> desktop)
  10. logic business business business logic logic usage usage presentation presentation problem 3: http httpbusiness logic usage is duplicated
  11. business logic problem 3: business business logic logic (duplication) usage usage presentation presentation http httpworst when there’s an anemic domain model
  12. presentation presentation http business logic presentation presentation httpproblem 4: rich clientmore duplication (different languages),more difficult to separateresponsibilities (where is what?)
  13. presentation business http usage logic business logic presentation business http usage logicproblem 5: (testability)difficult to reach a particular point inthe conversation. presentation changesoften (smell: divergent change)
  14. presentation business logic http presentationadopted solution
  15. SmartphoneClient.prototype.next = function() { var self = this this.seq( this.purchase, this.transferIn, this.optIn, this.waitForCompletion ).exit(function(exit) { exit.callHandler(self.callbacks) })} var client = new SmartphoneClient(server, { onError: function(error) { app.pages.notify.error(error) }, onRoute: function(route) { app.goTo(route) }, onUrl: function(url) { location.href = url } })
  16. presentationbusiness logic http presentation clear separationfrom application and conversation state
  17. presentation business logic http presentation stateless: easy to scalehorizontally, conversations can be migrated/resumed
  18. $app->post("/purchase/:id/transfer-in/opt-in/pin", function($purchaseId) use($app) { ContentDelivery::on($app) ->process(function($request, $response) use($purchaseId, $app) { $purchase = Purchase::fromRequest($request, $purchaseId); $transferIn = $purchase->boundedTransferIn("pin-flow"); $transferIn->checkPin($request->params("pin")); $response->status(200); }) ->rescue(OnebipCoreNotSupportedActionInState, 409) ->rescue(OnebipPurchasePurchaseNotFound, 404) ->rescue(OnebipPurchaseTransferInNotFound, 404) ->rescue(OnebipPurchaseSpendingLimitReached, 403) ->rescue(OnebipPurchaseCheckPinFailed, 400) ->rescue(HttpUnauthenticatedRequest, 401) ->deliver(); }); look, no session, and the controller code is really small
  19. presentation business logic http presentationclear application interface
  20. $app->map("/purchases", ...$app->get("/purchase/:id", ...$app->post("/purchase/:id/login", ...$app->get("/purchase/:id/buyer", ...$app->post("/purchase/:id/transfers-in", ...$app->get("/purchase/:id/transfer-in", ...$app->put("/purchase/:id/transfer-in", ...$app->post("/purchase/:id/transfer-in/opt-in/pin", ...$app->put("/purchase/:id/transfer-in/mo", ... clear application interface
  21. presentationbusiness logic http presentation testable
  22. public function testShouldLoginWithARegisteredMsisdn(){ $self = $this; $msisdn = Baker::bake("Msisdn", "registered"); $purchase = Baker::bake("Purchase", "from Country", $msisdn->country()); $insertByUser = $msisdn->withoutNationalPrefixAsString(); $this->request("POST", "/purchase/{$purchase->id()}/login") ->withBody(array("msisdn" => $insertByUser)) ->run(function($response) use ($self) { $self->assertEquals(201, $response->status()); });} testable
  23. business logic http presentation what about presentationold phones?
  24. businessnow logic http presentation http presentation presentation
  25. namespace HttpFeaturePhone{ class Client { public function next() { try { $this->purchase(); $this->transferIn(); $this->optIn(); $this->waitForCompletion(); } catch (ClientExit $exit) { $exit->callHandler($this->callbacks); } catch (Exception $e) { $exit = new ClientExit("error", $e->getMessage()); $exit->callHandler($this->callbacks); } } ... }} $client = new HttpFeaturePhoneClient($server, array( "onError" => function($error) use ($app) { $app->flash($error); $app->redirectTo($app->urlFor("error-ui")); }, "onRoute" => function($route) use ($app) { $app->renderOrRedirectTo($route); }, "onUrl" => function($url) use ($app) { $app->redirectTo($url); } ));
  26. presentation http business logic http presentation presentationtomorrow...
  27. questions?

×