Building a REST API with Scalaz
When we were building our cloud orchestration platform, we were looking into writing our API Gateway server in scala and strictly functional because we knew the API is going to be doing lot of heavy lifting and it will be constantly extended. We stumbled upon scalaz, a brilliant library for scala to write functional programs. It was indeed a pleasure for the whole team to write the API since then.
I will be talking more about how we designed and built the REST API that powers megam cloud platform(megam.io). Developers with scala experience will be able to take away on getting started with scalaz library.
Outline/structure of the Session
- Introduction to scala/playframework & design of our RESTful API
We will start with a quick introduction to scala-lang.org, and the high velocity webframework playframework.com. Then we go in to start the seed of our topic by providing insights on design of how our RESTful API is architected.
- What is scalaz ? What do we use from scalaz ?
One of the functional frameworks for Haskell refugees in Scala is scalaz (https://github.com/scalaz/scalaz). Hence we start with an introduction on what it is and then talk about the basic Functor, Applicative, Monad (map, flatMap). After setting the background we go in deeper on where we have leveraged scalaz and how. We have leveraged in the following areas
- Error handling using Validation
- Chaining computations using ValidationNels
- EitherT on aggregate error handing of a iterated computation
- IO wrapper
- State monad on implementing inmemory persistance.
- A walkthrough of an API call
We will talk about one API and its journey in the RESTful API server (https://github.com/megamsys/megam_gateway.git). We start with the HMAC API authentication approach as designed in a functional way and how it gets handled automatically during every API call using scala implicits.
- Transport layer for JSON
The data layer as responsible for storing data and its design philosophy will be discussed. This is essentially the business logic. The JSON that comes in gets parsed and stored in the NOSQL database Riak(docs.basho.com) after a type validated schema.
- Response Layer
Now that we have completed our data storage, its time to return stuff back to the client. We use a function appoach to collate errors and send back to the client.
We will show a demo of its workings..
At the end of the talk, listeners will be able to have a good understanding how to program in scala, using scalaz to write an elastic RESTful API.
People who'd want to learn scala and write functional code with scalaz library