07 restful webservices design

12
Restful webservices design Ahmed Elbassel Email: [email protected] Skype: ahmed_elbassel

Transcript of 07 restful webservices design

Restful webservices designAhmed Elbassel

Email: [email protected]: ahmed_elbassel

Restful Webservices

- History

- What is webservices?

- Resource-Oriented Architecture

- HTTP methods

- Restful API design

- References

History

- Before 1999, people used to use SOAP to integrate APIs.

- In 2000, REST was defined by Roy Fielding in his PhD dissertation "Architectural Styles and the Design of Network-based Software Architectures"

History

- Properties:- Scalability: to support large numbers of components and interactions among components.- Simplicity of a Uniform Interface- Modifiability of components to meet changing needs (even while the application is running)- Portability of components by moving program code with the data

- Constraints:- Client-Server- Stateless: Each request from any client contains all the information necessary to service the

request- Cacheable: Responses must, implicitly or explicitly, define themselves as cacheable, or not,

to avoid unsuitable response.- Layered System: A client cannot ordinarily tell whether it is connected directly to the end

server, or to an intermediary along the way.

What is Restful Webservices?

- Representational state transfer (REST) or RESTful Web services are one way of providing interoperability between computer systems on the Internet.

- Fielding used REST to design HTTP 1.1

Resource-Oriented Architecture

- In software engineering, a resource-oriented architecture (ROA) is a style of software architecture and programming paradigm for designing and developing software in the form of resources with "RESTful" interfaces.

HTTP methods

- HTTP implements REST, and we’re using HTTP to provide our restful webservices.

HTTP Method Operation Note

POST Create It creates a new instance, it returns 201 code

PUT Update/Replace It updates or replaces the entire instance, it returns 200.

GET Read It reads from the server, it returns http code 200

PATCH Update/Modify It updates specific thing inside the instance, it returns 200

DELETE Remove It removes an instance, it returns 200

Restful API design

- It’s all about resources, REST tries to make every resource is a unique location.

- URI: is used to locate the resource.

- To get tulip1 image in hard drive: /images/tulip1.jpg

Restful API design

Suppose we have the following Object:

User

Name: StringSSN: StringJob: Stringaddresses:

Address

Street: StringBuilding: StringApartment: String

Restful API design - Moving from Verbs to Nouns

- Old API: getUser.- Verb (get) + resource name(User)

- Old API: createUser.- Verb(create) + resource name(User)

- Old API: deleteUser.- Verb(delete) + resource name(User)

- Old API: updateUser.- Verb(update) + resource name(user)

- HTTP provides the method name, your turn to provide the resource name:- GET: /User/userId- POST: /User- DELETE: /User/userId- PUT: /User/userId

Restful API design - More

- Old API: getUserAddress.- Verb (get) + resource name(UserAddress)

- Old API: createUserAddress.- Verb(create) + resource name(UserAddress)

- Old API: deleteUserAddress.- Verb(delete) + resource name(UserAddress)

- Old API: updateUserAddress.- Verb(update) + resource name(userAccont)

- HTTP provides the method name, your turn to provide the resource name:- GET: /User/userId/addresses- POST: /user/userId/addresses- DELETE: /user/userId/adresses/id- PUT: /User/userId/adresses

References

https://msdn.microsoft.com/en-us/library/dd203052.aspx

https://en.wikipedia.org/wiki/Resource-oriented_architecture

https://en.wikipedia.org/wiki/Representational_state_transfer