Friday, March 8, 2019

What is Web API and why to use it?

ASP.NET Web API is a framework for building HTTP services that can be consumed by a broad range of clients including browsers, mobiles, iphone and tablets. It is very similar to ASP.NET MVC since it contains the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection.
ASP.NET Web API is introduced in .net framework 4.0   it is an extension of WCF REST API. In short, it is an replacement of WCF REST API. It can be used with ASP.NET MVC and other types of Web applications like ASP.NET WebForms. Also, Web API can be used as an stand-alone Web services application.
Why to choose Web API?
  1. If you need a Web Service and don’t need SOAP, then ASP.NET Web API is best choice.
  2. Used to build simple, non-SOAP-based HTTP Services on top of existing WCF message pipeline.
  3. Easy configuration unlike WCF REST service.
  4. Simple service creation with Web API. With WCF REST Services, service creation is difficult.
  5. Based on HTTP and so easy to define, expose and consume in a REST-ful way.
  6. Based on light weight RESTful architecture and good for devices which have limited bandwidth like smart phones.
  7. Open Source.

REST architectural Model

  • REST-REpresentational State Transfer
  • Resource-based
  • Representation
  • Six Constraints
    • Client-Server
    • Stateless
    • Cacheable
    • Uniform Interface
    • Layered System
    • Code-On-Demand
The REST architectural style describes six constraints. These constraints, applied to the architecture, were originally communicated by Roy Fielding in his doctoral dissertation and defines the basis of RESTful-style.

Resource-based

REST is resource based API because RESTful API is as below points
  • Things vs Actions
  • Nouns vs Verbs
  • Identified by URIs
  • Multiple URIs may refers to same resource as like CRUD operation on student resource using HTTP verbs.
  • Separate from their representations-resource may represent as per as request content type either JSON or XML etc.

Representation

  • How resources get manipulated
  • Part of the resource state-transferred between client and server
  • Typically JSON or XML
For Example-
Resource– Author (Arjun)
Service– Contact information about arjun (GET)
Representation-name,mobile, address as JSON or XML format

REST Constraints

REST architecture style describe six constraints for REST APIs.

1. Uniform Interface

The uniform interface constraint defines the interface between clients and servers. It simplifies and decouples the architecture, which enables each part to evolve independently.
For us this means
  • HTTP Verbs (GET,POST,PUT,DELETE)
  • URIs (resource name)
  • HTTP response (status and body)
The four guiding principles of the uniform interface are:
1.1) Identifying the resource – Each resource must have a specific and cohesive URI to be made available, for example, to bring a particular user registered on the site:
HTTP/1.1 GET http://arjun.com/user/arjun
1.2) Resource representation – Is how the resource will return to the client. This representation can be in HTML, XML, JSON, TXT, and more. Example of how it would be a simple return of the above call:
{
"name": "Arjun Walmiki",
"job": "REST API Developer",
"hobbies": ["blogging", "coding", "music"]
}
1.3) Self-descriptive Messages – Each message includes enough information to describe how to process the message. Beyond what we have seen so far, the passage of meta information is needed (metadata) in the request and response. Some of this information are: HTTP response code, Host, Content-Type etc. Taking as an example the same URI as we have just seen:
GET /#!/user/arjun HTTP/1.1
User-Agent: Chrome/37.0.2062.94
Accept: application/json
Host: arjun.com
1.4) Hypermedia as the Engine of Application State (HATEOAS)– Clients deliver state via body contents, query-string parameters, request headers and the requested URI (the resource name). Services deliver state to clients via body content, response codes, and response headers. This part is often overlooked when talking about REST. It returns all the necessary information in response so client knows how to navigate and have access to all application resources.
Just one example:
Request:
HTTP/1.1 POST http://arjun.com/arjun/posts
Response:

{
"post": {
"id": 42,
"title": "concepts REST",
"decription": "a little about the concept of architecture of REST",
"_links": [
{
"href": "/arjun/post/42",
"method": "GET",
"rel": "self"
},
{
"href": "/arjun/post/42",
"method": "DELETE",
"rel": "remove"
},
{
"href": "/arjun/post/42/comments",
"method": "GET",
"rel": "comments"
},
{
"href": "/arjun/posts/42/comments",
"method": "POST",
"rel": "new_comment"
},
{...}
]
},
"_links": {
"href": "/post",
"method": "GET",
"rel": "list"
}
}

2. Stateless

One client can send multiple requests to the server; however, each of them must be independent, that is, every request must contain all the necessary information so that the server can understand it and process it accordingly. In this case, the server must not hold any information about the client state. Any information status must stay on client – such as sessions.

3. Cacheable

Because many clients access the same server, and often requesting the same resources, it is necessary that these responses might be cached, avoiding unnecessary processing and significantly increasing performance.

4. Client-Server

The uniform interface separates clients from servers. This separation of concerns means that, for example, clients are not concerned with data storage, which remains internal to each server, so that the portability of client code is improved. Servers are not concerned with the user interface or user state, so that servers can be simpler and more scalable. Servers and clients may also be replaced and developed independently, as long as the interface is not altered.

5. Layered System

A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. Intermediary servers may improve system scalability by enabling load-balancing and by providing shared caches. Layers may also enforce security policies.

6. Code-On-Demand (Optional)

This condition allows the customer to run some code on demand, that is, extend part of server logic to the client, either through an applet or scripts. Thus, different customers may behave in specific ways even using exactly the same services provided by the server. As this item is not part of the architecture itself, it is considered optional. It can be used when performing some of the client-side service which is more efficient or faster.

Summary

We have looked into REST Architecture design and its six constraints about REST API. Here we notice if only optional constraint of REST architecture is code on demand. If a service violates any other constraint, it cannot strictly be referred to as RESTful.



Thank you ,
Arjun Walmiki

No comments:

Post a Comment