API Over HTTP

Post on 15-Jan-2015

8.107 views 6 download

Tags:

description

 

Transcript of API Over HTTP

over

problem 1:

businesslogic

presentat

ion

http

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

businesslogic

presentat

ion

http

presentat

ion

http

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

businesslogic

presentat

ion

http

presentat

ion

http

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

businesslogic

presentat

ion

http

presentat

ion

http

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

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

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

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

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

problem 3:business logic usage is duplicated

businesslogic

presentat

ion

http

presentat

ion

http

business

logic

usag

e

business

logic

usag

e

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

businesslogic

presentat

ion

http

presentat

ion

http

presentation

presentation

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

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

business

logic

presentation

presentation

http

adopted solution

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

business

logic

presentation

presentation

http

clear separationfrom application and conversation state

business

logic

presentation

presentation

http

stateless: easy to scale horizontally, conversations

can be migrated/resumed

$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

business

logic

presentation

presentation

http

clear application interface

$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

business

logic

presentation

presentation

http

testable

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

business

logic presentation

presentation

http

whatabout

old phones?

business

logic presentation

presentation

http

presentat

ion

http

now

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

business

logic presentation

presentation

http

presentat

ion

http

tomorrow...

questions?