๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Web

RESTful API ์„ค๊ณ„ ์›์น™ / ๋„ค์ด๋ฐ ๊ทœ์น™

by 1mj 2023. 5. 8.

RESTful API

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•œ ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ

 

RESTful API Naming

  • ๋ฆฌ์†Œ์Šค ํ‘œํ˜„์— ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
  • ๊ณ„์ธต๊ด€๊ณ„๋ฅผ ๊ตฌ๋ถ„์ง“๊ธฐ ์œ„ํ•ด ์Šฌ๋ž˜์‹œ(/)๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
  • URI ๋งˆ์ง€๋ง‰์— ์Šฌ๋ž˜์‹œ๋ฅผ ๋ถ™์ด์ง€ ๋งˆ๋ผ.
  • ํ•˜์ดํ”ˆ(-) ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ๋ผ.
  • ์–ธ๋”์Šค์ฝ”์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.
  • ์†Œ๋ฌธ์ž๋งŒ์„ ์‚ฌ์šฉํ•˜๋ผ.

 

๋ฆฌ์†Œ์Šค ํ‘œํ˜„์— ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

RESTful URI ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฆฌ์†Œ์Šค๋Š” ์ˆ˜ํ–‰๋˜๋Š” ํ–‰์œ„๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด์ด๋‹ค.

์ด ๋ฆฌ์†Œ์Šค๊ฐ€ ์–ด๋Š ๋ฒ”์ฃผ์— ํ•ด๋‹นํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๊ทธ ๋ฒ”์ฃผ์— ๋งž๋Š” ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

โ˜‘๏ธ ๋ฌธ์„œ(Document)

  • ๋‹จ์ผ ๊ฐœ๋…(ํŒŒ์ผ ํ•˜๋‚˜, ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค row)
  • ๋‹จ์ˆ˜ ์‚ฌ์šฉ(/device-management, /user-management)
http://api.example.com/device-management/managed-devices/{device-id}
http://api.example.com/user-management/users/{id}
http://api.example.com/user-management/users/admin

 

โ˜‘๏ธ ์ปฌ๋ ‰์…˜(Collection)

  • ์„œ๋ฒ„๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค ๋””๋ ‰ํ„ฐ๋ฆฌ
  • ์„œ๋ฒ„๊ฐ€ ๋ฆฌ์†Œ์Šค์˜ URI๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌ
  • POST ๊ธฐ๋ฐ˜ ๋“ฑ๋ก
  • ๋ณต์ˆ˜ ์‚ฌ์šฉ
  • ex) ํšŒ์› ๊ด€๋ฆฌ API
http://api.example.com/user-management/users
http://api.example.com/user-management/users/{id}

 

โ˜‘๏ธ ์Šคํ† ์–ด(Store)

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž์› ์ €์žฅ์†Œ
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค์˜ URI๋ฅผ ์•Œ๊ณ  ๊ด€๋ฆฌ
  • PUT ๊ธฐ๋ฐ˜ ๋“ฑ๋ก
  • ๋ณต์ˆ˜ ์‚ฌ์šฉ
  • ex) ์ •์  ์ปจํ…์ธ  ๊ด€๋ฆฌ, ์›๊ฒฉ ํŒŒ์ผ ๊ด€๋ฆฌ
http://api.example.com/files
http://api.example.com/files/new_file.txt

 

โ˜‘๏ธ ์ปจํŠธ๋กค URI ํ˜น์€ ์ปจํŠธ๋กค๋Ÿฌ(Controller)

  • ๋ฌธ์„œ, ์ปฌ๋ ‰์…˜, ์Šคํ† ์–ด๋กœ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šด ์ถ”๊ฐ€ ํ”„๋กœ์„ธ์Šค ์‹คํ–‰
  • ๋™์‚ฌ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉ
  • ๋™์‚ฌ ์‚ฌ์šฉ
  • ex) GET, POST ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” HTTP FORM ์˜ ๊ฒฝ์šฐ ์ปจํŠธ๋กค URI(๋™์‚ฌ) ๋ฅผ ์‚ฌ์šฉ
http://api.example.com/cart-management/users/{id}/cart/checkout
http://api.example.com/song-management/users/{id}/playlist/play

 

