SlideShare a Scribd company logo
REST beyond CRUD
Paulo Sousa
@pagsousa
 Sample REST API for “more than CRUD”
 Examples
 Shopping Cart
 Book printing
Shopping cart
 Create a new cart
 Obtain its contents
 Add an item
 Remove an item
 Empty the cart
 Calculate full charges
 Proceed to checkout
Base URI
 Cart as top level resource
 http://example.org/carts/
Or
 Cart as sub resource of user
 http://example.org/users/{id}/cart
Creating a cart
 Cart as top level resource
POST example.org/carts/
 Would return a new cart resource URI
201 Created
Location: http://example.org/carts/123
 Cart as sub resource of user
 Not necessary, cart is already available
 http://example.org/users/{id}/cart
Obtain the content of the cart
 Issue a GET to the resource
GET /carts/{id}
or
GET /users/{id}/cart
 On begining, would return a representation of the empty cart
200 Ok
{itens: []}
 Otherwise would return the current content of the cart
200 Ok
{itens: [{id:..., qty:...}, ...]}
Add an item
 PUT an updated representation of the entire cart
PUT /carts/{id}
{itens: [{id: ..., qty: ...},{id: ..., qty: ...}]}
Or
 Treat the cart as a collection and POST a new item
POST /carts/{id}/itens
{id: ..., qty: ...}
 Would return the URI of the newly added item
201 Created
Location: http://example.org/carts/123/itens/456
Remove an item
 PUT an updated representation of the entire cart
PUT /carts/{id}
{itens: [{id: ..., qty: ...}]}
Or
 Treat the cart as a collection and DELETE an item
DELETE /carts/{id}/itens/{itemid}
Empty the cart
 PUT an updated representation of the empty cart
PUT /carts/{id}
{itens: []}
Or
 Treat the cart as a collection and DELETE the itens
DELETE /carts/{id}/itens
Calculate full charges
 If the calculation is transitory, just GET the result of the calculation
GET /carts/{id}/fullcharges
or
GET /users/{id}/cart/fullcharges
 Would return
{itens: 100, taxes: 23, shipping: 12, total: 140, currency:
EUR}
 Or an updated shopping cart representation with charges element
{itens: [...], charges: {...}}
 This element should be considered transient and not part of the resource
 Former approach is “better” as it is a separate resource
Calculate full charges (2)
 If charges are to be considered a persistent part part of the
resource
 By doing the calculation you are in fact changing the resource
 i.e., Have side effect, so GET is to be avoided
 POST the request for the calculation,
 e.g.
POST /carts/{cid}
{fullcharges: yes}
 Would return updated shopping cart
{itens: [...], charges: {...}}
Procceed to checkout
 POST an order with the content of the shopping cart?
GET /carts/{id}/as-order
POST /orders
Or
 POST to a check-out controller?
POST /carts/{id}/checkout
or
POST /checkout?cart={id}
or
POST /salesprocess/checkout
Book printing example
 Get a pdf of a book
 Print a book
Book printing example
 Download a pdf representation of a book for
printing at the client
GET /books/{id}?template={tmpl}
Accept: application/pdf
Book printing example
 Print a book (at the server)
POST /books/{id}/print?printer=hp01
 In fact what we want is to order a printing service...
POST /printorders/
{what: “/books/{id}”, quality: ..., ... }
202 Accepted
Location: /printorders/3Af42X
Not very
beatiful
Book printing example
 Query the status
GET /printorders/{id}
200 Ok
{status = “pending”, ...}
Book printing example
 Cancel the printing order
DELETE /printorders/{id}
200 Ok
 If the order could not be deleted anymore
(e.g., already printing), the service would
return a 405 Method Not Allowed

More Related Content

Viewers also liked

Benefits of Hypermedia API
Benefits of Hypermedia APIBenefits of Hypermedia API
Benefits of Hypermedia API
Paulo Gandra de Sousa
 
Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID code
Paulo Gandra de Sousa
 
Modern web architectural patterns
Modern web architectural patternsModern web architectural patterns
Modern web architectural patterns
Paulo Gandra de Sousa
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Paulo Gandra de Sousa
 

Viewers also liked (7)

Benefits of Hypermedia API
Benefits of Hypermedia APIBenefits of Hypermedia API
Benefits of Hypermedia API
 
Decoupled Communication
Decoupled CommunicationDecoupled Communication
Decoupled Communication
 
Communication
CommunicationCommunication
Communication
 
Principles of Service Orientation
Principles of Service OrientationPrinciples of Service Orientation
Principles of Service Orientation
 
Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID code
 
Modern web architectural patterns
Modern web architectural patternsModern web architectural patterns
Modern web architectural patterns
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
 

More from Paulo Gandra de Sousa

Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
Paulo Gandra de Sousa
 
Minds-on DDD
Minds-on DDDMinds-on DDD
Minds-on DDD
Paulo Gandra de Sousa
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
Paulo Gandra de Sousa
 
