SlideShare a Scribd company logo
RESTful APIs: Tips &
Tricks
Maksym Bruner
April 5, 2018
I am:
• Solution Architect in EPAM
• Java developer with 10+ years of experience
(also .NET, PHP)
• Kharkiv JUG Program Committee Head
• Bar owner
Who am I?
What do we need tests for?
1. Why it matters and what we won't talk about
2. Quick tips - one page you can easily find in Internet
3. What is hard to find in Internet
4. What is nearly impossible to find and what nobody likes to talk about
5. Tools, tools, tools
6. Modern approaches for communication between backend and frontend
Agenda
What do we need tests for?
• HATEOAS – never seen
• Management solutions – different focus
• Implementation details – takes to much time
Not today
•API (Application Programming Interface) is a
traditional and well known integration method.
An approach to represent Business or IT assets and enable
and enable programmatic access to them
•Recently, the term “API” often implies Web API:
“Web APIs are the defined interfaces through which
interactions happen between an enterprise and applications
applications that use its assets” - Wikipedia
•In business context APIs are seen as one of the pillars
of digital business: they help in building the
automated business ecosystems also known as
WHAT IS AN API?
GET statuses/mentions_timeline
GET statuses/user_timeline
GET statuses/retweets_of_me
GET statuses/retweets/:id
GET statuses/show/:id
POST statuses/destroy/:id
POST statuses/update
POST statuses/retweet/:id
01001
10110 CORE
BUSINESS
WHAT?
HOW?
API CLASSIFICATIONS: WHAT TYPES OF APIs EXIST?
▶ Data
▶ Processes
▶ Things
ASSETS
▶ Private
▶ Partner
▶ Public
SCOPE
▶ Free
▶ Developer pays
▶ Developer gets paid
▶ Indirect
BUSINESS
MODEL
▶ Based on specification or standard (e.g.
FHIR, OData)
▶ Unique, non-standard
STANDAR
DS
▶ SOAP
WEB
SERVICES
▶ Hypermedia
▶ Pragmatic REST
REST
▶ Websockets
▶ Comet
▶ Web hooks
EVENT-
DRIVEN
TECHNOL
OGY
Simple task:
Implement 3 tier application for saved search service:
• User submits a query
• System generates search results – saved search
• User can view submitted queries and then view
results
• User can archive or delete saved search
Three teams:
• Frontend
• Backend
• DevOps
Workshop
What could go wrong?
• Paths, methods
• Data models
• Input parameters
• Actions
• Security
Efforts:
• 1 hour for discussing happy-path scenarios
• 1 hour for discussing exceptional cases
QUICK WINS
1. Nouns
GET /requests
GET /requests/new
GET /getRequests
2. GET and state
GET /users/123/activate -> activate
3. Plural nouns
GET /users
GET /user
4. Subresources
GET /users/123/activities
What you can find easily on the Internet?
5. Headers
Content-Type: application/json
Accept: appication/json, application/xml
6. Sorting and filtering
GET /cars?color=red
GET /cars?sort=+name,-age
7. Select fields
GET /cars?fields=name,color
8. Paging
GET /cars?limit=0&offset=10
What else?
10. Field names
Use camelCase that is a standard for JSON
11. Use error codes
2xx – ok
3xx – redirection
4xx – client error
5xx – server error
12. Versioning
/v1/cars
13. Shortcuts
/requests/closed
/notifications/last
14. Use or not use wrappers
{
count: 10,
list: [..]
}
15. Defaults for pagination and filtering
16. Authentication
WHAT IS NOT SO CLEAR THERE
What must be considered during design:
• How simple is to understand and ”Clean” design
• How to secure URI
• Usage scenarios and domain
• Caching
• Overlapping with other URIs
• REST requirements
Scenarios:
• View my requests
• View my requests (Support team)
• View requests from my department (Manager)
URI Structures
GET /requests
GET /requests?user_id={user_id}
GET /{user_id}/requests
GET /users/{user_id}/requests
GET /requests
X-User-Identifier: {user_id}
GET /support/requests
GET /requests/my
Cache-Control: private
GET /me/requests
Cache-Control: private
GET /requests/1234
--------------------
HTTP/1.1 200 OK
{
id: “1234”,
subject: “Password expired”,
categoryId: “22”,
created: …,
status: “APPROVED”,
assignee: {
id: “4321”,
name: “Amit Kumar”
}
}
Data models
POST /requests
{
subject: “Password expired”,
categoryId: “22”
}
--------------------
HTTP/1.1 201 Created
Location:
http://example.org/requests/1234
{
id: “1234”,
…
}
PUT /requests/1234
{
!!!
}
--------------------
PATCH /requests/1234
{
subject: “Password is expired”
}
--------------------
PATCH /requests/1234
[
{ subject: “Password is expired” },
{ tags: “3,45,121” },
]
Data models #2
GET /requests/1234/edits
--------------------
[
{
date: “2018-04-04T14:20:25.912Z”,
author: {
id: “513”,
name: “Rajeev Jane”
},
changes: [
{ subject: “Password is expired” },
{ tags: “3,45,121” },
]
},…
]
Prefer header
Applicable for:
• Different content schemas for GET requests
• return-minimal
• return-full
• Different return objects
• return-content
• return-status
• return-no-content
• Versioning
• Free format responses
Data models #3
HTTP/1.1 201 Created
Location: http://example.org/requests/1234
{
id: “1234”,
…
}
HTTP/1.1 201 Created
Location: http://example.org/requests/1234
{
status: ”ok”
}
HTTP/1.1 204 No Content
Location: http://example.org/requests/1234
Take into account:
• Caching must be proxy aware
• Aggressive caching might happen
• Header that might affect response content
Additional directives:
• max-age=<seconds> – overrides Expires header
• must-revalidate – forces resources validation
Examples:
Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: public, max-age=31536000
What is eligible for:
• GET and HEAD
• 200, 203, 206, 300, 301, 410
Caching API Calls
Cache-
Control
Local
cache
Cache
anywhere
Revalidati
on
no-store no no n/a
private yes no no
no-cache yes yes yes
public yes yes no
If-Modified-Since != Last-Modified = 200
If-Modified-Since == Last-Modified = 304
If-None-Match != Etag = 200
If-None-Match == Etag = 304
HTTP/1.1 200 OK
Etag: “1234-1”
Last-Modified: Sat, 27 Jun 2015 11:03:32 GMT
Authentication methods:
• Basic
Authorization: Basic ZnJlZDpmcmVk
• API Key
X-API-Key: abcdef12345
• OAuth
Authorization: Bearer
pwwbkvv7abqzonnvztpea91ich7vprwdorbt4w4m
• OAuth JWT tokens
• OAuth scopes: granularity for permissions
• 54 scopes used by Slack API
• users.profile:read,
• users.profile:write
Security
Code Description
400 Bad
Request
Request cannot be processed. Used when
request contains unsupported parameters.
Also, used for validation errors or wrong
input format indication.
401
Unauthorized
Request requires authentication.
403 Forbidden
Access is not allowed to a requested
resource or operation.
404 Not Found Resource was not found.
500 Internal
Server Error
Error happened on server side. Default
handlers that provides such details as a
stack trace in response MUST be avoided.
Error design (including validation)
{
code: 1234,
message: “Validation failed”,
errors: [
{
field: “referenceEntityId”,
errorMessage: “Not found”
},
{
field: “name”,
errorMessage: “Name is required”
}
]
}
Use Content-Type for different schemas:
• application/json;profile=NoBodyError
• application/json;profile=NoParameterError
• application/json;profile=ValidationError
What else can be included:
• Link to documentation
• Error description
WHAT IS HARD TO FIND
Take care of:
• All possible workflows
• Linked resources
• Actors
• Domain model
Design tips:
• Try treating actions as resources
• Use PATCH with complex payloads
• Use headers for meta information
Complex actions
Context: IT support system
Use cases:
1 Re-assign request
2 Enrich extra request information
3 Clone request
4 Grant permission to view requests
5 Transfer to another system
6 Merge requests
7 Lock/unlcok request
8 Change status
9 skip
1 Re-assign request
2 Enrich extra request information
3 Clone request
Complex actions #2
1. PATCH /requests/{request_id}
{
assigneeId: <new_id>,
}
2. PUT /requests/{request_id}/environment {
{
osVersion: "MAC OS X...",
hardware: {…},
destination: "EXTERNAL NETWORK",
...
}
3. POST /requests
Content-Type: application/reference
X-Reference-Id: <requst_id>
X-Clone-Parameters: copy-attachments
4 Grant permission to view
requests
5 Transfer to another system
Complex actions #3
4. PATCH /requests/{request_id}/acl
{ userId: <user_id>, permissions: "view,edit” }
5. POST /legacy_support/transfers
{ requestId: “1234” }
201 Created
{
transferId: "321",
status: "TRANSFERED",
originalRequestId: "1234",
legacyRequestId: "90189",
legacyLink: "http://old.support.company.net/requests/ID-
90189"
}
GET /legacy_support/requests/90189
301 Moved Permanently
Location: http://old.support.company.net/requests/ID-90189
6 Merge requests
7 Lock/unlock request
Complex actions #4
7. PATCH /requests/{request_id}/lock_status
{
locked: true,
details: { comment: "Lock for audit" }
}
GET /requests/{request_id}/lock_status
{
locked: true,
lockedBy: "John Doe"
since: "2018-04-04T14:20:25.912Z"
details: {
comment: "Lock for audit"
}
}
PATCH /requests/{request_id}/lock_status
{ locked: false }
8 Change status
Complex actions #5
7. PATCH /requests/{request_id}/status
{
status: "CLOSED",
details: {
resolution: "RESOLVED",
comment: "Password was reset"
}
}
GET /requests/{request_id}/status/history
[
{
status: "CLOSED",
date: "2018-04-04T14:20:25.912Z",
changedBy: "John Doe"
},
{
status: "IN_PROGRESS",
date: "2018-04-03T14:20:25.912Z",
changedBy: "John Doe"
},…
]
Limitations:
• URL length
• Copy link
• Durability
• Caching results (client/server)
Additional considerations:
• Saved presets
• Saved queries
Complex searches
1. GET /requests?name=param&status=SUBMITTED
2. GET /requests?filter={query}
3. POST /requests/search
{
author: "John",
status: "SUBMITTED"
}
201 Created
{
searchId: "9876",
expire: "2018-04-10T14:20:25.912Z"
}
GET /requests/search/9876?limit=15&offset=30
200 OK
X-Total-Count: 253
...
1. Resources
2. Methods
3. Response Codes
4. Tags
5. Models
Design steps
6. Complex Actions
7. Pagination & Sorting
8. Security
9. Error Models
10. Examples
1.Control and monitoring
• Control what you publish
• Throttling
• Control changes
2.Versioning
• Use hard versioning by including API version to requests
• Use soft versioning: change responses by manipulating
headers
Development
TOOLS
• Declarative schema definition
• UI support
• Validation
• New 3.0 version released last
year
• Code generation
Swagger or Open API
Swagger Hub
Swagger Diff
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
author: Author
votes: Int
}
type Query {
author(id: Int!): Author
}
GraphQL
query PostsForAuthor {
author(id: 1) {
firstName
posts {
title
votes
}}}
--------------------
{
"data": {
"author": {
"firstName": "Tom",
"posts": [
{ "title": "Introduction to GraphQL",
"votes": 2 }
]
}
}}
What next?
Workshop (1 day):
• Define business case
• Go through iterative process and design API
• 10 steps
• From scratch to comprehensive design
• Implement according to specification
• Code generation
• Spring adaptation
• Deployment and access
What next?
Questions?

More Related Content

What's hot (20)

PDF
In search of: A meetup about Liferay and Search 2016-04-20
Tibor Lipusz
 
PPT
AppSec 2007 - .NET Web Services Hacking
Shreeraj Shah
 
PPT
On-page SEO for Drupal
Svilen Sabev
 
PPTX
Apache Solr-Webinar
Edureka!
 
PPT
HTML5 hacking
Blueinfy Solutions
 
PDF
Drupal security
Jozef Toth
 
PPTX
Introduction to Lucene & Solr and Usecases
Rahul Jain
 
PPTX
Introduction to Web Services
Jeffrey Anderson
 
PDF
Intro apache
koppenolski
 
PDF
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
Sam Bowne
 
PDF
Web Services PHP Tutorial
Lorna Mitchell
 
PPTX
Tagging search solution design Advanced edition
Alexander Tokarev
 
PDF
Getting started with DSpace 7 REST API
4Science
 
PPT
Struts2-Spring=Hibernate
Jay Shah
 
PPTX
Web crawler
poonamkenkre
 
PDF
How to Do a Performance Audit of Your .NET Website
DNN
 
PDF
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 1 of 2)
Sam Bowne
 
