REST API

๊น€๋‚จ๊ฒฝยท2022๋…„ 12์›” 2์ผ
0

network

๋ชฉ๋ก ๋ณด๊ธฐ
19/27

์˜๋ฏธ

๐Ÿ’ก ์›น์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ์ž์›(Resource)์„ HTTP URI๋กœ ํ‘œํ˜„ํ•˜๊ณ , HTTP ํ”„๋กœํ† ์ฝœ์„ ํ†ตํ•ด์„œ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ์ •๋ฆฌํ•˜๋Š” ๋ฐฉ์‹

Richardson ์„ฑ์ˆ™๋„ ๋ชจ๋ธ(RMM)

๋‹จ๊ณ„ ๋‚ด์šฉ
0๋‹จ๊ณ„ HTTP ์‚ฌ์šฉ
1๋‹จ๊ณ„ ๊ฐœ๋ณ„ ๋ฆฌ์†Œ์Šค์™€์˜ ํ†ต์‹  ์ค€์ˆ˜
2๋‹จ๊ณ„ HTTP ๋ฉ”์†Œ๋“œ ์›์น™ ์ค€์ˆ˜
3๋‹จ๊ณ„ HATEOAS ์›์น™ ์ค€์ˆ˜
*2๋‹จ๊ณ„๊นŒ์ง€๋งŒ ์ถฉ์กฑํ•ด๋„ ์ข‹์€ API ๋””์ž์ธ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ  HTTP API๋ผ๊ณ  ํ•จ

๐Ÿ“— 0๋‹จ๊ณ„
๋‹จ์ˆœํžˆ HTTP ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ
๋‹จ ํ•˜๋‚˜์˜ endpoint(/appointment)์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉฐ ์ž‘๋™.
๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ body๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด HTTP Method๋Š” POST๋ฅผ ์‚ฌ์šฉ

์š”์ฒญ ์‘๋‹ต
POST /appointment HTTP 1.1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "name" : "a"
ย ย ย ย "date" : "2022-12-02"
}
HTTP 1.1 200 OK
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "slots" : [
ย ย ย ย ย ย {"id" : "1", "name" : "a", "time" : "09:00"}
ย ย ย ย ย ย {"id" : "2", "name" : "a", "time" : "10:00"}
ย ย ย ย ]
}

์š”์ฒญ ์‘๋‹ต
POST /appointment HTTP 1.1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "name" : "a"
ย ย ย ย "time" : "09:00"
ย ย ย ย "patient" : "A"
}
HTTP 1.1 200 OK
//ํ—ค๋” ์ƒ๋žต//

๐Ÿ“— 1๋‹จ๊ณ„
๊ฐœ๋ณ„ ๋ฆฌ์†Œ์Šค์— ๋งž๋Š” endpoint ์‚ฌ์šฉ(/doctors/a)
์š”์ฒญํ•˜๊ณ  ๋ฐ›๋Š” ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์‘๋‹ต์œผ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•จ

์š”์ฒญ ์‘๋‹ต
POST /doctors/a HTTP 1.1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย date : "2022-12-02"
}
HTTP 1.1 200 OK
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "slots" : [
ย ย ย ย ย ย {"id" : "1", "name" : "a", "time" : "09:00"}
ย ย ย ย ย ย {"id" : "2", "name" : "a", "time" : "10:00"}
ย ย ย ย ]
}

์š”์ฒญ ์‘๋‹ต
POST /slots/1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย patient : "A"
}
HTTP 1.1 200 OK
{
ย ย ย ย "appointment" : {
ย ย ย ย ย ย ย ย "slot" : {"id" : "1", "name" : "a", "time" : "09:00"},
ย ย ย ย ย ย ย ย "patient" : "A"
ย ย ย ย }
}

๐Ÿ“— 2๋‹จ๊ณ„
CRUD์— ์ ํ•ฉํ•œ HTTP ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ

์š”์ฒญ ์‘๋‹ต
GET /doctors/a/slots?date=2022-12-02
//ํ—ค๋” ์ƒ๋žต//
HTTP 1.1 200 OK
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "slots" : [
ย ย ย ย ย ย {"id" : "1", "name" : "a", "time" : "09:00"}
ย ย ย ย ย ย {"id" : "2", "name" : "a", "time" : "10:00"}
ย ย ย ย ]
}
* query parameter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ „๋‹ฌ