Design Patterns: Back to Basics
Design Patterns: Back to BasicsDesign Patterns: Back to Basics
Design Patterns: Back to Basics
Paulo Gandra de Sousa
 
Hypermedia APIs
Hypermedia APIsHypermedia APIs
Hypermedia APIs
Paulo Gandra de Sousa
 
Documenting Software Architectures
Documenting Software ArchitecturesDocumenting Software Architectures
Documenting Software Architectures
Paulo Gandra de Sousa
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
Paulo Gandra de Sousa
 

More from Paulo Gandra de Sousa (9)

Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Minds-on DDD
Minds-on DDDMinds-on DDD
Minds-on DDD
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
Design Patterns: Back to Basics
Design Patterns: Back to BasicsDesign Patterns: Back to Basics
Design Patterns: Back to Basics
 
Hypermedia APIs
Hypermedia APIsHypermedia APIs
Hypermedia APIs
 
Revision control with Mercurial
Revision control with MercurialRevision control with Mercurial
Revision control with Mercurial
 
Documenting Software Architectures
Documenting Software ArchitecturesDocumenting Software Architectures
Documenting Software Architectures
 
models of distributed computing
models of distributed computingmodels of distributed computing
models of distributed computing
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 

Recently uploaded

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 

REST beyond CRUD

  • 1. REST beyond CRUD Paulo Sousa @pagsousa
  • 2.  Sample REST API for “more than CRUD”  Examples  Shopping Cart  Book printing
  • 3. Shopping cart  Create a new cart  Obtain its contents  Add an item  Remove an item  Empty the cart  Calculate full charges  Proceed to checkout
  • 4. Base URI  Cart as top level resource  http://example.org/carts/ Or  Cart as sub resource of user  http://example.org/users/{id}/cart
  • 5. Creating a cart  Cart as top level resource POST example.org/carts/  Would return a new cart resource URI 201 Created Location: http://example.org/carts/123  Cart as sub resource of user  Not necessary, cart is already available  http://example.org/users/{id}/cart
  • 6. Obtain the content of the cart  Issue a GET to the resource GET /carts/{id} or GET /users/{id}/cart  On begining, would return a representation of the empty cart 200 Ok {itens: []}  Otherwise would return the current content of the cart 200 Ok {itens: [{id:..., qty:...}, ...]}
  • 7. Add an item  PUT an updated representation of the entire cart PUT /carts/{id} {itens: [{id: ..., qty: ...},{id: ..., qty: ...}]} Or  Treat the cart as a collection and POST a new item POST /carts/{id}/itens {id: ..., qty: ...}  Would return the URI of the newly added item 201 Created Location: http://example.org/carts/123/itens/456
  • 8. Remove an item  PUT an updated representation of the entire cart PUT /carts/{id} {itens: [{id: ..., qty: ...}]} Or  Treat the cart as a collection and DELETE an item DELETE /carts/{id}/itens/{itemid}
  • 9. Empty the cart  PUT an updated representation of the empty cart PUT /carts/{id} {itens: []} Or  Treat the cart as a collection and DELETE the itens DELETE /carts/{id}/itens
  • 10. Calculate full charges  If the calculation is transitory, just GET the result of the calculation GET /carts/{id}/fullcharges or GET /users/{id}/cart/fullcharges  Would return {itens: 100, taxes: 23, shipping: 12, total: 140, currency: EUR}  Or an updated shopping cart representation with charges element {itens: [...], charges: {...}}  This element should be considered transient and not part of the resource  Former approach is “better” as it is a separate resource
  • 11. Calculate full charges (2)  If charges are to be considered a persistent part part of the resource  By doing the calculation you are in fact changing the resource  i.e., Have side effect, so GET is to be avoided  POST the request for the calculation,  e.g. POST /carts/{cid} {fullcharges: yes}  Would return updated shopping cart {itens: [...], charges: {...}}
  • 12. Procceed to checkout  POST an order with the content of the shopping cart? GET /carts/{id}/as-order POST /orders Or  POST to a check-out controller? POST /carts/{id}/checkout or POST /checkout?cart={id} or POST /salesprocess/checkout
  • 13. Book printing example  Get a pdf of a book  Print a book
  • 14. Book printing example  Download a pdf representation of a book for printing at the client GET /books/{id}?template={tmpl} Accept: application/pdf
  • 15. Book printing example  Print a book (at the server) POST /books/{id}/print?printer=hp01  In fact what we want is to order a printing service... POST /printorders/ {what: “/books/{id}”, quality: ..., ... } 202 Accepted Location: /printorders/3Af42X Not very beatiful
  • 16. Book printing example  Query the status GET /printorders/{id} 200 Ok {status = “pending”, ...}
  • 17. Book printing example  Cancel the printing order DELETE /printorders/{id} 200 Ok  If the order could not be deleted anymore (e.g., already printing), the service would return a 405 Method Not Allowed