PDF
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
Sam Bowne
 
PDF
Offline strategies for HTML5 web applications - pfCongres2012
Stephan Hochdörfer
 
PDF
Alfresco Content Modelling and Policy Behaviours
J V
 
In search of: A meetup about Liferay and Search 2016-04-20
Tibor Lipusz
 
AppSec 2007 - .NET Web Services Hacking
Shreeraj Shah
 
On-page SEO for Drupal
Svilen Sabev
 
Apache Solr-Webinar
Edureka!
 
HTML5 hacking
Blueinfy Solutions
 
Drupal security
Jozef Toth
 
Introduction to Lucene & Solr and Usecases
Rahul Jain
 
Introduction to Web Services
Jeffrey Anderson
 
Intro apache
koppenolski
 
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
Sam Bowne
 
Web Services PHP Tutorial
Lorna Mitchell
 
Tagging search solution design Advanced edition
Alexander Tokarev
 
Getting started with DSpace 7 REST API
4Science
 
Struts2-Spring=Hibernate
Jay Shah
 
Web crawler
poonamkenkre
 
How to Do a Performance Audit of Your .NET Website
DNN
 
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 1 of 2)
Sam Bowne
 
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
Sam Bowne
 
Offline strategies for HTML5 web applications - pfCongres2012
Stephan Hochdörfer
 
