15
Delight Driven Development of REST APIs with RAML By Vijay Dhanakodi

Rest With Raml

Embed Size (px)

Citation preview

Delight Driven Development of REST APIs with RAML

By Vijay Dhanakodi

REST is also called as representational state transfer.The REST API lets us to interact with anything that can send an HTTP request.REST APIs are very easy to create and design and it supports various methods like GET, POST, PUT, DELETE etc to perform various types of operations.

In Mule, we can create and design a REST service various ways.But, with the new specification of RAML, it became quite a easy task to create, design and test our APIs as per our requirement.

Before we start, we will actually look into what RAML really is.

RAML which is also called as RESTful API Modeling Language, helps us to manage the whole api lifecycle starting from it’s design, develop to sharing.

It is basically build on top of YAML for describing RESTful APIs and provides all the information to describe an API.

Reference :- http://raml.org/

Advantage of RAML:-

• RAML lets us see what our API looks like we you design it with it’s easy to read plain text. • RAML lets us see what our API looks like we you design it with it’s easy to read plain text. • RAML lets us see what our API looks like we you design it with it’s easy to read plain text.

Reference :- http://raml.org Here in this slides, we will be seeing various ways of designing an API with different RAML specs.

RAML can be designed in API designer.

API Designer is a standalone/embeddable editor for RAML (RESTful API Modeling Language) written in JavaScript using Angular.JS. By default, the editor uses an in-browser filesystem stored in HTML5 Local storage.

API designer :- https://www.mulesoft.com/platform/api/api-designer

With this tool/editor, we can design our APIs and test it there itself using a mock service with the tool.

So, we need to log in into our account to start designing our APIs.https://anypoint.mulesoft.com/#/signin If we don’t have an account we can sign up and create one.

Once we sign in, we need to click APIs menu in the top right as follows:-

If we don’t have any API present there in our account, we can create a new API by clicking the button as below :-

Now, a dialog box will arrive and we will be filling it by giving our API name, Version name and a end point as follows :-

So, we will be creating our first RAML in the API designer as follows.:-

The initial RAML file will contain ROOT section, which will describe the basic information of the API.It includes API Title, API Version, BaseUri and all the BaseUri Parameters. #%RAML 0.8 is the first line in the RAML and it describe the RAML version

Now, we will add some more tags in the file like Protocols and Media Type

Protocols :- The protocols property MAY be used to specify the protocols that an API supports. If the protocols property is not specified, the protocol specified at the baseUri property is used.

Media Type :- The media types returned by API responses, and expected from API requests that accept a body, MAY be defaulted by specifying the mediaType property. This property is specified at the root level of the API definition.Both the above properties are optional though.

Resource :- Resources are identified by their relative URI, which MUST begin with a slash (/).

Let’s add a resource say /product . So, we will have a resource named product which will also act as a sub url of baseUri.displayName :- The displayName attribute provides a friendly name to the resource and can be used by documentation generation tools. This property is OPTIONAL.

description :- Each resource have a description that describe the resource. This property is OPTIONAL.

Now we can add methods for our resources.

methods :- methods are operations that are performed on a resource.There can be several methods applied on a resources such as :-

* GET* POST* PUT* DELETEand many more.We will be seeing GET performed on our resource.

Methods also contains the properties like displayName and description

As you can see here, we applied GET method on our resource /product

Though description is an option properties for methods, but it is RECOMMENDED that all API definition methods include the description property.

We can add our custom headers in our API

Headers :- The headers property is a map in which the key is the name of the header, and the value is itself a map specifying the header attributes. It contains properties like displayName, type, required, example etc.