Thuta Learning
API Tutorial
BasicWeb Developmentအလယ်အလတ်

REST အကြောင်းအရာ

API Tutorialသင်ခန်းစာ 325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Resource၊ representation နဲ့ endpoint ကို ခွဲခြားရှင်းပြနိုင်ဖို့
  • URL ကို verb မဟုတ်ဘဲ နာမ်စား (plural collection) နဲ့ ဒီဇိုင်းလုပ်နိုင်ဖို့
  • HTTP method ဘယ်ဟာက safe ဖြစ်ပြီး ဘယ်ဟာက idempotent ဖြစ်လဲ၊ retry အတွက် ဘာအဓိပ္ပာယ်လဲ ပြောနိုင်ဖို့
  • Stateless server မှာ request တိုင်းက credential ကိုယ်တိုင် သယ်ရတာ ဘာကြောင့်လဲ ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

REST ဆိုတာ web API တွေကို ခန့်မှန်းလို့ရအောင် ဒီဇိုင်းလုပ်တဲ့ အလေ့အကျင့်တွေရဲ့ အစုအဝေးပါ။ API က စီမံတဲ့ အရာတိုင်း — user၊ order၊ note — ဟာ ကိုယ်ပိုင် လိပ်စာ (URL) ရှိတဲ့ အရာတစ်ခုပါ။ ဖတ်ချင်ရင် အဲဒီလိပ်စာကို GET နဲ့ တောင်းပါ၊ အသစ်ဆောက်ချင်ရင် collection ရဲ့ လိပ်စာကို POST နဲ့ ပို့ပါ၊ ဖျက်ချင်ရင် DELETE သုံးပါ။ /getUser ဒါမှမဟုတ် /deleteOrder လို verb တွေကို ကိုယ်တိုင် မဆန်းဘူး၊ HTTP method က verb ဖြစ်ပြီးသားမို့ပါ။

စာကြည့်တိုက်ကို စဉ်းစားကြည့်ပါ။ စာအုပ်တိုင်းမှာ စင်နံပါတ် (URL) ရှိပါတယ်။ စာကြည့်တိုက်မှူးက စံ request အနည်းငယ်ကို နားလည်ပါတယ် — ကြည့်မယ်၊ ထည့်မယ်၊ အစားထိုးမယ်၊ ဖယ်မယ်။ လူတိုင်း ဒီ request အနည်းငယ်ကိုပဲ သုံးတာမို့ ဘယ်စာကြည့်တိုက်ကို ဝင်ဝင် မေးနည်းကို သိပါတယ်။

Problem → Why → Solution

ပြဿနာ: စည်းမျဉ်းမရှိတဲ့ API နှစ်ခု

Team နှစ်ခုက ဆိုင်တစ်ဆိုင်တည်းအတွက် API တွေ ဆောက်ကြပါတယ်။ Team A က endpoint တွေကို /getAllUsers၊ /createNewUser၊ /removeUserById နဲ့ /user_update လို့ နာမည်ပေးပါတယ်။ Team B ကတော့ /users နဲ့ /users/42 ကိုပဲ ဖော်ပြပြီး ဘာလုပ်မလဲဆိုတာ HTTP method ကို ပြောခိုင်းပါတယ်။ Developer အသစ်တစ်ယောက်က Team B ရဲ့ API ကို ဥပမာတစ်ခုမြင်ရုံနဲ့ ခန့်မှန်းနိုင်ပါတယ်။ Team A ကိုတော့ endpoint တိုင်းကို docs မှာ ဖတ်ရပြီး နာမည်တစ်ခုစီမှာ သေးငယ်တဲ့ ဆုံးဖြတ်ချက်တွေ ပုန်းနေပါတယ် — /user_update က အစားထိုးတာလား ပြင်တာလား၊ နှစ်ခါခေါ်လည်း ဘေးကင်းလား။

ပိုနက်တဲ့ ပြဿနာတစ်ခုလည်း ရှိပါတယ်။ ဈေးဝယ်ခြင်းတောင်းကို server memory ထဲ သိမ်းထားပြီး နောက် request က တခြား server ဆီ ရောက်သွားရင် ခြင်းတောင်း ပျောက်သွားပါတယ်။ ခန့်မှန်းလို့ရတဲ့ resource ဒီဇိုင်းနဲ့ server လိုအပ်တာအားလုံးကို သယ်လာတဲ့ request တွေက ပြဿနာနှစ်ခုလုံးကို အတူ ဖြေရှင်းပေးပါတယ်။

