34
Lionframe Rapid RESTful API development powered by

Lionframe - Rapid RESTful API development

Embed Size (px)

Citation preview

LionframeRapid RESTful API development

powered by

Michał Marcinkowski

Współzałożyciel LAKION

Miłośnik Symfony2 i Open Source

?Co to jest API

Application Programming

Interface

User Interface

User Interface

Software-to-software Interface

Amazon Client

My website Client

API

UI

UI

wykorzystanie API

• Współdzielenie treści i danych pomiędzy aplikacjami/systemami

• Budowanie aplikacji korzystających z serwisu

?Czy każda aplikacja zależy od jakiegoś API

?Czy każda aplikacja zależy od jakiegoś APITAK!

REST

CECHY REST• Bezstanowość

• Cache

• Jednolitość interfejsów

• Oparte na standardach (HTTP, URL, XML, JSON)

• Niezależność od platformy i języka

• Może być stosowane w obecności firewalli

• Prostota

Prostota RESTPOST / HTTP/1.1

Host: www.example.org Content-Type: application/soap+xml; charset=utf-8

Content-Length: 300

<?xml version="1.0"?> <soap:Envelope

xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/"> <m:GetUserDetails>

<m:UserID>12345</UserID> </m:GetUserDetails>

</soap:Body> </soap:Envelope>

GET /users/12345 HTTP/1.1 Host: www.example.org Accept: application/json

Dojrzałość REST

Richardson Maturity Model

Level 0 THE SWAMP OF POX

• Protokół jako tunel dla request-response (zazwyczaj HTTP)

• Jeden entry point URI

• Jeden rodzaj metody (w HTTP, zazwyczaj metoda POST)

Backend system

Mobile application

appointmentService

POST <openSlotRequest

POST <appointmentRequest

Level 0 THE SWAMP OF POX

Level 1 RESOURCES

• Wiele URIs • Każdy URI inny resource • Jeden rodzaj metody (POST)

Backend system

Mobile application

doctors/jdoe

POST <openSlotRequest

POST <appointmentRequest

slots/1234

Level 1 RESOURCES

Level 2 HTTP VERBS

• Używanie poprawnych metod

• Używanie poprawnych kodów odpowiedzi

Level 2 HTTP VERBS

• GET

• POST

• PUT

• PATCH

• DELETE

HTTP CODES• 2xx Success

• 200 OK

• 201 Created

• 204 No Content

• 3xx Redirection

• 301 Moved Permanently

• 5xx Server Error

• 500 Internal Server Error

• 503 Service Unavailable

• 4xx Client Error

• 400 Bad Request

• 401 Unauthorized

• 403 Forbidden

• 404 Not Found

• 405 Method Not Allowed

• 409 Conflict

Level 3 Hypermedia controls

• Używanie HATEOAS do odkrywania możliwości API

DEMO Time

/** * @Assert\NotBlank() */ private $firstName;

VALIDATIOn

SERIALIZACJA#src/Acme/DemoBundle/Resources/config/serializer/Entity.Player.yml Acme\DemoBundle\Entity\Player: exclusion_policy: ALL xml_root_name: player properties: id: expose: true type: integer xml_attribute: true firstName: expose: true type: string lastName: expose: true type: string

HATEOAS#src/Acme/DemoBundle/Resources/config/serializer/Entity.Player.yml Acme\DemoBundle\Entity\Player: exclusion_policy: ALL xml_root_name: player properties: id: expose: true type: integer xml_attribute: true firstName: …. relations: - rel: self href: route: acme_api_player_show parameters: id: expr(object.getId())

Z czego składa się LIONframe

Sylius Resource Bundle

• Domyślny kontroler

• Domyślny routing

• Domyślne repository z paginacją

• Aliasy do serwisów dla managerów, repository, kontrolerów

• Podstawowe filtrowanie i sortowanie

• Wywołuje zdarzenia (events)

FOS REST BUNDLE

• Wykrywa format po nagłówach HTTP

• Wybiera i zwraca response w porządanym formacie

• Integruje kontroler z serializerem

JMS Serializer BUNDLE

• Serializuje dane do opowiedniego formatu

• Łatwo konfigurowalny serializer

BaZinga hateoas Bundle

• Dodaje linki relacji

• Obsługuje XML i JSON

• Łatwo konfigurowalne relacje

@micmarcinkowski @Lakion

lakion.com/lionframe

„Time is money” Benjamin Franklin

Image Credits

http://martinfowler.com/articles/richardsonMaturityModel.html

https://www.flickr.com/photos/kwl/4247555680