Alfresco Content Modelling and Policy Behaviours
J V
 

Similar to REST Api Tips and Tricks (20)

PPTX
REST Methodologies
jrodbx
 
PPTX
Best Practices for Architecting a Pragmatic Web API.
Mario Cardinal
 
PPTX
A Deep Dive into RESTful API Design Part 2
VivekKrishna34
 
PPTX
Rest WebAPI with OData
Mahek Merchant
 
PPTX
Rest APIs Training
Shekhar Kumar
 
PDF
Создание API, которое полюбят разработчики. Глубокое погружение
SQALab
 
PDF
Embracing HTTP in the era of API’s
Visug
 
PDF
Architecting &Building Scalable Secure Web API
SHAKIL AKHTAR
 
PDF
REST Api with Asp Core
Irina Scurtu
 
PPT
RESTful SOA - 中科院暑期讲座
Li Yi
 
PDF
Modern REST API design principles and rules.pdf
Aparna Sharma
 
PPTX
RESTful APIs in .NET
Greg Sohl
 
PDF
What is REST?
Saeid Zebardast
 
PDF
Principles of building effective REST API
Georgy Podsvetov
 
PPTX
Building-Robust-APIs-ASPNET-Web-API-and-RESTful-Patterns.pptx
keshabregmi1
 
