RESTful API๐Ÿ’ก: REST API Design Rulebook

์ •์€๊ฒฝยท2020๋…„ 10์›” 29์ผ
0

RESTful

RESTful์˜ ์˜๋ฏธ
REST์ ์ด๋‹ค, REST์Šค๋Ÿฝ๋‹ค

๊ทธ๋Ÿผ, REST๋Š” ๋ฌด์—‡?

REST (Representational State Transfer)
์œ„ํ‚ค REST

RESTful API

  • "restful api"๋ž€ Endpoint์˜ ์ฃผ์†Œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ํŒจํ„ด์„ ์˜๋ฏธ!
  • ํŒจํ„ด์€ "http ๋ฉ”์†Œ๋“œ์ด๋ฆ„/์ฝ๊ณ ์žํ•˜๋Š”๋ถ€๋ถ„์˜๋ช…์‚ฌ"์ž„!
  • Restful api์—์„œ endpoint์˜ ์ฃผ์†Œ์—๋Š” ๋ช…์‚ฌ๋งŒ ๋“ค์–ด๊ฐ!!!!
  • ์ด๋Ÿฐ ํŒจํ„ด์„ ์“ฐ๋Š” ์ด์œ ๋Š” ์ผ๊ด€์„ฑ์žˆ๋Š” ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ์ž„!

    ์˜ˆ)
    Post/user
    Get/user1
    Get/users
    Delete/user1!




REST API Design Rulebook

: Designing Consistent RESTful Web Service Interfaces

์ฝ์–ด๋ด…์‹œ๋‹น

ํšŒ์‚ฌ์—์„œ ์žฅ๊ณ ๋กœ ๋‚ ์”จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋งก์•˜๋‹ค.
query string์— 'ํ•˜์ดํ”ˆ(-)์„ ์จ์•ผํ•˜๋‚˜? ์–ธ๋”๋ฐ”(_)๋ฅผ ์จ์•ผํ•˜๋‚˜?' ์˜๋ฌธ์ด ๋“ค์–ด์„œ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋Š”๋ฐ.
๊ตฌ๊ธ€ SEO์ด์Šˆ๋กœ ํ•˜์ดํ”ˆ์„ ์“ฐ๋ผ๋Š” ์˜๊ฒฌ์ด ๋งŽ์•˜๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ์ฑ…์„ ์†Œ๊ฐœํ•˜๋Š” ๊ธ€์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.
๊ตญ๋‚ด์„œ์ ์—๋Š” ebook์—†์–ด์„œ
์•„๋งˆ์กด์—์„œ kindle์šฉ ebook์œผ๋กœ ๊ตฌ๋งคํ–ˆ๋‹ค.
์ด์ œ ์ฝ์–ด๋ณด์ž!

๋ชฉ์ฐจ

  1. Introduction
    Hello World Wide Web
    Web Architecture
    Web Standards
    REST API Design Rulebook
    REST APIs
    REST API Design
    Recap
  2. Identifier Design with URIs
    URIs
    URI Format
    URI Authority Design
    Resource Modeling
    Resource Archetypes
    URI Path Design
    URI Query Design
    Recap
  3. Interaction Design with HTTP
    HTTP/1.1
    Request Methods
    Response Status Codes
    Recap
  4. Metadata Design
    HTTP Headers
    Media Types
    Media Type Design
    Recap
  5. Representation Design
    Message Body Format
    Hypermedia Representation
    Media Type Representation
    Error Representation
    Recap
  6. Client Concerns
    Introduction
    Versioning
    Security
    Response Representation Composition
    Processing Hypermedia
    JavaScript Clients
    Recap
  7. Final Thoughts
    State of the Art
    Uniform Implementation
    Recap
    A. My First REST API

๋‚˜๋งŒ์˜ summary

Preface

  • Representational State Transfer (REST)
  • REST๋Š” WWW์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐ์ˆ ์ ์ธ ๋ฌ˜์‚ฌ
  • REST Application Programming Interface (REST API)๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‹œ์Šคํ…œ์˜ ๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜๋“ค์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋กํ•˜๋Š” ์›น์„œ๋ฒ„์˜ ์ข…๋ฅ˜

    A REST Application Programming Interface (REST API) is a type of web server that enables a client, either user-operated or automated, to access resources that model a systemโ€™s data and functions.
    Masse, Mark. REST API Design Rulebook (p. 4). O'Reilly Media. Kindle Edition.

