38
Designing REST APIs with RAML(Part 1) By Anirban Sen Chowdhary

Designing rest with raml

Embed Size (px)

Citation preview

Page 1: Designing rest with raml

Designing REST APIs with RAML(Part 1)

By Anirban Sen Chowdhary

Page 2: Designing rest with raml

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.

Page 3: Designing rest with raml

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/

Page 4: Designing rest with raml

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.

Page 5: Designing rest with raml

How can we design RAML for our RESTAPIs ???

Page 6: Designing rest with raml

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.

Page 7: Designing rest with raml

Designing RAML for REST API

Page 8: Designing rest with raml

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.

Page 9: Designing rest with raml

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

Page 10: Designing rest with raml

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

Page 11: Designing rest with raml

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 :-

Page 12: Designing rest with raml

Designing RAML for REST API

Page 13: Designing rest with raml

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

Page 14: Designing rest with raml

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.

Page 15: Designing rest with raml

Adding Resources

Page 16: Designing rest with raml

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.

Page 17: Designing rest with raml

Adding Methods

Page 18: Designing rest with raml

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

Page 19: Designing rest with raml

Adding GET Methods

Page 20: Designing rest with raml

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.

Page 21: Designing rest with raml

Adding Headers

Page 22: Designing rest with raml

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.

Page 23: Designing rest with raml

Adding Query String

Page 24: Designing rest with raml

We can also add our query parameters in our API

queryParameters :- The queryParameters property is a map in which the key is the query parameter's name, and the value is itself a map specifying the query parameter's attributes. It contains properties like displayName, type, required, example, description etc.

Page 25: Designing rest with raml

Adding Response

Page 26: Designing rest with raml

We can different response based on status code :

Page 27: Designing rest with raml

Responses :- A resource methods may have one or more responses.

Responses may be described using the description property, and may include example attributes or schema properties.

Responses must be a map of one or more HTTP status codes, where each status code itself is a map that describes that status code.

In our example, we have mapped 2 responses for status code 200 and 404 respectively.So, these response will be generated as per the status code.

Page 28: Designing rest with raml

We can see at right side of our API designer, where preview of our API is displayed and the preview gets updated everytime we change or update our code.Here we can see the description, structure of our API and also the description of elements we have provided.

Page 29: Designing rest with raml

Testing our API

Page 30: Designing rest with raml

API designer platform provides an option to test our APIs that we build in it. In the left side we can see a button called Mocking Service, if we on that button, the API will be ready for a mock test. You can see in the code, that our baseUri has been commented and a new mock Uri is generated.

Page 31: Designing rest with raml

So, to test the API, we need to hit Try it button in the right side, that display our API graphically .:-

Page 32: Designing rest with raml

After hitting Try it button we can see protocols, custom headers and query parameter field that we need to fill before we hit the service. We can see it is already filled with example values.

Page 33: Designing rest with raml

We need to hit the GET button to hit the service that is designed with GET method:-

Page 34: Designing rest with raml

We can see that the response is generated with status code 200

Page 35: Designing rest with raml

We can click the save button and save our API design in API designer.

Page 36: Designing rest with raml

Conclusion…..

Page 37: Designing rest with raml

So we can see it is very very easy to design and create a RAML file for our RESTful api.The API designer helps us greatly in designing as well as testing the API we create.In the next part I will show to design our APIs with other methods and specifications.

So at the end I can only say that, let’s spread our knowledge and expand our Mule community.

Page 38: Designing rest with raml

Thank You