Simplest RAML file (a.k.a. a Webserver & client in 10 lines)

What's the simplest RAML file that does something useful?

Prerequisites

     First of all, let's make sure we have the python-raml code generator installed, as well as requests, a popular python http client library.

npm install raml-python -g  
pip install requests  

Generating the Package

     Ok, this may not be the *simplest possible* RAML file, but it's still pretty small.

simple.raml:
#%RAML 0.8
title: simple API  
/check/in:
  get:
    queryParameters:
      name:
      lat : {type: number}
      long: {type: number}

     Next, let's add in implementation code for the server side function(s). Note how this seems more like native python than http calls.

simple/rpc_impl:
def rpc_check_in_get(name, lat=None, long=None):  
    return ["Hello, %s@(%s,%s)!" % (name, lat, long),"\n"]

     Generate the rest of the package:

raml-python-generator simple.raml -o simple  

     Now you can start importing stuff:

>>> import simple
>>> print simple.raml_version
'8'  
>>> print simple.parser_version
'1.4'  
>>>

Running the code

     Run the server (in one window):

python -um simple.server_wgsi

     In another window, we'll run this file:

# run_client.py
from simple.rpc_requests import RequestsAPI

api = RequestsAPI()  
api.prefixUri = 'http://localhost:8080'  
x = api.rpc_hello_world_get()  
x = api.rpc_check_in_get("1",2.1,4,3)  
print 'x', x.status_code  
print 'x', x.content  
print 'x', x.json()

     With the following command:

python run_client.py`

Congratulations

     You've just created a RAML file, then used that to create a working web server and a client library that look very much like native python. And it only took a total of 10 lines. ( 8 RAML + 2 python )

     Pretty cool, huh?

     Next up, we'll look into how this process works in more detail.