PDF
Don't screw it up! How to build durable API
Alessandro Cinelli (cirpo)
 
PDF
Restful Web Services Cookbook 1st Edition Subbu Allamaraju
roanematot2s
 
PDF
JOSA TechTalks - RESTful API Concepts and Best Practices
Jordan Open Source Association
 
PDF
WebCamp: Developer Day: Принципы построения эффективного REST API - Георгий П...
GeeksLab Odessa
 
PDF
Modern REST API design principles and rules.pdf
Aparna Sharma
 
REST Methodologies
jrodbx
 
Best Practices for Architecting a Pragmatic Web API.
Mario Cardinal
 
A Deep Dive into RESTful API Design Part 2
VivekKrishna34
 
Rest WebAPI with OData
Mahek Merchant
 
Rest APIs Training
Shekhar Kumar
 
Создание API, которое полюбят разработчики. Глубокое погружение
SQALab
 
Embracing HTTP in the era of API’s
Visug
 
Architecting &Building Scalable Secure Web API
SHAKIL AKHTAR
 
REST Api with Asp Core
Irina Scurtu
 
RESTful SOA - 中科院暑期讲座
Li Yi
 
Modern REST API design principles and rules.pdf
Aparna Sharma
 
RESTful APIs in .NET
Greg Sohl
 
