27
Symfony2 & REST API Alexey Verkeenko Grossum [email protected]

Алексей Веркеенко "Symfony2 & REST API"

  • Upload
    fwdays

  • View
    1.521

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Алексей Веркеенко "Symfony2 & REST API"

Symfony2 & REST API

Alexey VerkeenkoGrossum

[email protected]

Page 2: Алексей Веркеенко "Symfony2 & REST API"

WHAT IS REST?

• REST (Representational State Transfer) is the software architectural style of the World Wide Web.

• REST gives a coordinated set of constraints to the design of components in a distributed hypermedia system that can lead to a higher-performing and more maintainable architecture.

Page 3: Алексей Веркеенко "Symfony2 & REST API"

A BIT OF REST HISTORY

• This technology became popular, when it was described in detail and presented by Roy Fielding in his doctoral dissertation called “Architectural Styles and the Design of Network-based Software Architectures” in 2000.

Page 4: Алексей Веркеенко "Symfony2 & REST API"

REST RULES

• The general rule stands that each unit of information is identified by URL – which means, in turn, that URL in essence is the primary key for the data unit.

• For example, third book on the bookshelf would have a view of /book/3, and 35th page of that book would be found here: /book/3/page/35.

Page 5: Алексей Веркеенко "Symfony2 & REST API"

FULL LIST OF REQUEST TYPES

• CONNECT

• DELETE

• GET

• HEAD

• OPTIONS

• PATCH

• POST

• PUT

• TRACE

Page 6: Алексей Веркеенко "Symfony2 & REST API"

RESPONSE CODES

• There are 38 response codes and you can see the list on the right:

Page 7: Алексей Веркеенко "Symfony2 & REST API"

API TRANSACTION EXAMPLE

• One transaction for this API would consist from following (minimum):• Request method, for example, GET

• Request path, for example, /object/list

• Request body, for example, form

• Response code, for example, 200 OK

• Response body, for example data in JSON format

Page 8: Алексей Веркеенко "Symfony2 & REST API"

REST EXCLUSIONS

• You cannot call a service a REST one, if:• It has single point of entry

• All requests are processed by POST method

• Response metadata is located in the body of the response and not in its header

• URL-addresses contain method names

Page 9: Алексей Веркеенко "Symfony2 & REST API"

REST ADVANTAGES

• The advantages of using REST in our projects:• All our resources have unique identifiers (URL), which

gives us an opportunity to logically formulate our requests and structure URLs available for requests

• Universality. You can use the same server response data to return back to XML or JSON for software processing or to wrap in a beautiful design for people to see.

• REST is a service that implements actual service-oriented architecture (SOA), an architectural pattern in computer software design in which application components provide services to other components via a communications protocol, typically over a network.

Page 10: Алексей Веркеенко "Symfony2 & REST API"

ERRORS GUIDELINES

• Basic error informing guidelines:• Use expected format

• Do not neglect available response types

• Show coherent messages

Page 11: Алексей Веркеенко "Symfony2 & REST API"

USAGE PROBLEMS

• Absence of mutual agreement and standardization

• Partial support of response headers

• Different clients’ behavior for the same response code

• Complex process of searching for errors during development

Page 12: Алексей Веркеенко "Symfony2 & REST API"

REQUEST OPTIONS

• Safe request – a request that does not change the application’s condition.

• Idempotent request – a request, the multiple execution of which equals to the single execution effect.

Page 13: Алексей Веркеенко "Symfony2 & REST API"

CORRELATION TABLE

• Table of HTTP method correlation for safe and idempotent requests:

HTTP-method Safe Idempotent

GET Yes Yes

HEAD Yes Yes

OPTIONS Yes Yes

PUT No Yes

DELETE No Yes

POST No No

Page 14: Алексей Веркеенко "Symfony2 & REST API"

MONITORING GOALS

• In order to choose correct instruments and concentrate your attention on really important issues, begin from identifying main needs you want to satisfy with monitoring.

Page 15: Алексей Веркеенко "Symfony2 & REST API"

TWO QUESTIONS TO ASK

• Ask yourself these two questions: • What do I want to find out?

• What am I planning to do based on this information?

Page 16: Алексей Веркеенко "Symfony2 & REST API"

WRAPPER CLASS

• Do not call API directly from your code. Instead, implement a wrapper class for it.

• This allows you to:• Cache results if the response speed is not high or there

is a big data exchange

• In case you need to incorporate changes, you only have to do it in one place

• Convenient implementation because you only deal with one class

• Mock-object’s easy realization to change our wrapper class during the work on testing

Page 17: Алексей Веркеенко "Symfony2 & REST API"

CREATION AND DOCUMENTATION

• You can find information here: • https://restunited.com/

• https://apimatic.io/

• http://swagger.io/

Page 18: Алексей Веркеенко "Symfony2 & REST API"

• Upload the bundle and its dependencies with the help of composer:

• Connect it to the application core:

IN THE BEGINNING…

Page 19: Алексей Веркеенко "Symfony2 & REST API"

CONFIGURATION PARAMETERS

Page 20: Алексей Веркеенко "Symfony2 & REST API"

POST ENTITY

Page 21: Алексей Веркеенко "Symfony2 & REST API"

FORM & CONTROLLER CREATION

Page 22: Алексей Веркеенко "Symfony2 & REST API"

FORM TYPE

Page 23: Алексей Веркеенко "Symfony2 & REST API"

POST CREATION

Page 24: Алексей Веркеенко "Symfony2 & REST API"

PUT ACTION

Page 25: Алексей Веркеенко "Symfony2 & REST API"

PATCH ACTION

Page 26: Алексей Веркеенко "Symfony2 & REST API"

DELETE ACTION

Page 27: Алексей Веркеенко "Symfony2 & REST API"

THANK YOU!

GOT QUESTIONS?

Alexey [email protected]

Grossum