naming.txt
Inconsistent                        Consistent (REST style)
GET  /getAllUsers                   GET    /users
POST /createNewUser                 POST   /users
POST /removeUserById?id=42          DELETE /users/42
GET  /user_update?id=42&name=Bo     PATCH  /users/42

ဘာကြောင့် ထုံးစံက ဉာဏ်ရှိတဲ့ နာမည်ထက် သာတာလဲ

ထုံးစံဆိုတာ ကိုယ်ရေးစရာ၊ ဖတ်စရာ မလိုတဲ့ documentation ပါ။ Resource တိုင်း ပုံစံတူရင် cache နဲ့ proxy တွေလည်း အားကိုးလို့ရပါတယ် — GET ကို safe လို့ သတ်မှတ်ထားလို့ cache က GET response ကို သိမ်းနိုင်ပေမယ့် POST ကိုတော့ ပြန်မပို့သင့်ပါဘူး။ GET နဲ့ data ပြောင်းတဲ့ API က ဒီယူဆချက်တွေကို ချိုးဖောက်ပြီး crawler ဒါမှမဟုတ် link ကို ကြိုဖွင့်တဲ့ browser က link လိုက်ရုံနဲ့ record တွေ ဖျက်ပစ်နိုင်ပါတယ်။

Core concept: resource, representation, endpoint

REST (Representational State Transfer) ဆိုတာ Roy Fielding က ၂၀၀၀ ပြည့်နှစ်က ရေးတဲ့ ဒေါက်တာဘွဲ့ စာတမ်းမှာ ဖော်ပြခဲ့တဲ့ architectural style ပါ။ ဒါက စံနှုန်း ဒါမှမဟုတ် protocol မဟုတ်ဘဲ constraint အစုအဝေးပါ။ API သုံးသူအတွက် အရေးအကြီးဆုံးက client နဲ့ server ခွဲထားတာ၊ request တိုင်း stateless ဖြစ်တာ၊ response တွေကို cache လုပ်လို့ရတာ၊ resource အားလုံးကို uniform interface နဲ့ ရောက်တာတို့ပါ။ နေ့စဉ် စကားပြောမှာ REST API လို့ ပြောရင် များသောအားဖြင့် resource တွေပေါ် စီစဉ်ထားပြီး JSON သုံးတဲ့ HTTP API ကို ဆိုလိုပါတယ်။ တိတိကျကျ ပြောရရင် ဒီလို API အများစုက constraint အချို့ကိုပဲ လိုက်နာပါတယ်၊ အဲဒါလည်း ရပါတယ်။

Resource ဆိုတာ user ဒါမှမဟုတ် order လို API က စီမံတဲ့ အရာတစ်ခုဖြစ်ပြီး URL နဲ့ ဖော်ထုတ်ပါတယ်။ Representation ဆိုတာ အဲဒီ resource ကို အချိန်တစ်ခုမှာ ပြသတဲ့ ပုံစံပါ၊ များသောအားဖြင့် JSON document ဖြစ်ပါတယ်။ User တစ်ယောက်တည်းကို XML ဒါမှမဟုတ် CSV row အဖြစ်လည်း ပြနိုင်ပါတယ်။ Endpoint ဆိုတာ ခေါ်လို့ရတဲ့ URL နဲ့ method ပေါင်းစပ်မှုပါ — GET /users/42 နဲ့ DELETE /users/42 က resource တစ်ခုတည်းအတွက် endpoint နှစ်ခုပါ။ ဒီသုံးခုကို စိတ်ထဲ ခွဲထားပါ — user က resource၊ JSON က representation၊ GET /users/42 က endpoint ပါ။

Collection နဲ့ item တွေက ရိုးရှင်းတဲ့ ပုံစံကို လိုက်ပါတယ်။ /users က collection ဖြစ်ပြီး /users/42 က အဲဒီထဲက item တစ်ခုပါ။ Collection တွေအတွက် plural နာမ်ကို သုံးပါ၊ identifier ကို path ထဲ ထည့်ပါ၊ လုပ်ဆောင်ချက်ကိုတော့ HTTP method က ဖော်ပြပါစေ။ GET က ဖတ်တယ်၊ POST က collection ထဲ item အသစ် ဆောက်တယ်၊ PUT က item ကို အပြည့်အစုံ အစားထိုးတယ်၊ PATCH က တစ်စိတ်တစ်ပိုင်း ပြောင်းတယ်၊ DELETE က ဖယ်တယ်။ ဆက်စပ်မှုတွေ သဘာဝကျကျ nested ဖြစ်နိုင်ပါတယ်၊ ဥပမာ GET /users/42/orders။ ဒါပေမယ့် အလွှာနှစ်ခုထက် မနက်ပါနဲ့၊ URL ဖတ်ရခက်လာလို့ပါ။

