Simplicity is the ultimate sophistication Leonardo da Vinci, 1452



Modern serverless apps still require state and asynchronous longer-running processes...



Rapid prototyping and development

API | State | Integration


The essence of an API | function

The first step is designing a nice API for the services you need: decouple the API's form from the function and design the conceptual API. Let's take the classic example of a cart service...


$msg cart.get (customerId) : (cart)

$msg cart.addItem (customerId, sku, quantity) : (cart)

$msg cart.checkout (customerId, paymentConfirmation) : (cart)


We can add details like types later, but for now we have a simple cart API in the form of messages. The API will now insulate you from the external services you'll choose later for database, workflows etc. This API has an implicit REST form behind it and you can see that and also invoke it) if you click on the "REST" tab.


The state | form

The first thing needed is a state, somewhere to store this cart, so it survives html refreshes, can move from the client's desktop to the phone etc. Adding that functionality behind the API is very simple:


$when cart.get (customerId ?= "Joe") 
=> diesel.memdb.get (collection="cart", id=customerId)
=> $if (document is undefined) diesel.memdb.upsert (
       collection="cart", 
       document = {customerId : customerId, items : []}, 
       id=customerId)
=> (cart = document)

.todo make this idempotent

$when cart.addItem (customerId ?= "Joe", sku ?="123", quantity ?= 5) 
=> cart.get
=> (cart=cart+{items:{sku : "$sku", quantity : "$quantity"} })
=> diesel.memdb.upsert (collection="cart", document = cart, id=customerId)


A few DB services are hosted and available here or you can use one of many external state services, now hidden behind your API.


Mocking APIs and services

If you'd rather add a mock for the cart API you defined, it's also very easy:

$mock cart.addItem (customerId ?= "Joe", sku ?="123", quantity ?= 5) 
=> cart.get
=> (cart=cart+{items:{sku : "$sku", quantity : "$quantity"} })

$mock cart.get (customerId ?= "Joe") => (cart = {customerId : "Joe", items : []})





Integration and mediation

Integrating systems and composing their functionality...

$msg catalog.getPrice (sku)

$msg cart.addItem (customerId ?= "Joe", sku, quantity)

$msg billing.charge (customerId >= "Joe", amount)


APIs are often much less interesting than the stories they tell... or the stories you can create with them, i.e. systems, apps etc. The behaviour of these systems as a whole... that's where the value of the system is.



Testing microservices


$send say.hi (name = "Jane")
$expect (greeting contains "Jane")


How do we specify our expectations about the value? How do we communicate it, record it, share it, cooperate on it?



Use cases and user stories can turn into automated tests! (one that you can run and trace right now) (read more++)



Interested in simplifying microservices testing? ...


In the modern age, enterprises are transformed by microservices, APIs and reactive principles.

If you want to take a refreshing look at microservices requirements, acceptance and truly continuous testing, while fiddling with the architecture itself, start by creating a free account.






Was this useful?    

By: Razie | 2018-04-30 .. 2019-08-25 | Tags: admin , dsl


Viewed 306 times ( | History | Print ) this page.