API Over HTTP

27
over

description

 

Transcript of API Over HTTP

Page 1: API Over HTTP

over

Page 2: API Over HTTP

problem 1:

businesslogic

presentat

ion

http

presentation depends on the interaction flow, and mobile flows are really different

Page 3: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2:conversation state shared between client and server. conversation state on server leads to “session abuse”

Page 4: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2:conversation state shared between client and server. conversation state on server leads to “session abuse”

Page 5: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2:conversation state shared between client and server. conversation state on server leads to “session abuse”

Page 6: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2:conversation state shared between client and server. conversation state on server leads to “session abuse”

conv

ersa

tion

stat

e

Page 7: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2:conversation state shared between client and server. conversation state on server leads to “session abuse”

conv

ersa

tion

stat

eapplication

state

Page 8: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2: (sessions)could make horizontal scalability harder than it should be

conv

ersa

tion

stat

eapplication

state

businesslogic

pres

enta

tion

http

pres

enta

tion

http

Page 9: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

problem 2: (sessions)it’s very hard to migrate conversations from agents (mobile <-> desktop)

conv

ersa

tion

stat

eapplication

state

Page 10: API Over HTTP

problem 3:business logic usage is duplicated

businesslogic

presentat

ion

http

presentat

ion

http

business

logic

usag

e

business

logic

usag

e

Page 11: API Over HTTP

problem 3: (duplication)worst when there’s an anemic domain model

business

logic

presentat

ion

http

business

logic

usag

e

presentat

ion

http

business

logic

usag

e

Page 12: API Over HTTP

businesslogic

presentat

ion

http

presentat

ion

http

presentation

presentation

problem 4: rich clientmore duplication (different languages), more difficult to separate responsibilities (where is what?)

Page 13: API Over HTTP

problem 5: (testability)difficult to reach a particular point in the conversation. presentation changes often (smell: divergent change)

businesslogic

presentat

ion

http

presentat

ion

http

business

logic

usag

e

business

logic

usag

e

Page 14: API Over HTTP

business

logic

presentation

presentation

http

adopted solution

Page 15: API Over HTTP

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 }})

Page 16: API Over HTTP

business

logic

presentation

presentation

http

clear separationfrom application and conversation state

Page 17: API Over HTTP

business

logic

presentation

presentation

http

stateless: easy to scale horizontally, conversations

can be migrated/resumed

Page 18: API Over HTTP

$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('\Onebip\Core\NotSupportedActionInState', 409) ->rescue('\Onebip\Purchase\PurchaseNotFound', 404) ->rescue('\Onebip\Purchase\TransferInNotFound', 404) ->rescue('\Onebip\Purchase\SpendingLimitReached', 403) ->rescue('\Onebip\Purchase\CheckPinFailed', 400) ->rescue('\Http\UnauthenticatedRequest', 401) ->deliver(); });

look, no session, and the controller code is really small

Page 19: API Over HTTP

business

logic

presentation

presentation

http

clear application interface

Page 20: API Over HTTP

$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

Page 21: API Over HTTP

business

logic

presentation

presentation

http

testable

Page 22: API Over HTTP

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

Page 23: API Over HTTP

business

logic presentation

presentation

http

whatabout

old phones?

Page 24: API Over HTTP

business

logic presentation

presentation

http

presentat

ion

http

now

Page 25: API Over HTTP

namespace Http\FeaturePhone{ 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 Http\FeaturePhone\Client($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); }));

Page 26: API Over HTTP

business

logic presentation

presentation

http

presentat

ion

http

tomorrow...

Page 27: API Over HTTP

questions?