Method တွေမှာ HTTP စံ (RFC 9110) က သတ်မှတ်ထားတဲ့ အာမခံချက်တွေလည်း ပါပါတယ်။ Safe method က server ပေါ်မှာ ဘာမှ မပြောင်းပါဘူး — GET, HEAD, OPTIONS က safe ပါ။ Idempotent method ကတော့ တစ်ကြိမ်ပို့ပို့ ဆယ်ကြိမ်ပို့ပို့ server ပေါ် အကျိုးသက်ရောက်မှု တူပါတယ် — GET, PUT, DELETE က idempotent ဖြစ်ပြီး POST မဟုတ်ပါဘူး၊ PATCH ကိုတော့ အာမမခံပါဘူး။ Request တစ်ခု timeout ဖြစ်ပြီး ရောက်မရောက် မသိတဲ့အခါ ဒါက အရေးကြီးပါတယ်။ Idempotent request ကို ပြန်ပို့တာ ဘေးကင်းပေမယ့် POST ကို ပြန်ပို့ရင် order ဒုတိယတစ်ခု ဖြစ်သွားနိုင်လို့ idempotency key လို အပို ကာကွယ်မှု လိုပါတယ်။

နောက်ဆုံးအနေနဲ့ stateless ဆိုတာ server က request တွေကြားမှာ ကိုယ့်ကို မမှတ်ထားဘူးလို့ ဆိုလိုပါတယ်။ Request တိုင်းက server လိုတာကို သယ်လာပါတယ် — credential၊ identifier၊ page နံပါတ်။ ဒါကြောင့် တူညီတဲ့ server အများကြီးကို load balancer နောက်မှာ ထားလို့ရတာဖြစ်ပြီး authorization ကို server memory ထဲမှာ မထားဘဲ call တိုင်းမှာ header ထဲ ထည့်ပို့ရတာလည်း ဒါကြောင့်ပါ။

မှတ်ထားရမယ့် ပုံစံ: URL မှာ နာမ်၊ method မှာ verb
  1. နာမ်ကို ရွေး: /notes
  2. Verb ကို ရွေး: GET, POST, PUT...
  3. Server လိုတာအားလုံး ပို့
  4. Status ကို ဖတ်၊ ပြီးမှ body

URL က "ဘာကို" ပြောပြီး method က "ဘာလုပ်မလဲ" ကို ပြောပါတယ်။

အထွေထွေ ပုံစံ: collection နဲ့ item

text
METHOD /collection            work on the whole collection
METHOD /collection/{id}       work on one item
METHOD /collection/{id}/sub   work on a nested collection

GET    /users           list           POST   /users           create
GET    /users/{id}      read one       PUT    /users/{id}      replace
PATCH  /users/{id}      change a part  DELETE /users/{id}      remove

{id} နေရာမှာ 42 လို တကယ့် identifier ကို ထည့်ပါတယ်။ Collection URL (/users) က ပစ္စည်းအများကို လက်ခံပြီး item URL (/users/42) က တစ်ခုတည်းကို ပြောပါတယ်။ Method ကိုပြောင်းရုံနဲ့ URL အတူတူက လုပ်ဆောင်ချက်မတူ ဖြစ်သွားပါတယ်။

Example: POST နဲ့ PUT ကို နှစ်ခါစီ ပို့ကြည့်မယ်

ဒီ mock server က notes ဆိုတဲ့ resource ကို မှတ်ဉာဏ်ထဲမှာ သိမ်းပါတယ်။ Client က POST /notes ကို အတူတူ နှစ်ကြိမ် ပို့ပြီး PUT /notes/1 ကိုလည်း အတူတူ နှစ်ကြိမ် ပို့ပါတယ်။ ရလဒ်ကို ကြည့်ပါ — POST နှစ်ခါက note နှစ်ခု ဖြစ်စေပြီး PUT နှစ်ခါက note တစ်ခုတည်းကို အတူတူ အခြေအနေမှာ ထားပါတယ်။ ဒါက idempotent ဆိုတာရဲ့ အဓိပ္ပာယ်ပါ။