Chapter 1. Introduction

1) Hello World Wide Web

  • URI (Uniform Resource Identifier): ์›น ๋ฌธ์„œ์— ์œ ๋‹ˆํฌํ•œ ์ฃผ์†Œ๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฌธ๋ฒ•
  • HTTP (HyperText Transfer Protocol): ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ์ปดํ“จ๋„ˆ๊ฐ€ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์‹œ์ง€ ๊ธฐ๋ฐ˜์˜ ์–ธ์–ด
  • HTML (HyperText Mark-up Language): ๋ฌธ์„œ๋“ค๊ณผ ์—ฐ๊ด€๋œ ๋งํฌ๋“ค์„ ํฌํ•จํ•˜๋Š” ์œ ์šฉํ•œ ๋ฌธ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ

    On August 6, 1991, on the Webโ€™s first page, Berners-Lee wrote, The WorldWideWeb (W3) is a wide-area hypermedia information retrieval initiative aiming to give universal access to a large universe of documents.[10]
    Masse, Mark. REST API Design Rulebook (p. 10). O'Reilly Media. Kindle Edition.

2) Key constraints:

  • Client-Server
    • separation of concerns
  • Uniform Interface
    • Identification of resources
      • URI
    • Manipulation of resources through representations
      • "representation"์€ ๋ฆฌ์†Œ์Šค ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ "๋ฆฌ์†Œ์Šค์™€ interactํ•˜๋Š” ๋ฐฉ์‹"์„ ์˜๋ฏธ
    • Self-descriptive message
    • Hypermeida as the engine of application state (HATEOAS)
  • Layered System
  • Cache
  • Stateless
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ state์— ๋Œ€ํ•œ ๋ณต์žก์„ฑ์„ ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ, ์›น์„œ๋ฒ„๋Š” ๋” ๋งŽ์€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ
  • Code-on-demand
    • ์˜ต์…”์ €๋„ ํ•จ

3) Web Standards

4) REST

5) REST APIs

Chapter 2. Identifier Design with URIs

"a set of design rules for REST API URIs"

1) URI (Uniform Resource Identifier)

Tim Berners-Lee included a note about the opacity of URIs in his โ€œAxioms of Web Architectureโ€ list:

The only thing you can use an identifier for is to refer to an object.
When you are not dereferencing, 
you should not look at the contents of the URI string to gain other information. 
								โ€” Tim Berners-Lee 

http://www.w3.org/DesignIssues/Axioms.html
Masse, Mark. REST API Design Rulebook (p. 22). O'Reilly Media. Kindle Edition.

2) URI format

The rules presented in this section pertain to the format of a URI. RFC 3986[19] defines the generic URI syntax as shown below:

URI = scheme "://" authority "/" path [ "?" query ] [ "#" fragment ]

Masse, Mark. REST API Design Rulebook (pp. 22-23). O'Reilly Media. Kindle Edition.

Rule 1. ๊ณ„์ธต ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ๋Š” "/" ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉ
Rule 2. URI์—๋Š” ํŠธ๋ ˆ์ผ๋ง ์Šฌ๋Ÿฌ์‹œ("/") ์‚ฌ์šฉ ๋ถˆํ•„์š”
Rule 3. ํ•˜์ดํ”ˆ("-")์„ ์‚ฌ์šฉํ•˜๋ฉด URI ๊ฐ€๋…์„ฑ์ด ์ฆ๊ฐ€๋จ
Rule 4. ์–ธ๋”์Šค์ฝ”์–ด("_")๋Š” URI์— ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ
Rule 5. URI ๊ฒฝ๋กœ์—๋Š” ์†Œ๋ฌธ์ž๊ฐ€ ๋” ์„ ํ˜ธ๋จ
    - ๋„๋ฉ”์ธ ์ด๋ฆ„์€ ๋Œ€์†Œ๋ฌธ์ž ์ƒ๊ด€์—†๋Š”๋ฐ
    - ๋„๋ฉ”์ธ์„ ์ œ์™ธํ•œ URI๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ด์šฉ!

