Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Обработка сложных POST/PATCH запросов в RESTfu API

Dmitry Petrov
December 03, 2012
450

Обработка сложных POST/PATCH запросов в RESTfu API

Доклад с выступления на sfcampua 2012

Dmitry Petrov

December 03, 2012
Tweet

Transcript

  1. ProFIT Dmitry Petrov Ежедневно: ~ 1 000 заказов ~1 000

    000 печатной продукции 1 час простоя ~ 25 000$ Product Fulfillment and Information Tracking
  2. RESTful API Dmitry Petrov ~ 60 entity ~100 API endpoints

    Сложная бизнес логика RESTful API для ProFIT
  3. Dmitry Petrov GET /api/orders/12/items/fg45sf54 Ответ сервера: { "id": "fg45sf54", "url":

    "http://localhost/api/orders/12/items/fg45sf54", "product": "business cards", "quantity": 1000, "previews": { "front": { "large": "http://localhost/large/front.jpg", "medium": "http://localhost/medium/front.jpg", "small": "http://localhost/small/front.jpg", }, "back": { "large": "http://localhost/large/back.jpg", "medium": "http://localhost/medium/back.jpg", "small": "http://localhost/small/back.jpg", } } } GET /api/orders/12 Ответ сервера: { "id": 12, "url": "http://localhost/api/orders/12", "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": null, "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } RESTful API, примеры GET
  4. Dmitry Petrov GET /api/product-box-types/12-type/associations Ответ сервера: [ { "id": 1,

    "product": "business_cards", "quantity": 1000 }, ...... ] GET /api/machines/KARAT+1/hot-folders Ответ сервера: [ { "path":"/home/somepath/", "types": [ "34-f-Type", "33-S-Type", ...... ] }, ...... ] GET /api/press-sheets/134/label Ответ сервера: { "label": "epl string" } RESTful API, примеры GET
  5. Dmitry Petrov POST http://localhost/api/press-sheets/12/transition Тело запроса: { "transition": "start:printing:front", "note":

    null } POST http://localhost/api/orders, PUT http://localhost/api/orders/12 Тело запроса: { "id": 12, "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": null, "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } RESTful API, примеры POST / PUT
  6. Dmitry Petrov PATCH http://localhost/api/orders/12 Тело запроса: { "client": { "email":

    "", "phone": null } } PATCH http://localhost/api/orders/12 Тело запроса: { "client": { "email": "" }, "address": { "street": "Vavilova", "residentional": true } } Объект: { "id": 12, "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "[email protected]", "phone": "8-888-999", "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } RESTful API, примеры PATCH
  7. DTO Dmitry Petrov Data Transfer Object DTO attribute1: String attribute2:

    String Assembler createDTO updateDomainObject serialize deserialize DomainObject1 attribute1: String DomainObject2 attribute2: String
  8. Dmitry Petrov GET /api/orders/12 Ответ сервера: { "id": 12, "url":

    "http://localhost/orders/12", "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": null, "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } Примеры DTO
  9. Dmitry Petrov { "transition": "start:printing:front", "note": null } { "label":

    "epl string" } [ { "path":"/home/somepath/", "types": [ "34-f-Type", ...... ] }, ...... ] Примеры DTO
  10. Dmitry Petrov GET /api/orders/12 Ответ сервера: { "id": 12, "url":

    "http://localhost/api/orders/12", "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": null, "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } JMSSerializerBundle & GET метод
  11. Dmitry Petrov POST /api/orders, Тело запроса: { "id": 12, "client":

    { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": null, "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } JMSSerializerBundle & POST метод
  12. Dmitry Petrov PATCH /api/orders/12 Request: { "client": { "email": "",

    "phone": null } } JMSSerializerBundle & PATCH метод
  13. Проблемы / Минусы Dmitry Petrov GET - сериализация null значений

    PATCH - десериализация в объект PATCH - merge null значений MERGE - много бесполезного кода RESTful API, JMSSerializerBundle
  14. Dmitry Petrov GET /api/orders/12 Ответ сервера: { "id": "12", "url":

    "http://localhost/orders/12", "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "", "phone": "", "address": { "country": "Russia", "city": "Saratov", "zip": "123456", "street": "Vavilova", "residentional": "false" } } } SimpleThingsFormSerializerBundle
  15. Проблемы / Минусы Dmitry Petrov Конвертирование данных в string Отсутствие

    поддержки PATCH (v. 2.0) Идеологическая неприязнь Грязная смесь *Type и *DTO SimpleThingsFormSerializerBundle
  16. Допущения Dmitry Petrov Выходной формат json Метадата хранится в yml

    Всегда есть get/set методы Изобретаем велосипед
  17. Через 36 часов... поезд Саратов - Киев идет 30 часов

    Dmitry Petrov SimpleSerializer SimpleSerializerBundle Подробности можно прочитать на хабре Изобретаем велосипед
  18. Преимущества Dmitry Petrov Библиотека Разделение правил сериализации от формата Отсутствие

    озвученных минусов "Интеллектуальная" десериализация SimpleSerializer
  19. Dmitry Petrov PATCH /api/orders/12 Тело запроса: { "client": { "email":

    "", "comment": "I'm hacker" } } Объект: { "id": 12, "client": { "firstname": "Dmitry", "lastname": "Petrov", "email": "[email protected]", "phone": "8-888-999", "address": { "country": "Russia", "city": "Saratov", "zip": 123456, "street": "Vavilova", "residentional": false } } } RESTful API, валидация
  20. Dmitry Petrov POST /api/press-sheets/12/transition Тело запроса: { "transition": "start:printing:front", "note":

    null, "comment": "I'm hacker" } POST /api/press-sheets/12/transition Тело запроса: { "transition": "start:printing:front", "comment": "I'm hacker" } Объект: { "transition": "start:printing:front", "note": null } RESTful API, валидация
  21. WSSE Dmitry Petrov Atom Authentication How to create a custom

    Authentication Provider EscapeWSSEAuthenticationBundle (v. 2.0) MopaWSSEAuthenticationBundle (v. 2.1) RESTful API, аутентификация
  22. Password digest Dmitry Petrov Base64 (SHA1 (Nonce + CreationTimestamp +

    Password)) RESTful API, аутентификация