notes-rest.mjs
import http from "node:http";

// An in-memory "notes" resource: POST creates, PUT replaces, GET lists.
const notes = new Map();
let nextId = 1;

function readJson(req) {
  return new Promise((resolve) => {
    let raw = "";
    req.on("data", (chunk) => (raw += chunk));
    req.on("end", () => resolve(raw ? JSON.parse(raw) : null));
  });
}

function send(res, status, payload) {
  res.writeHead(status, { "Content-Type": "application/json" });
  res.end(JSON.stringify(payload));
}

const server = http.createServer(async (req, res) => {
  const body = await readJson(req);
  const item = req.url.match(/^\/notes\/(\d+)$/);
  if (req.method === "POST" && req.url === "/notes") {
    const note = { id: nextId++, text: body.text };
    notes.set(note.id, note);
    send(res, 201, note);
  } else if (req.method === "PUT" && item) {
    const note = { id: Number(item[1]), text: body.text };
    notes.set(note.id, note);
    send(res, 200, note);
  } else if (req.method === "GET" && req.url === "/notes") {
    send(res, 200, [...notes.values()]);
  } else {
    send(res, 404, { error: "not_found" });
  }
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;

async function call(method, path, payload) {
  const options = { method };
  if (payload) {
    options.headers = { "Content-Type": "application/json" };
    options.body = JSON.stringify(payload);
  }
  const res = await fetch(base + path, options);
  console.log(method, path, "->", res.status, JSON.stringify(await res.json()));
}

await call("POST", "/notes", { text: "buy rice" });
await call("POST", "/notes", { text: "buy rice" });
await call("PUT", "/notes/1", { text: "buy tea" });
await call("PUT", "/notes/1", { text: "buy tea" });
await call("GET", "/notes");

server.close();

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Collection endpointif (req.method === "POST" && req.url === "/notes") {

    Collection URL ကို POST လုပ်ရင် item အသစ် ဆောက်တယ်လို့ ဆိုလိုပါတယ်။ Client က ဘယ် id ကို သုံးမလဲ မသတ်မှတ်ပါဘူး၊ server က ရွေးပေးပါတယ်။

  2. Server-assigned idconst note = { id: nextId++, text: body.text };

    POST တိုင်းက nextId ကို တိုးပြီး note အသစ်တစ်ခု ထုတ်ပေးလို့ နှစ်ခါပို့ရင် note နှစ်ခု ဖြစ်ပါတယ်။ ဒါကြောင့် POST က idempotent မဟုတ်တာပါ။

  3. Item endpoint} else if (req.method === "PUT" && item) {

    PUT /notes/1 က item ကို အမည်ပေးပြီး client က representation အပြည့်အစုံကို ပို့ပါတယ်။ အတူတူ ထပ်ပို့ရင် note 1 ရဲ့ အခြေအနေ အတူတူပဲ ဖြစ်ပါတယ်။

  4. Status 201send(res, 201, note);

    Resource အသစ် ဆောက်ပြီးရင် 201 Created ပြန်တာ ထုံးစံပါ၊ ရှိပြီးသား ကို အစားထိုးတာက 200 ပါ။ အပြည့်အစုံ API တွေက Location header နဲ့ item ရဲ့ URL ကိုပါ ပြန်ပေးတတ်ပါတယ်။

  5. One helperasync function call(method, path, payload) {

    Helper တစ်ခုတည်းကို သုံးတာကြောင့် ခေါ်ဆိုမှုတစ်ခုနဲ့တစ်ခု ကွာတာက method နဲ့ path ပဲ ဖြစ်ပြီး ယှဉ်ကြည့်ရ လွယ်ပါတယ်။

  6. The same call twiceawait call("PUT", "/notes/1", { text: "buy tea" });

    ဒီ line ကို နှစ်ခါ run တာက note တစ်ခုတည်းကိုပဲ ထားပါတယ်။ ရှေ့က POST နှစ်ခါနဲ့ ယှဉ်ကြည့်ပါ၊ အဲဒါက note နှစ်ခု ကျန်ခဲ့ပါတယ်။

You should see
POST /notes -> 201 {"id":1,"text":"buy rice"}
POST /notes -> 201 {"id":2,"text":"buy rice"}
PUT /notes/1 -> 200 {"id":1,"text":"buy tea"}
PUT /notes/1 -> 200 {"id":1,"text":"buy tea"}
GET /notes -> 200 [{"id":1,"text":"buy tea"},{"id":2,"text":"buy rice"}]

Safe နဲ့ idempotent ကို တစ်ချက်ကြည့်

MethodSafeIdempotent
GETဟုတ်ဟုတ်
HEADဟုတ်ဟုတ်
POSTမဟုတ်မဟုတ်
PUTမဟုတ်ဟုတ်
PATCHမဟုတ်အာမမခံ
DELETEမဟုတ်ဟုတ်

Idempotent ဆိုတာ server ရဲ့ အခြေအနေအကြောင်းပါ၊ response အကြောင်း မဟုတ်ပါဘူး။ DELETE /notes/1 ကို ပထမကြိမ်ခေါ်ရင် 204 ပြန်နိုင်ပြီး ဒုတိယကြိမ်မှာ note မရှိတော့လို့ 404 ပြန်နိုင်ပါတယ်။ Status မတူပေမယ့် နှစ်ကြိမ်ပြီးတဲ့အခါ server အခြေအနေက တစ်ကြိမ်ပြီးတဲ့အခါနဲ့ တူတာမို့ DELETE က idempotent ပါပဲ။

GET နဲ့ data မပြောင်းပါနဲ့

GET /deleteUser?id=42 လို endpoint မျိုးကို prefetch လုပ်တဲ့ browser၊ search crawler၊ link preview ကိရိယာတွေက ခေါ်မိနိုင်ပါတယ်။ ဖတ်ရုံသက်သက်ဖြစ်ရမယ့် method က record တစ်ခု ဖျက်လိုက်ရင် ဘေးကင်းမှု အာမခံချက်ကို ချိုးဖောက်တာပါ။ ပြောင်းလဲမှုတိုင်းကို POST, PUT, PATCH ဒါမှမဟုတ် DELETE နဲ့ လုပ်ပါ။

Stateless လုပ်ထုံးလုပ်နည်း

Server အများကြီးကို load balancer နောက်မှာ ထားတဲ့အခါ request တစ်ခုချင်းက ဘယ် server ဆီ ရောက်မလဲ မသိနိုင်ပါဘူး။ Token ကို request တိုင်းမှာ ထည့်ပို့ရင် server တိုင်းက တစ်ယောက်ချင်းကို မမှတ်ထားဘဲ ဖြေနိုင်ပါတယ်။

text
Stateless requests behind a load balancer
-----------------------------------------
 client --GET /cart + token--> load balancer --> server A  (answers)
 client --GET /cart + token--> load balancer --> server B  (answers too)

 The token rides in every request, so no server has to remember the client.

Server က session ကို memory ထဲ သိမ်းတဲ့ ပုံစံ (stateful) လည်း အလုပ်လုပ်ပါတယ်၊ ဒါပေမယ့် server များလာတဲ့အခါ session ကို မျှဝေဖို့ အပိုအလုပ် လိုလာပါတယ်။ ဒါကြောင့် public API အများစုက stateless ပုံစံကို ရွေးကြပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ REST ကို ဘယ်လို သုံးလဲ

ငွေပေးချေမှုကနေ source code hosting အထိ public API အများအပြားက plural နာမ်နဲ့ စံ method တွေကို သုံးပါတယ်၊ ဒါကြောင့် API တစ်ခုကို သင်ပြီးရင် နောက်တစ်ခုကို လွယ်လွယ် ဖတ်နိုင်ပါတယ်။ CDN နဲ့ browser cache တွေက GET response တွေကို သိမ်းထားနိုင်လို့ ပြောင်းလဲမှု မရှိတဲ့ data တွေကို မြန်မြန် ပို့နိုင်ပါတယ်။ Load balancer နောက်က server တွေကို ထပ်တိုးဖို့ လွယ်တာလည်း request တွေ stateless ဖြစ်လို့ပါ။

Retry logic ကိုလည်း method ပေါ် အခြေခံပါတယ်။ HTTP client library တွေနဲ့ gateway တွေက idempotent request တွေကို timeout ဖြစ်ရင် အလိုအလျောက် ပြန်ပို့ပေမယ့် POST ကိုတော့ ပြန်မပို့ဘဲ ထားတတ်ပါတယ်။ ငွေပေးချေမှုလို အရေးကြီးတဲ့ POST တွေမှာ provider အချို့က idempotency key header ကို လက်ခံပါတယ် — client က key တစ်ခု ထည့်ပို့ရင် ထပ်ပို့တာကို server က မူရင်းတစ်ခုတည်းအဖြစ်ပဲ ကိုင်တွယ်ပါတယ်။ ဒါက provider ရဲ့ documentation မှာ ရှိမရှိ အမြဲ စစ်ရမယ့် feature တစ်ခုပါ။ တစ်ခါတလေ လုပ်ဆောင်ချက်က CRUD နဲ့ မကိုက်ရင် POST /orders/42/cancel လို sub-resource action ကို သုံးတာက လက်တွေ့ဆန်တဲ့ အဖြေပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. URL ထဲမှာ verb ထည့်တာ

/getUsers၊ /createOrder၊ /deleteItem လို နာမည်တွေက HTTP method နဲ့ ထပ်နေပါတယ်။ ပြီးတော့ GET /deleteItem လိုမျိုး method နဲ့ မကိုက်တဲ့ ပေါင်းစပ်မှုလည်း ဖြစ်လာတတ်ပါတယ်။ /users နဲ့ /orders/7 လို နာမ်ကိုပဲ URL မှာ ထားပြီး method ကို လုပ်ဆောင်ချက်အဖြစ် သုံးပါ။

2. Timeout ဖြစ်တဲ့ POST ကို မစဉ်းစားဘဲ ပြန်ပို့တာ

POST /orders ကို ပို့ပြီး response မရောက်ရင် order ဆောက်ပြီးသား ဖြစ်နိုင်ပါတယ်၊ ပြန်ပို့ရင် ဒုတိယ order ဖြစ်ပြီး ငွေနှစ်ခါ ကျသွားနိုင်ပါတယ်။ Idempotency key သုံးပါ၊ ဒါမှမဟုတ် ပြန်မပို့ခင် order ရှိမရှိ GET နဲ့ အရင်စစ်ပါ။

3. PUT ကို partial update အဖြစ် သုံးတာ

PUT က item တစ်ခုလုံးကို အစားထိုးတာပါ။ field တစ်ခုတည်း ပို့ရင် ကျန် field တွေ ပျောက်သွားနိုင်ပါတယ်။ တစ်စိတ်တစ်ပိုင်းပဲ ပြောင်းချင်ရင် PATCH ကို သုံးပါ၊ ဒါမှမဟုတ် PUT မှာ representation အပြည့်အစုံကို ပို့ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Collection တွေကို plural နာမ်၊ စာလုံးသေး၊ hyphen သုံးပြီး နာမည်ပေးပါ (/order-items) — singular နဲ့ plural မရောပါနဲ့။
  • Filter, sort နဲ့ page တွေကို path မှာ မဟုတ်ဘဲ query string ထဲ ထည့်ပါ (/orders?status=paid)။
  • POST အောင်မြင်ရင် 201 နဲ့ ဆောက်ပြီးသား item ရဲ့ representation ကို ပြန်ပေးပါ။
  • Nested path ကို အလွှာနှစ်ခုအထိပဲ ထားပါ၊ ပိုနက်ရင် resource တစ်ခုချင်းကို ကိုယ်ပိုင် URL ပေးပါ။

Mini challenge

Mini challenge: request reviewer

"METHOD /path" ပုံစံ request စာကြောင်းတစ်ခုကို လက်ခံပြီး collection ဖြစ်လား item ဖြစ်လား၊ retry လုပ်လို့ရလား ဆိုတာ ပြောပြမယ့် review() function ကို ရေးပါ။ /getUser လို verb URL ကိုတော့ သတိပေးရမယ်။

  • Method အတွက် ADVICE object ထဲမှာ safe/idempotent ဖြစ်မဖြစ်ကို ဖော်ပြတဲ့ စာသား သတ်မှတ်ပါ
  • Query string ကို ဖယ်ပြီး path ရဲ့ နောက်ဆုံး အပိုင်းက ဂဏန်းချည်းပဲ ဆိုရင် item၊ မဟုတ်ရင် collection
  • ပထမ path အပိုင်းက /get, /create, /delete, /update သို့မဟုတ် /remove နဲ့ စပြီး စာလုံးကြီး ဒါမှမဟုတ် _ ဆက်ရင် "warning: verb in path" ပြန်ရမယ်
  • ပေးထားတဲ့ request ငါးခုကို ဒီ ပုံစံနဲ့ print ထုတ်ရမယ်: "<request> -> <kind>, <advice>"
review.mjs
const ADVICE = {
  // TODO: one entry per method: GET, HEAD, OPTIONS, PUT, DELETE, PATCH, POST
};

function review(request) {
  const [method, url] = request.split(" ");
  // TODO: path = url without the query string
  // TODO: return a warning when the first segment looks like /getUser or /createOrder
  // TODO: kind = "item" when the last path segment is only digits, else "collection"
  // TODO: return "<request> -> <kind>, <advice>"
}

// TODO: review "GET /users", "DELETE /users/42", "POST /orders",
//       "PATCH /orders/7" and "GET /getUser?id=3", printing each result

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: GET /users -> collection, safe, retry freely DELETE /users/42 -> item, idempotent, retry freely POST /orders -> collection, not idempotent, use an idempotency key PATCH /orders/7 -> item, not guaranteed idempotent, use an idempotency key GET /getUser?id=3 -> warning: verb in path

အရိပ်အမြွက် ကြည့်ရန်

Query ကို ဖယ်ဖို့ url.split("?")[0] ကို သုံးပါ။ နောက်ဆုံး အပိုင်းကို path.split("/").filter(Boolean).pop() နဲ့ ယူပြီး /^\d+$/ နဲ့ စစ်ပါ။ Verb ရှာဖို့ regular expression /^\/(get|create|delete|update|remove)[A-Z_]/ ကို စမ်းကြည့်ပါ။

အဖြေ ကြည့်ရန်
review.mjs
const ADVICE = {
  GET: "safe, retry freely",
  HEAD: "safe, retry freely",
  OPTIONS: "safe, retry freely",
  PUT: "idempotent, retry freely",
  DELETE: "idempotent, retry freely",
  PATCH: "not guaranteed idempotent, use an idempotency key",
  POST: "not idempotent, use an idempotency key",
};

function review(request) {
  const [method, url] = request.split(" ");
  const path = url.split("?")[0];
  if (/^\/(get|create|delete|update|remove)[A-Z_]/.test(path)) {
    return `${request} -> warning: verb in path`;
  }
  const last = path.split("/").filter(Boolean).pop();
  const kind = /^\d+$/.test(last) ? "item" : "collection";
  return `${request} -> ${kind}, ${ADVICE[method]}`;
}

const requests = [
  "GET /users",
  "DELETE /users/42",
  "POST /orders",
  "PATCH /orders/7",
  "GET /getUser?id=3",
];
for (const request of requests) console.log(review(request));

Quick Quiz 1 / 3

POST /orders ကို ပို့ပြီး response မရောက်ဘဲ timeout ဖြစ်သွားပါတယ်။ ဘေးကင်းဆုံး နောက်အဆင့်က ဘာလဲ။

Quick Quiz 2 / 3

REST နာမည်ပေးပုံနဲ့ အကိုက်ညီဆုံး request က ဘယ်ဟာလဲ။

Quick Quiz 3 / 3

REST API server ကို "stateless" လို့ ပြောရင် ဘာကို ဆိုလိုလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Resource က API စီမံတဲ့ အရာ၊ representation က ပြသတဲ့ ပုံစံ (JSON)၊ endpoint က ခေါ်လို့ရတဲ့ URL နဲ့ method ပေါင်းစပ်မှုပါ။
  • URL မှာ plural နာမ်ကိုထားပြီး (/users, /users/42) method က လုပ်ဆောင်ချက်ကို ပြောပါတယ်။
  • GET, HEAD, OPTIONS က safe ပါ၊ GET, PUT, DELETE က idempotent ဖြစ်ပြီး POST မဟုတ်လို့ ပြန်ပို့ရင် ကာကွယ်မှု လိုပါတယ်။
  • Stateless server အတွက် request တိုင်းက credential နဲ့ လိုအပ်တာအားလုံးကို ကိုယ်တိုင် သယ်ရပါတယ်။

RFC 9110 — HTTP Semantics — IETF

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

REST အကြောင်းအရာ | Thuta Learning