์š”์ฒญ ์‘๋‹ต
POST /slots/1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย patient : "A"
}
HTTP 1.1 201 Created
Location: /slots/1/appointment
{
ย ย ย ย "appointment" : {
ย ย ย ย ย ย ย ย "slot" : {"id" : "1", "name" : "a", "time" : "09:00"},
ย ย ย ย ย ย ย ย "patient" : "A"
ย ย ย ย }
}

โ—พ๏ธ GET : ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค์ง€ ์•Š๋Š” ์š”์ฒญ์— ์‚ฌ์šฉ
โ—พ๏ธ POST : ์š”์ฒญ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ
โ—พ๏ธ PUT : ์š”์ฒญ๋งˆ๋‹ค ๊ฐ™์€ ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜(๋ฉฑ๋“ฑ์„ฑ), ๊ต์ฒด
โ—พ๏ธ PATCH : ์ˆ˜์ •


๐Ÿ“— 3๋‹จ๊ณ„
2๋‹จ๊ณ„์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‚˜ ์‘๋‹ต์—๋Š” ๋ฆฌ์†Œ์Šค์˜ URI๋ฅผ ํฌํ•จํ•œ ๋งํฌ ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•ด์•ผํ•จ
์ด ๋•Œ ๋งํฌ์š”์†Œ๋Š” ์ƒˆ๋กœ์šด ๋งํฌ๋ฅผ ๋„ฃ์–ด ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ

์š”์ฒญ ์‘๋‹ต
GET /doctors/a/slots?date=2022-12-02
//ํ—ค๋” ์ƒ๋žต//
HTTP 1.1 200 OK
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย "slots" : [
ย ย ย ย ย ย {"id" : "1", "name" : "a", "time" : "09:00"}
ย ย ย ย ย ย {"id" : "2", "name" : "a", "time" : "10:00"}
ย ย ย ย ],
ย ย ย ย "links" : {
ย ย ย ย ย ย "appointment" : {
ย ย ย ย ย ย ย ย "href" : "http://localhost:8080/slots/1",
ย ย ย ย ย ย ย ย "method" : "POST"
ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย }
}

์š”์ฒญ ์‘๋‹ต
POST /slots/1
//ํ—ค๋” ์ƒ๋žต//
{
ย ย ย ย patient : "A"
}
HTTP 1.1 201 Created
Location: /slots/1/appointment
{
ย ย ย ย "appointment" : {
ย ย ย ย ย ย ย ย "slot" : {"id" : "1", "name" : "a", "time" : "09:00"},
ย ย ย ย ย ย ย ย "patient" : "b"
ย ย ย ย },
ย ย ย ย "links" : {
ย ย ย ย ย ย "self" : {
ย ย ย ย ย ย ย ย "href" : "http://localhost:8080/slots/1",
ย ย ย ย ย ย ย ย "method" : "GET"
ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย },
ย ย ย ย ย ย "cancel" : {
ย ย ย ย ย ย ย ย "href" : "http://localhost:8080/slots/1/cancel",
ย ย ย ย ย ย ย ย "method" : "DELETE"
ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย }
}

Open API

๐Ÿ’ก ๋ˆ„๊ตฌ์—๊ฒŒ๋‚˜ ์—ด๋ ค์žˆ์ง€๋งŒ, ์ •ํ•ด์ง„ ์ด์šฉ์ˆ˜์น™์— ๋”ฐ๋ผ ์ œํ•œ(๊ฐ€๊ฒฉ, ์ •๋ณด์˜ ์ œํ•œ ๋“ฑ)์ด ์กด์žฌํ•จ

์˜ˆ) Open Weather Map - ๋‚ ์”จ ์ •๋ณด ์ œ๊ณต, JSONํ˜•ํƒœ๋กœ ์‘๋‹ต

API Key

๐Ÿ’ก API๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ
๐Ÿ’ก ๋กœ๊ทธ์ธํ•œ ์ด์šฉ์ž์—๊ฒŒ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์„ API Key์˜ ํ˜•ํƒœ๋กœ ์ œ๊ณตํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๋•Œ API Key๋ฅผ ์ „๋‹ฌํ•˜๋ฉด, ์‘๋‹ต์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

์ฐธ๊ณ 

REST API ์„ฑ์ˆ™๋„ ๋ชจ๋ธ (Maturity Model)
Richardson ์„ฑ์ˆ™๋„ ๋ชจ๋ธ
์ฝ”๋“œ์Šคํ…Œ์ด์ธ  ๊ต๊ณผ์„œ

profile
๊ธฐ๋ณธ์— ์ถฉ์‹คํ•˜๋ฉฐ ์•ž์œผ๋กœ ๋ฐœ์ „ํ•˜๋Š”

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