What is REST?
Saeid Zebardast
 
Principles of building effective REST API
Georgy Podsvetov
 
Building-Robust-APIs-ASPNET-Web-API-and-RESTful-Patterns.pptx
keshabregmi1
 
Don't screw it up! How to build durable API
Alessandro Cinelli (cirpo)
 
Restful Web Services Cookbook 1st Edition Subbu Allamaraju
roanematot2s
 
JOSA TechTalks - RESTful API Concepts and Best Practices
Jordan Open Source Association
 
WebCamp: Developer Day: Принципы построения эффективного REST API - Георгий П...
GeeksLab Odessa
 
Modern REST API design principles and rules.pdf
Aparna Sharma
 
Ad

Recently uploaded (20)

PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Ad

REST Api Tips and Tricks

  • 1. RESTful APIs: Tips & Tricks Maksym Bruner April 5, 2018
  • 2. I am: • Solution Architect in EPAM • Java developer with 10+ years of experience (also .NET, PHP) • Kharkiv JUG Program Committee Head • Bar owner Who am I?
  • 3. What do we need tests for? 1. Why it matters and what we won't talk about 2. Quick tips - one page you can easily find in Internet 3. What is hard to find in Internet 4. What is nearly impossible to find and what nobody likes to talk about 5. Tools, tools, tools 6. Modern approaches for communication between backend and frontend Agenda
  • 4. What do we need tests for? • HATEOAS – never seen • Management solutions – different focus • Implementation details – takes to much time Not today
  • 5. •API (Application Programming Interface) is a traditional and well known integration method. An approach to represent Business or IT assets and enable and enable programmatic access to them •Recently, the term “API” often implies Web API: “Web APIs are the defined interfaces through which interactions happen between an enterprise and applications applications that use its assets” - Wikipedia •In business context APIs are seen as one of the pillars of digital business: they help in building the automated business ecosystems also known as WHAT IS AN API? GET statuses/mentions_timeline GET statuses/user_timeline GET statuses/retweets_of_me GET statuses/retweets/:id GET statuses/show/:id POST statuses/destroy/:id POST statuses/update POST statuses/retweet/:id 01001 10110 CORE BUSINESS
  • 6. WHAT? HOW? API CLASSIFICATIONS: WHAT TYPES OF APIs EXIST? ▶ Data ▶ Processes ▶ Things ASSETS ▶ Private ▶ Partner ▶ Public SCOPE ▶ Free ▶ Developer pays ▶ Developer gets paid ▶ Indirect BUSINESS MODEL ▶ Based on specification or standard (e.g. FHIR, OData) ▶ Unique, non-standard STANDAR DS ▶ SOAP WEB SERVICES ▶ Hypermedia ▶ Pragmatic REST REST ▶ Websockets ▶ Comet ▶ Web hooks EVENT- DRIVEN TECHNOL OGY
  • 7. Simple task: Implement 3 tier application for saved search service: • User submits a query • System generates search results – saved search • User can view submitted queries and then view results • User can archive or delete saved search Three teams: • Frontend • Backend • DevOps Workshop What could go wrong? • Paths, methods • Data models • Input parameters • Actions • Security Efforts: • 1 hour for discussing happy-path scenarios • 1 hour for discussing exceptional cases
  • 9. 1. Nouns GET /requests GET /requests/new GET /getRequests 2. GET and state GET /users/123/activate -> activate 3. Plural nouns GET /users GET /user 4. Subresources GET /users/123/activities What you can find easily on the Internet? 5. Headers Content-Type: application/json Accept: appication/json, application/xml 6. Sorting and filtering GET /cars?color=red GET /cars?sort=+name,-age 7. Select fields GET /cars?fields=name,color 8. Paging GET /cars?limit=0&offset=10
  • 10. What else? 10. Field names Use camelCase that is a standard for JSON 11. Use error codes 2xx – ok 3xx – redirection 4xx – client error 5xx – server error 12. Versioning /v1/cars 13. Shortcuts /requests/closed /notifications/last 14. Use or not use wrappers { count: 10, list: [..] } 15. Defaults for pagination and filtering 16. Authentication
  • 11. WHAT IS NOT SO CLEAR THERE
  • 12. What must be considered during design: • How simple is to understand and ”Clean” design • How to secure URI • Usage scenarios and domain • Caching • Overlapping with other URIs • REST requirements Scenarios: • View my requests • View my requests (Support team) • View requests from my department (Manager) URI Structures GET /requests GET /requests?user_id={user_id} GET /{user_id}/requests GET /users/{user_id}/requests GET /requests X-User-Identifier: {user_id} GET /support/requests GET /requests/my Cache-Control: private GET /me/requests Cache-Control: private
  • 13. GET /requests/1234 -------------------- HTTP/1.1 200 OK { id: “1234”, subject: “Password expired”, categoryId: “22”, created: …, status: “APPROVED”, assignee: { id: “4321”, name: “Amit Kumar” } } Data models POST /requests { subject: “Password expired”, categoryId: “22” } -------------------- HTTP/1.1 201 Created Location: http://example.org/requests/1234 { id: “1234”, … }
  • 14. PUT /requests/1234 { !!! } -------------------- PATCH /requests/1234 { subject: “Password is expired” } -------------------- PATCH /requests/1234 [ { subject: “Password is expired” }, { tags: “3,45,121” }, ] Data models #2 GET /requests/1234/edits -------------------- [ { date: “2018-04-04T14:20:25.912Z”, author: { id: “513”, name: “Rajeev Jane” }, changes: [ { subject: “Password is expired” }, { tags: “3,45,121” }, ] },… ]
  • 15. Prefer header Applicable for: • Different content schemas for GET requests • return-minimal • return-full • Different return objects • return-content • return-status • return-no-content • Versioning • Free format responses Data models #3 HTTP/1.1 201 Created Location: http://example.org/requests/1234 { id: “1234”, … } HTTP/1.1 201 Created Location: http://example.org/requests/1234 { status: ”ok” } HTTP/1.1 204 No Content Location: http://example.org/requests/1234
  • 16. Take into account: • Caching must be proxy aware • Aggressive caching might happen • Header that might affect response content Additional directives: • max-age=<seconds> – overrides Expires header • must-revalidate – forces resources validation Examples: Cache-Control: no-cache, no-store, must-revalidate Cache-Control: public, max-age=31536000 What is eligible for: • GET and HEAD • 200, 203, 206, 300, 301, 410 Caching API Calls Cache- Control Local cache Cache anywhere Revalidati on no-store no no n/a private yes no no no-cache yes yes yes public yes yes no If-Modified-Since != Last-Modified = 200 If-Modified-Since == Last-Modified = 304 If-None-Match != Etag = 200 If-None-Match == Etag = 304 HTTP/1.1 200 OK Etag: “1234-1” Last-Modified: Sat, 27 Jun 2015 11:03:32 GMT
  • 17. Authentication methods: • Basic Authorization: Basic ZnJlZDpmcmVk • API Key X-API-Key: abcdef12345 • OAuth Authorization: Bearer pwwbkvv7abqzonnvztpea91ich7vprwdorbt4w4m • OAuth JWT tokens • OAuth scopes: granularity for permissions • 54 scopes used by Slack API • users.profile:read, • users.profile:write Security
  • 18. Code Description 400 Bad Request Request cannot be processed. Used when request contains unsupported parameters. Also, used for validation errors or wrong input format indication. 401 Unauthorized Request requires authentication. 403 Forbidden Access is not allowed to a requested resource or operation. 404 Not Found Resource was not found. 500 Internal Server Error Error happened on server side. Default handlers that provides such details as a stack trace in response MUST be avoided. Error design (including validation) { code: 1234, message: “Validation failed”, errors: [ { field: “referenceEntityId”, errorMessage: “Not found” }, { field: “name”, errorMessage: “Name is required” } ] } Use Content-Type for different schemas: • application/json;profile=NoBodyError • application/json;profile=NoParameterError • application/json;profile=ValidationError What else can be included: • Link to documentation • Error description
  • 19. WHAT IS HARD TO FIND
  • 20. Take care of: • All possible workflows • Linked resources • Actors • Domain model Design tips: • Try treating actions as resources • Use PATCH with complex payloads • Use headers for meta information Complex actions Context: IT support system Use cases: 1 Re-assign request 2 Enrich extra request information 3 Clone request 4 Grant permission to view requests 5 Transfer to another system 6 Merge requests 7 Lock/unlcok request 8 Change status 9 skip
  • 21. 1 Re-assign request 2 Enrich extra request information 3 Clone request Complex actions #2 1. PATCH /requests/{request_id} { assigneeId: <new_id>, } 2. PUT /requests/{request_id}/environment { { osVersion: "MAC OS X...", hardware: {…}, destination: "EXTERNAL NETWORK", ... } 3. POST /requests Content-Type: application/reference X-Reference-Id: <requst_id> X-Clone-Parameters: copy-attachments
  • 22. 4 Grant permission to view requests 5 Transfer to another system Complex actions #3 4. PATCH /requests/{request_id}/acl { userId: <user_id>, permissions: "view,edit” } 5. POST /legacy_support/transfers { requestId: “1234” } 201 Created { transferId: "321", status: "TRANSFERED", originalRequestId: "1234", legacyRequestId: "90189", legacyLink: "http://old.support.company.net/requests/ID- 90189" } GET /legacy_support/requests/90189 301 Moved Permanently Location: http://old.support.company.net/requests/ID-90189
  • 23. 6 Merge requests 7 Lock/unlock request Complex actions #4 7. PATCH /requests/{request_id}/lock_status { locked: true, details: { comment: "Lock for audit" } } GET /requests/{request_id}/lock_status { locked: true, lockedBy: "John Doe" since: "2018-04-04T14:20:25.912Z" details: { comment: "Lock for audit" } } PATCH /requests/{request_id}/lock_status { locked: false }
  • 24. 8 Change status Complex actions #5 7. PATCH /requests/{request_id}/status { status: "CLOSED", details: { resolution: "RESOLVED", comment: "Password was reset" } } GET /requests/{request_id}/status/history [ { status: "CLOSED", date: "2018-04-04T14:20:25.912Z", changedBy: "John Doe" }, { status: "IN_PROGRESS", date: "2018-04-03T14:20:25.912Z", changedBy: "John Doe" },… ]
  • 25. Limitations: • URL length • Copy link • Durability • Caching results (client/server) Additional considerations: • Saved presets • Saved queries Complex searches 1. GET /requests?name=param&status=SUBMITTED 2. GET /requests?filter={query} 3. POST /requests/search { author: "John", status: "SUBMITTED" } 201 Created { searchId: "9876", expire: "2018-04-10T14:20:25.912Z" } GET /requests/search/9876?limit=15&offset=30 200 OK X-Total-Count: 253 ...
  • 26. 1. Resources 2. Methods 3. Response Codes 4. Tags 5. Models Design steps 6. Complex Actions 7. Pagination & Sorting 8. Security 9. Error Models 10. Examples
  • 27. 1.Control and monitoring • Control what you publish • Throttling • Control changes 2.Versioning • Use hard versioning by including API version to requests • Use soft versioning: change responses by manipulating headers Development
  • 28. TOOLS
  • 29. • Declarative schema definition • UI support • Validation • New 3.0 version released last year • Code generation Swagger or Open API
  • 32. type Author { id: Int! firstName: String lastName: String posts: [Post] } type Post { id: Int! title: String author: Author votes: Int } type Query { author(id: Int!): Author } GraphQL query PostsForAuthor { author(id: 1) { firstName posts { title votes }}} -------------------- { "data": { "author": { "firstName": "Tom", "posts": [ { "title": "Introduction to GraphQL", "votes": 2 } ] } }}
  • 33. What next? Workshop (1 day): • Define business case • Go through iterative process and design API • 10 steps • From scratch to comprehensive design • Implement according to specification • Code generation • Spring adaptation • Deployment and access What next?