Iain Hull

A Simple REST DSL Part 6

A big thank you to everyone who attended my talk at ScalaDays

A big thank you to everyone who attended my talk at ScalaDays. I really enjoyed all your excellent feedback. Here is a blog I was working on before hand. Before talking about testing I would like to cover some changes I made to extractors and assertions. Previously extractors were simple functions, this is an efficient implementation but the test failures result in generic exceptions and the only clue to the cause is a stack trace.

A Simple REST DSL Part 5

Today I want to talk about how I structure my DSL code base.

I haven’t had a chance to write about my REST DSL in ages. So today I want to talk about how I structure my DSL code base. First all functionality is implemented in a simple API. This is a collection of classes and methods defined in the Api object. This has a lot of advantages The full functionality is available to users who do not want to use the DSL. The full functionality can be tested independently of the DSL.

A Simple REST DSL Part 4

Providing a fluid syntax for common request properties.

At the end of part 3 we had a Domain Specific Language (DLS) that could neatly express a complete REST use case. It fully described the requests and their expected responses. val Jason: Person = ??? val personJson = Json.stringify(Jason) val EmptyList = List[Person]() RequestBuilder() url "http://api.rest.org/person" apply { implicit rb => GET asserting (statusCode is Status.OK, jsonBodyAsList[Person] is EmptyList) val id = POST body personJson asserting (statusCode is Status.Created) returning (header("X-Person-Id")) GET / id asserting (statusCode is Status.

A Simple REST DSL Part 3

Processing http responses.

In part 2, I showed how to describe and execute REST requests with a simple descriptive syntax, but it completely ignored processing the response. This should be as simple as making the request. First let’s simplify access to the components of the response. You can create a returning method to execute the Request and extract one or more values from the Response. For example: {% highlight scala %} val personJson = “”"{ “name”: “Jason” }""" RequestBuilder() url “http://api.

A Simple REST DSL Part 2

Using a builder to construct a DSL.

In part 1, I showed the builder pattern can improve code readability and composibility of REST requests. Now, let’s discover how the builder can be used as the basis for a simple DSL. First you need to decide what words or phrases to use to bootstrap the request DSL. Do this by describing some sample requests in simple English and looking for patterns, for example: Get from url http://api.rest.org/person/ Post personJson to url http://api.