RFC 3986 defines URIs as case-sensitive except for the scheme and host components. For example:
(1) http://api.example.restapi.org/my-folder/my-doc
(2) HTTP://API.EXAMPLE.RESTAPI.ORG/my-folder/my-doc
(3) http://api.example.restapi.org/My-Folder/my-doc
(1) This URI is fine.
(2) The URI format specification (RFC 3986) considers this URI to be identical to URI #1.
(3) This URI is not the same as URIs 1 and 2, which may cause unnecessary confusion.
Masse, Mark. REST API Design Rulebook (p. 24). O'Reilly Media. Kindle Edition.

Rule 6. ํŒŒ์ผ ํ™•์žฅ์ž๋Š” URI ํฌํ•จํ•˜์ง€ ๋ง ๊ฒƒ
   - REST API์—์„œ๋Š” URI ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š์•„์š”
   - ํŒŒ์ผ์ด ์–ด๋–ค ์ข…๋ฅ˜์˜ ํŒŒ์ผ์ธ์ง€๋Š” HTTP์˜ Content-Type ํ—ค๋”์—์„œ ๋ฏธ๋””์–ด ํƒ€์ž…์„ ํ†ตํ•ด ์•Œ๋ ค์ฃผ์ง€์š”!

3) URI Authority Design
๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜ ๋ถ€๋ถ„!

Rule 1. API๋ฅผ ์œ„ํ•ด์„œ ์ผ๊ด€์ ์ธ ์„œ๋ธŒ๋„๋ฉ”์ธ ์ด๋ฆ„ ์‚ฌ์šฉํ•  ๊ฒƒ
Rule 2. ํด๋ผ์€ ๊ฐœ๋ฐœ์ž ํฌํ„ธ์„ ์œ„ํ•ด์„œ ์ผ๊ด€์ ์ธ ์„œ๋ธŒ๋„๋ฉ”์ธ ์ด๋ฆ„ ์‚ฌ์šฉํ•  ๊ฒƒ

4) Resource Modeling

5) Resource Archetypes
1. Document
2. Collection : ์„œ๋ฒ„๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋“ค์˜ "๋””๋ ‰ํ„ฐ๋ฆฌ"
3. Store : ํด๋ผ์ด์–ธํŠธ ๊ด€๋ฆฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋“ค์˜ "์ €์žฅ์†Œ"
4. Controller : ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ธํ’‹๊ณผ ์•„์›ƒํ’‹์ด ์žˆ์Œ
"storeํ•˜๊ณ  controller์˜ ๊ฐœ๋… ์•„๋ฆฌ์†ก์†กํ•˜๋‹ค -_-"

6) URI Path Design
Rule 1. ์ปฌ๋ ‰์…˜ ์ด๋ฆ„์œผ๋กœ๋Š” ๋ณต์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ
Rule 2. ์Šคํ† ์–ด ์ด๋ฆ„์œผ๋กœ๋Š” ๋ณต์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ
Rule 3. ์ปจํŠธ๋กค๋Ÿฌ ์ด๋ฆ„์œผ๋กœ๋Š” ๋™์‚ฌ/๋™์‚ฌ๊ตฌ ์‚ฌ์šฉํ•  ๊ฒƒ
Rule 4. ๋ณ€๋™์ด ์žˆ๋Š” ๊ฒฝ๋กœ ์„ธ๊ทธ๋จผํŠธ๋Š” ID๊ธฐ๋ฐ˜์˜ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅ
Rule 5. CRUD ๊ธฐ๋Šฅ์ด๋ฆ„์€ URI์— ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ

7) URI Query Design
Rule 1. URI์˜ ์ฟผ๋ฆฌ ์ปดํฌ๋„ŒํŠธ๋Š” ์ปฌ๋ ‰์…˜ ๋˜๋Š” ์Šคํ† ์–ด๋ฅผ filterํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ
Rule 2. URI์˜ ์ฟผ๋ฆฌ ์ปดํฌ๋„ŒํŠธ๋Š” ์ปฌ๋ ‰์…˜ ๋˜๋Š” ์Šคํ† ์–ด์˜ ๊ฒฐ๊ณผ๋ฅผ paginateํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ

Chapter 3.

Reference

profile
#์˜์‹์˜ํ๋ฆ„ #์ˆœ๊ฐ„์ˆœ๊ฐ„ #์ƒ๊ฐ์˜์Šค๋ƒ…์ƒท

0๊ฐœ์˜ ๋Œ“๊ธ€