์ผ๊ด€์„ฑ์ด ํ•ต์‹ฌ์ด๋‹ค.

์ผ๊ด€๋œ ๋ฆฌ์†Œ์Šค์™€ URI ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจํ˜ธํ•จ์ด ์ตœ์†Œํ™”๋˜๊ณ  ๊ฐ€๋…์„ฑ๊ณผ ์ง€์†์„ฑ์ด ๊ทน๋Œ€ํ™”๋œ๋‹ค.

 

โ˜‘๏ธ ๊ณ„์ธต ๊ด€๊ณ„ ํ‘œํ˜„์„ ์œ„ํ•ด ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

http://api.example.com/device-management
http://api.example.com/device-management/managed-devices
http://api.example.com/device-management/managed-devices/{id}
http://api.example.com/device-management/managed-devices/{id}/scripts
http://api.example.com/device-management/managed-devices/{id}/scripts/{id}

 

โ˜‘๏ธ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋กœ ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

http://api.example.com/device-management/managed-devices/ /* X */
http://api.example.com/device-management/managed-devices /* O */

 

โ˜‘๏ธ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ํ•˜์ดํ”ˆ์„ ์‚ฌ์šฉํ•˜๋ผ.

// More readable
http://api.example.com/inventory-management/managed-entities/{id}/install-script-location
// Less readable
http://api.example.com/inventory-management/managedEntities/{id}/installScriptLocation

 

โ˜‘๏ธ ์–ธ๋”์Šค์ฝ”์–ด๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €๋‚˜ ํ™”๋ฉด์—์„œ๋Š” `_`๊ฐ€ ๊ฐ€๋ ค์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋ž€์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด `-`์„ ์‚ฌ์šฉํ•œ๋‹ค.

// More readable
http://api.example.com/inventory-management/managed-entities/{id}/install-script-location
// More error prone
http://api.example.com/inventory_management/managed_entities/{id}/install_script_location

 

โ˜‘๏ธ ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

์Šคํ‚ค๋งˆ์™€ ํ˜ธ์ŠคํŠธ์—๋งŒ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์ด ์—†๊ณ , ์ด์™ธ์—๋Š” ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ณ„๋œ๋‹ค.

์•„๋ž˜ 3๊ฐœ์˜ URL์€ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์ด ์—†๋‹ค๋ฉด ๋ชจ๋‘ ๋™์ผํ•˜์ง€๋งŒ 1, 2๋ฒˆ์€ ๋™์ผํ•˜๊ณ  3๋ฒˆ์€ ๊ฐ™์ง€ ์•Š๋‹ค.

http://api.example.org/my-folder/my-doc // 1
HTTP://API.EXAMPLE.ORG/my-folder/my-doc // 2
http://api.example.org/My-Folder/my-doc // 3

 

โ˜‘๏ธ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

// X
http://api.example.com/device-management/managed-devices.xml
// O
http://api.example.com/device-management/managed-devices

 

CRUD ํ•จ์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

URI ๋Š” ์–ด๋– ํ•œ ๋™์ž‘์ด ์ˆ˜ํ–‰๋˜๋Š”์ง€๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.

๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ž‘์—…์€ HTTP Method๋ฅผ ์ด์šฉํ•œ๋‹ค.

HTTP GET http://api.example.com/device-management/managed-devices/{id}  // Get device for given Id
HTTP PUT http://api.example.com/device-management/managed-devices/{id}  // Update device for given Id
HTTP DELETE http://api.example.com/device-management/managed-devices/{id}  // Delete device for given Id

 

ํ•„ํ„ฐ๋ฅผ ์œ„ํ•ด ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ •๋ ฌ, ํ•„ํ„ฐ๋ง, ํŽ˜์ด์ง•์€ ์‹ ๊ทœ API ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋ผ.

http://api.example.com/device-management/managed-devices
http://api.example.com/device-management/managed-devices?region=USA
http://api.example.com/device-management/managed-devices?region=USA&brand=XYZ
http://api.example.com/device-management/managed-devices?region=USA&brand=XYZ&sort=installation-date

 

์ฐธ๊ณ ์ž๋ฃŒ

https://restfulapi.net/resource-naming

 

๋Œ“๊ธ€