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

API Versioning

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

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

  • URL path, header နဲ့ media type versioning သုံးမျိုးရဲ့ ပုံစံနဲ့ အားသာချက်၊ အားနည်းချက်တွေကို နှိုင်းယှဉ်နိုင်ဖို့
  • ပြောင်းလဲမှုတစ်ခုက additive လား breaking လား ခွဲခြားနိုင်ဖို့
  • Version မပါတဲ့ request အတွက် ဘေးကင်းတဲ့ default (oldest supported) ကို ရွေးနိုင်ဖို့
  • Deprecation၊ Sunset နဲ့ Link header တွေကို response မှာ ထည့်ပြီး client ဘက်က ဖတ်နိုင်ဖို့
  • Client မှာ version ကို ရှင်းရှင်းလင်းလင်း pin လုပ်ပြီး မသိတဲ့ field တွေကို လျစ်လျူရှုနိုင်ဖို့

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

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

စာအုပ်တစ်အုပ်ရဲ့ edition လို့ စဉ်းစားကြည့်ပါ။ စာအုပ်ကို ပြန်ပြင်ရတဲ့အခါ edition 2 လို့ နာမည်ပေးပြီး edition 1 ကို ဆက်ထားရင် edition 1 နဲ့ သင်တန်းပေးနေတဲ့ ဆရာတွေက ရုတ်တရက် ကျောင်းသားတွေရဲ့ စာမျက်နှာနံပါတ်တွေ မှားသွားတာမျိုး မဖြစ်တော့ပါဘူး။ API version ကလည်း အဲဒီလိုပါပဲ — contract ကို ပြောင်းရတဲ့အခါ အသစ်ကို နာမည်ထပ်ပေးလိုက်တာပါ။

API ကို သုံးနေတဲ့ client တွေက သင့်ရဲ့ ထိန်းချုပ်မှုအောက်မှာ မရှိပါဘူး၊ ဖုန်းထဲက app အဟောင်းတွေက မနက်ဖြန်ထိ ပြန်မ update ဖြစ်နိုင်ပါဘူး။ ဒါကြောင့် version က client တွေကို "ဒီ contract ကို ကတိပေးထားတယ်" လို့ ပြောပြပြီး သင့်ဘက်ကိုလည်း အသစ်တွေ ဖန်တီးခွင့်၊ အဟောင်းကို အချိန်ယူပြီး ပိတ်ခွင့် ပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: URL တူတူနဲ့ response ပုံစံ ပြောင်းသွားတာ

မတ်လက release လုပ်ခဲ့တဲ့ mobile app က GET /users/1 ကို ခေါ်ပြီး name field ကို ပြပါတယ်။ Backend team က code ကို သန့်ရှင်းအောင် name ကို firstName နဲ့ lastName နှစ်ခုအဖြစ် ခွဲလိုက်ပါတယ်၊ URL တူတူပဲ။ Deploy ပြီးတာနဲ့ app အဟောင်းတွေက profile screen မှာ crash ဖြစ်ပါတယ်၊ ဒါပေမယ့် သုံးစွဲသူတွေက app store ကနေ update လုပ်ဖို့ စောင့်ရပါတယ်။

ပြဿနာက developer မှားတာ မဟုတ်ပါဘူး၊ client ရဲ့ code မှာ ဘာမှ မမှားပါဘူး။ Server ဘက်က contract ကို တိတ်တိတ်လေး ချိုးဖောက်လိုက်တာပါ။ သင့်ကိုယ်တိုင်ရဲ့ client ဆိုရင် ချက်ချင်း ပြင်လို့ရပေမယ့် တခြားသူတွေရဲ့ client ဆိုရင် မရပါဘူး။

profile.js
// App released in March: written against the original response
async function showProfile(id) {
  const res = await fetch("https://api.example.com/users/" + id);
  const profile = await res.json();
  // after the backend split "name", profile.name is undefined
  return profile.name.toUpperCase();
}

// TypeError: Cannot read properties of undefined (reading 'toUpperCase')

ဘာကြောင့် version မပါရင် ဒီလို ဖြစ်တာလဲ

Version မရှိရင် API ရဲ့ အသစ်နဲ့ အဟောင်းကို ခွဲခြားပြောဖို့ နည်းလမ်း မရှိတော့လို့ ပြောင်းလဲမှုတိုင်းက client အားလုံးကို တပြိုင်နက် သက်ရောက်ပါတယ်။ Version ရှိရင် app အဟောင်းတွေက v1 ကို ဆက်ခေါ်နေနိုင်ပြီး အသစ်တွေက v2 ကို သုံးနိုင်ပါတယ်။ ပြောင်းလဲမှုက မလွဲမသွေ breaking ဖြစ်နေရင် version အသစ်တစ်ခု ဖွင့်ပြီး version အဟောင်းအတွက် ရက်သတ်မှတ်ပါ။

Core concept: contract ကို နာမည်ပေးပြီး ဖြည်းဖြည်း ပိတ်သိမ်းခြင်း

API ဆိုတာ သင်မမြင်နိုင်ဘူး၊ မကြာခဏ update လုပ်ခိုင်းလို့လည်း မရတဲ့ program တွေနဲ့ လုပ်ထားတဲ့ contract တစ်ခုပါ။ ပြီးခဲ့တဲ့ နွေဦးက release လုပ်တဲ့ mobile app က ဖုန်းထောင်ချီမှာ ရှိနေဆဲဖြစ်နိုင်ပြီး partner တစ်ဦးရဲ့ ညဘက် integration ကို နှစ်နှစ်လောက် မကိုင်ထားတာလည်း ရှိနိုင်ပါတယ်။ သူတို့အောက်က response ပုံစံ ပြောင်းသွားရင် ပျက်ပါတယ်၊ ပြီးတော့ စိတ်ဆိုးတဲ့ သုံးစွဲသူတွေဆီကမှ သိရပါတယ်။ Versioning ဆိုတာ contract ရဲ့ ပုံစံတစ်ခုချင်းစီကို နာမည်ပေးထားတာပါ — ဒါမှ အဟောင်းကို မှီခိုနေသူတွေကို အံ့ဩမသွားစေဘဲ API ကို တိုးတက်အောင် လုပ်လို့ရပါတယ်။

Version ကို ထည့်တဲ့နေရာ သုံးမျိုး အသုံးများပါတယ်။ URL path ထဲမှာ (/v2/users/42) ထည့်ရင် အမြင်သာဆုံးပါ — route လုပ်ရ၊ log ထုတ်ရ၊ cache လုပ်ရ၊ curl နဲ့ စမ်းရ လွယ်ပါတယ်။ Header ထဲမှာ (API-Version: 2၊ ဒါမှမဟုတ် Stripe ရဲ့ Stripe-Version လို ရက်စွဲတန်ဖိုး) ထည့်ရင် URL သန့်ပေမယ့် version က ဖြတ်သန်းစမ်းသပ်တဲ့အခါ မမြင်ရပါဘူး။ Media type ထဲမှာ (Accept: application/vnd.example.v2+json) ထည့်ရင် HTTP content negotiation နဲ့ အနီးဆုံးဖြစ်ပေမယ့် browser မှာ စမ်းရ အခက်ဆုံးပါ။ ဘယ်တစ်ခုမှ အမြဲမှန်တာ မဟုတ်ပါဘူး။ တစ်ခုရွေးပြီး နေရာတိုင်းမှာ တူအောင်သုံးပါ၊ ပြီးတော့ version မပါတဲ့ request ကို ဘာလို့ သတ်မှတ်မလဲ ဆုံးဖြတ်ပါ — ဘေးကင်းတဲ့ အဖြေက အသစ်ဆုံး မဟုတ်ဘဲ ထောက်ပံ့ထားတဲ့ အဟောင်းဆုံး contract ပါ။

ပြောင်းလဲမှုတိုင်းက version အသစ် မလိုပါဘူး။ Optional response field ထပ်ထည့်တာက client တွေက မသိတဲ့ field ကို လျစ်လျူရှုရင် အများအားဖြင့် အဆင်ပြေပါတယ်။ Field ဖယ်တာ ဒါမှမဟုတ် နာမည်ပြောင်းတာ၊ type ပြောင်းတာ၊ တန်ဖိုးရဲ့ အဓိပ္ပာယ် ပြောင်းတာ၊ required request parameter အသစ်ထည့်တာ၊ validation ပိုတင်းတာတွေက breaking change တွေပါ။ ပိုသိမ်မွေ့တဲ့ ပြောင်းလဲမှုတွေလည်း client ကို ချိုးပါတယ် — exhaustive switch က မမျှော်လင့်ထားတဲ့ enum value အသစ်၊ default page size ပြောင်းတာ၊ sort order ပြောင်းတာ၊ error format အသစ်တို့ပါ။ စမ်းသပ်နည်းက ရိုးရိုးပါပဲ: မှန်ကန်ပြီး ရှိပြီးသား client တစ်ခု ကျသွားနိုင်လား? ကျနိုင်ရင် breaking change ပါ။

Version တစ်ခုကို ပိတ်သိမ်းတာက အချိန်တစ်ချက်ထဲ အဖြစ်အပျက် မဟုတ်ဘဲ လုပ်ငန်းစဉ်တစ်ခုပါ။ ကြေညာပါ၊ ပြီးတော့ version အဟောင်းက ထွက်တဲ့ response တိုင်းမှာ Deprecation နဲ့ Sunset header တွေနဲ့ migration guide ဆီ Link တစ်ခု ထည့်ပါ။ ဘယ် client တွေက ဆက်ခေါ်နေသေးလဲ တိုင်းတာပြီး အများဆုံးသုံးသူတွေကို တိုက်ရိုက်ဆက်သွယ်ပါ၊ ထောက်ပံ့ထားတဲ့ version တိုင်းအတွက် contract test တွေ ဆက် run ပါ။ Sunset ရက်ကျော်သွားရင် endpoint က တိတ်တိတ်ဆိတ်ဆိတ် မတူတဲ့ data ပြန်မယ့်အစား 410 Gone နဲ့ ဖြေပါ။

Client ဘက်မှာလည်း တာဝန်ရှိပါတယ်။ Default ကို မယုံဘဲ request တိုင်းမှာ version ကို ရှင်းရှင်းလင်းလင်း pin လုပ်ပါ၊ Deprecation ဒါမှမဟုတ် Sunset header တွေ ပေါ်လာရင် log ထုတ်ပါ ဒါမှမဟုတ် alert ပေးပါ၊ ပြီးတော့ မသိတဲ့ field တွေကို လျစ်လျူရှုပြီး response ကို သည်းခံစွာ ဖတ်ပါ။ ဒီလိုလုပ်ရင် upgrade က version အဟောင်း ပိတ်တဲ့နေ့ မဟုတ်ဘဲ သင့်အချိန်ဇယားအတိုင်း ဖြစ်ပါလိမ့်မယ်။

မှတ်ထားရမယ့် ပုံစံ
  1. Client က version ကို pin
  2. Server က အဲဒီ contract ဆီ route
  3. Version အဟောင်းကို Deprecation ပြ
  4. Sunset ရက်၊ ပြီးတော့ 410 Gone

Version တိုင်းမှာ အစရှိသလို အဆုံးလည်း ရှိရပါမယ်၊ အဲဒီအဆုံးကို client တွေက ကြိုမြင်ရမယ်။

အထွေထွေ ပုံစံ: version နေရာနဲ့ ပိတ်သိမ်းချက်

text
# Pick ONE place for the version and keep it everywhere
URL path:    GET /v2/users/42
Header:      GET /users/42     API-Version: 2
Media type:  GET /users/42     Accept: application/vnd.example.v2+json

# Retiring an old version: say so in every response it sends
Deprecation: @<unix seconds when it was deprecated>
Sunset: <HTTP-date after which it stops working>
Link: <migration guide URL>; rel="deprecation"

အပေါ်ပိုင်းက version ကို ထည့်နိုင်တဲ့ နည်းလမ်း သုံးမျိုးပါ၊ တစ်မျိုးတည်း ရွေးပါ။ အောက်ပိုင်းက version အဟောင်းရဲ့ response တိုင်းမှာ ထည့်ရမယ့် header သုံးခုပါ — ဘယ်တုန်းက deprecate လုပ်ခဲ့လဲ၊ ဘယ်ရက်ကျော်ရင် မလုပ်တော့လဲ၊ ဘယ်မှာ migrate လုပ်ရမလဲ။

Example: URL နဲ့ media type version နှစ်ခုလုံးကို server တစ်ခုထဲမှာ

ဒီ example မှာ internal model တစ်ခုတည်းကို serializer နှစ်ခုက version နှစ်မျိုးအဖြစ် ပြောင်းပေးပါတယ်။ v1 က name တစ်ခုတည်း၊ v2 က firstName နဲ့ lastName ခွဲထားပါတယ်။ Server က /v1/users/1 နဲ့ /v2/users/1 (URL versioning) ကို ပံ့ပိုးပြီး /users/1 ကို Accept: application/vnd.example.v2+json နဲ့ ခေါ်ရင်လည်း (media-type versioning) အလုပ်လုပ်ပါတယ်။

v1 ကို deprecated အဖြစ် မှတ်ထားလို့ v1 response တိုင်းမှာ Deprecation, Sunset နဲ့ Link header တွေ ပါပါတယ်၊ client က အဲဒါတွေကို မြင်တာနဲ့ warning ရေးပါတယ်။ Version မပါတဲ့ request က v1 ကို ရပါတယ် — default က အဟောင်းဆုံး contract ပါ။ မသိတဲ့ version ကတော့ URL မှာ 404၊ Accept မှာ 406 Not Acceptable ပါ။

versions.mjs
import http from "node:http";

// One internal model, two public representations of it.
const user = { id: 1, firstName: "Aye", lastName: "Chan" };
const representations = {
  1: (u) => ({ id: u.id, name: u.firstName + " " + u.lastName }),
  2: (u) => ({ id: u.id, firstName: u.firstName, lastName: u.lastName }),
};
const DEPRECATED = {
  1: { since: "@1767225600", sunset: "Fri, 15 Jan 2027 00:00:00 GMT" },
};

function send(res, version, via) {
  const render = representations[version];
  if (!render) {
    res.writeHead(via === "path" ? 404 : 406, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ error: "unsupported_version" }));
    return;
  }
  const headers = { "Content-Type": "application/vnd.example.v" + version + "+json" };
  if (via === "media-type") headers.Vary = "Accept";
  const info = DEPRECATED[version];
  if (info) {
    headers.Deprecation = info.since;
    headers.Sunset = info.sunset;
    headers.Link = '<https://api.example.com/docs/migrate-to-v2>; rel="deprecation"';
  }
  res.writeHead(200, headers);
  res.end(JSON.stringify(render(user)));
}

const server = http.createServer((req, res) => {
  const byPath = req.url.match(/^\/v(\d+)\/users\/1$/);
  if (byPath) return send(res, Number(byPath[1]), "path");
  if (req.url === "/users/1") {
    const accept = req.headers.accept ?? "";
    const wanted = accept.match(/application\/vnd\.example\.v(\d+)\+json/);
    const version = wanted ? Number(wanted[1]) : 1; // default: the oldest contract
    return send(res, version, "media-type");
  }
  res.writeHead(404).end();
});

await new Promise((resolve) => server.listen(0, resolve));
const base = "http://127.0.0.1:" + server.address().port;

async function get(label, path, accept) {
  const res = await fetch(base + path, accept ? { headers: { Accept: accept } } : {});
  console.log(label + ": " + res.status + " " + (await res.text()));
  if (res.status === 200) {
    console.log("  content-type: " + res.headers.get("content-type"));
  }
  if (res.headers.has("deprecation")) {
    console.log("  warning: deprecated, sunset " + res.headers.get("sunset"));
  }
}

await get("URL v1", "/v1/users/1");
await get("URL v2", "/v2/users/1");
await get("URL v9", "/v9/users/1");
await get("Accept v2", "/users/1", "application/vnd.example.v2+json");
await get("no version", "/users/1");
await get("Accept v9", "/users/1", "application/vnd.example.v9+json");
server.close();

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

  1. One modelconst user = { id: 1, firstName: "Aye", lastName: "Chan" };

    Server ထဲမှာ ဒေတာ model တစ်ခုတည်း ရှိပါတယ်။ Version တိုင်းအတွက် model ကော်ပီ မထားဘဲ response ပုံစံကိုပဲ version အလိုက် ခွဲထားတာမို့ ပြင်ရတာ လွယ်ပါတယ်။

  2. v1 serializer1: (u) => ({ id: u.id, name: u.firstName + " " + u.lastName }),

    v1 ရဲ့ contract မှာ name တစ်ခုတည်း ပါတာကြောင့် firstName နဲ့ lastName ကို ပြန်ပေါင်းပေးပါတယ်။ Internal model ကို ပြောင်းသွားလည်း app အဟောင်းတွေက ဆက်အလုပ်လုပ်ပါတယ်။

  3. URL versionconst byPath = req.url.match(/^\/v(\d+)\/users\/1$/);

    Path ထဲက v နောက်က ဂဏန်းကို regular expression နဲ့ ဖတ်ပါတယ်။ ဒီ example အတွက်ပဲ ရိုးရှင်းအောင် /users/1 တစ်ခုတည်း route လုပ်ထားတာပါ၊ တကယ့် router မှာတော့ /v:version/users/:id လို parameter သုံးပါတယ်။

  4. Safe defaultconst version = wanted ? Number(wanted[1]) : 1;

    Accept header မှာ version မပါရင် 1 ကို ရွေးပါတယ်။ Latest ကို default လုပ်ရင် version header မသိတဲ့ client အဟောင်းတွေ ချက်ချင်း ပျက်သွားမှာ ဖြစ်လို့ အဟောင်းဆုံး contract ကို ရွေးတာပါ။

  5. Deprecation signalheaders.Deprecation = info.since;

    Version အဟောင်းရဲ့ response တိုင်းမှာ ဘယ်တုန်းက deprecate လုပ်လဲ ထည့်ပေးပါတယ်၊ ပြီးတော့ Sunset နဲ့ Link ကိုလည်း ဆက်ထည့်ပါတယ်။ Client ဘက်က ဒီ header တွေကို ဖတ်ပြီး warning ပြပါတယ်။

  6. Varyif (via === "media-type") headers.Vary = "Accept";

    Media-type versioning မှာ URL တူတူပေမယ့် Accept ပေါ်မူတည်ပြီး response ကွာတာမို့ Vary: Accept ထည့်ပါတယ်၊ ဒါမှ cache က v1 response ကို v2 ခေါ်သူကို မပေးမိပါဘူး။

You should see
URL v1: 200 {"id":1,"name":"Aye Chan"}
  content-type: application/vnd.example.v1+json
  warning: deprecated, sunset Fri, 15 Jan 2027 00:00:00 GMT
URL v2: 200 {"id":1,"firstName":"Aye","lastName":"Chan"}
  content-type: application/vnd.example.v2+json
URL v9: 404 {"error":"unsupported_version"}
Accept v2: 200 {"id":1,"firstName":"Aye","lastName":"Chan"}
  content-type: application/vnd.example.v2+json
no version: 200 {"id":1,"name":"Aye Chan"}
  content-type: application/vnd.example.v1+json
  warning: deprecated, sunset Fri, 15 Jan 2027 00:00:00 GMT
Accept v9: 406 {"error":"unsupported_version"}

Versioning နည်းလမ်းတွေကို ယှဉ်ကြည့်ခြင်း

နည်းလမ်းဥပမာအဓိက အားသာ/အားနည်းချက်
URL path/v2/users/42မြင်သာပြီး စမ်းရလွယ်၊ ဒါပေမယ့် URL တွေ ပွားတတ်
Custom headerAPI-Version: 2URL သန့်၊ ဒါပေမယ့် ရှာတွေ့ဖို့ ခက်
Media typeAccept: ...v2+jsonHTTP နဲ့ အနီးဆုံး၊ ဒါပေမယ့် tooling ခက်
Query parameter/users/42?version=2ရိုးရှင်း၊ ဒါပေမယ့် cache key ရောထွေးနိုင်

ဘယ်နည်းလမ်းကို ရွေးရွေး နေရာတိုင်းမှာ တူအောင် လိုက်နာပါ၊ တစ်ခုထက် မကသုံးရင် client တွေ ရှုပ်ပါတယ်။ Provider တချို့က major version ကို URL မှာ ထားပြီး ပိုသေးတဲ့ ပြောင်းလဲမှုတွေကို ရက်စွဲတန်ဖိုးတဲ့ header နဲ့ ခွဲပါတယ်၊ ဥပမာ GitHub REST API က X-GitHub-Api-Version header ကို သုံးပါတယ်။ Provider ရဲ့ လက်ရှိ စည်းမျဉ်းကို သူတို့ documentation မှာ စစ်ပါ။

Additive နဲ့ breaking ကို စက်နဲ့ စစ်ကြည့်ခြင်း

ပြောင်းလဲမှုတွေကို လူကသာ မကြည့်ဘဲ စက်နဲ့လည်း စစ်လို့ရပါတယ်။ အောက်က function က response field နာမည်နဲ့ type ကို နှိုင်းယှဉ်ပြီး field ဖယ်တာ၊ type ပြောင်းတာကို breaking လို့ သတ်မှတ်ပြီး field အသစ်ထည့်တာကို additive လို့ သတ်မှတ်ပါတယ်။ ဒါက ဖွဲ့စည်းပုံကိုပဲ စစ်နိုင်တာမို့ enum value အသစ်၊ default ပြောင်းတာလို အပြုအမူ ပြောင်းလဲမှုတွေကိုတော့ မမြင်ပါဘူး၊ အဲဒါတွေကို review နဲ့ test တွေက ဖမ်းရပါမယ်။ OpenAPI spec နှစ်ခုကို နှိုင်းယှဉ်တဲ့ tool တွေက ဒီအယူအဆကို ပိုပြည့်စုံအောင် လုပ်ပေးပါတယ်။

diff-contracts.mjs
function diffContracts(before, after) {
  const breaking = [];
  const additive = [];
  for (const [field, type] of Object.entries(before)) {
    if (!(field in after)) breaking.push(field + " removed");
    else if (after[field] !== type) {
      breaking.push(field + " changed from " + type + " to " + after[field]);
    }
  }
  for (const field of Object.keys(after)) {
    if (!(field in before)) additive.push(field + " added");
  }
  return { breaking, additive };
}

const v1 = { id: "number", name: "string" };
const candidates = {
  "v1 + avatarUrl": { id: "number", name: "string", avatarUrl: "string" },
  "v1 with string id": { id: "string", name: "string" },
  "v1 split name": { id: "number", firstName: "string", lastName: "string" },
};

for (const [label, shape] of Object.entries(candidates)) {
  const { breaking, additive } = diffContracts(v1, shape);
  const verdict = breaking.length ? "needs a new version" : "safe to ship in v1";
  console.log(label);
  if (breaking.length) console.log("  breaking: " + breaking.join(", "));
  if (additive.length) console.log("  additive: " + additive.join(", "));
  console.log("  verdict: " + verdict);
}
text
v1 + avatarUrl
  additive: avatarUrl added
  verdict: safe to ship in v1
v1 with string id
  breaking: id changed from number to string
  verdict: needs a new version
v1 split name
  breaking: name removed
  additive: firstName added, lastName added
  verdict: needs a new version

Version တစ်ခုကို ပိတ်သိမ်းတဲ့ အဆင့်များ

1. ကြေညာပြီး ရက်သတ်မှတ်

Changelog, email နဲ့ dashboard မှာ ကြေညာပြီး Sunset ရက်ကို ရှင်းရှင်းလင်းလင်း ပြောပါ။ Client တွေ ရွှေ့ဖို့ လအနည်းငယ်၊ တချို့ကိစ္စမှာ တစ်နှစ်လောက် ပေးပါ။

2. Response တိုင်းမှာ header ထည့်

Deprecation header က ဘယ်တုန်းက deprecate လုပ်လဲ ဆိုတာကို ဆိုလိုပြီး လက်ရှိ specification တွေမှာ @ နောက်က Unix seconds နဲ့ ရေးပါတယ် (ပိုဟောင်းတဲ့ API တွေက Deprecation: true ကို ပို့ကြပါတယ်)။ Sunset (RFC 8594) က မလုပ်တော့မယ့် HTTP-date ကို ပြောပြီး Link: rel="deprecation" က migration guide ဆီ ညွှန်ပါတယ်။

3. အသုံးပြုမှုကို တိုင်းတာ

Version တစ်ခုချင်းစီ၊ API key တစ်ခုချင်းစီအလိုက် request အရေအတွက်ကို log ထုတ်ပါ။ ရက်ကုန်ခါနီး ဆက်သုံးနေသူတွေကို တိုက်ရိုက်ဆက်သွယ်လို့ရပါတယ်။

4. Brownout နဲ့ sunset

Brownout ဆိုတာ version အဟောင်းကို ခဏတာ ရည်ရွယ်ချက်ရှိရှိ ပိတ်ပြီး မကြေညာထားသူတွေကို သတိပေးတာပါ။ Sunset ရက်ပြည့်ရင် 410 Gone ပြန်ပါ။

text
HTTP/1.1 200 OK
Content-Type: application/vnd.example.v1+json
Deprecation: @1767225600
Sunset: Fri, 15 Jan 2027 00:00:00 GMT
Link: <https://api.example.com/docs/migrate-to-v2>; rel="deprecation"

ကိုယ်တိုင်ကြည့်ချင်ရင် curl နဲ့ Accept header ပါအောင် ခေါ်ပြီး response header တွေကို ဖတ်ပါ။ Client code ထဲမှာလည်း ဒီ header တွေကို စောင့်ကြည့်ထားရင် version ပိတ်မယ့်နေ့မတိုင်ခင် သတိပေးခံရပါလိမ့်မယ်။

bash
curl -i https://api.example.com/users/1 \
  -H "Accept: application/vnd.example.v1+json"

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

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

Mobile app တွေအတွက် versioning က မရှောင်လွှဲနိုင်ပါဘူး၊ app store review ကြောင့် သုံးစွဲသူအားလုံး တစ်ပြိုင်နက် update မလုပ်နိုင်လို့ပါ။ Payment provider တွေလို တချို့က account တစ်ခုစီကို ရက်စွဲပါတဲ့ version တစ်ခုမှာ pin ထားပေးပြီး ပိုင်ရှင်က မိမိအချိန်မှာ ရွေ့ပြောင်းခွင့်ပေးပါတယ်။ Internal API တွေမှာလည်း ဒီပုံစံက အသုံးဝင်ပါတယ် — team နှစ်ခုက တပြိုင်နက် deploy မလုပ်နိုင်ရင် version နှစ်ခုကို ယှဉ်ပြီး ထားနိုင်ပါတယ်။

v1 ကနေ v2 သို့ user schema ပြောင်းတဲ့ project တစ်ခုမှာ compatibility table တစ်ခုရေးပါ (field တစ်ခုချင်းစီ ဘာဖြစ်သွားလဲ)၊ v2 model အပေါ်မှာ v1 adapter တစ်ခု ထားပါ၊ migration timeline ကို ကြေညာပါ၊ client ဘက်က consumer contract test တွေကို CI မှာ run ပါ။ Request တိုင်းရဲ့ log မှာ version ကို မှတ်ထားရင် "v1 ကို ဘယ်သူ ဆက်သုံးနေလဲ" ဆိုတာ မေးခွန်းတစ်ခုနဲ့ ဖြေလို့ရပါတယ်။ OpenAPI spec ကို version တစ်ခုစီအတွက် သီးသန့်ထားပြီး နောက်သင်ခန်းစာမှာ ပြမယ့်အတိုင်း breaking-change diff ကို CI မှာ ထည့်လို့ရပါတယ်။

အမှားများ

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

1. Version မပါဘဲ နေရာမှာပဲ ပြောင်းတာ

"Client က ကျွန်တော်တို့ပဲ" ဆိုပြီး field နာမည်ပြောင်းတာ၊ type ပြောင်းတာ မလုပ်ပါနဲ့၊ app အဟောင်း၊ cache၊ တခြား team ရဲ့ script တွေ ရှိနေဆဲပါ။ Breaking change ဆိုရင် version အသစ်လိုပါတယ်။

2. Field အသစ်ထည့်တာ အမြဲ ဘေးကင်းတယ်လို့ ထင်တာ

Client တစ်ချို့က response ကို strict schema နဲ့ စစ်ပြီး မသိတဲ့ field ပါရင် reject လုပ်ပါတယ်၊ exhaustive switch က enum value အသစ်မှာ ကျပါတယ်။ ဒါကြောင့် client ကို မသိတဲ့ field ကို လျစ်လျူရှုအောင် ရေးပြီး သင့်ဘက်ကလည်း enum အသစ်ကို သတိထားပါ။

3. Version မပါရင် latest ကို default လုပ်တာ

Header မသုံးတတ်တဲ့ client အဟောင်းတွေက version မပို့ပါဘူး၊ latest ကို default လုပ်ထားရင် v3 ထွက်တဲ့နေ့ သူတို့ အကုန် ပျက်ပါမယ်။ အဟောင်းဆုံး ထောက်ပံ့တဲ့ contract ကို default လုပ်ပါ၊ ဒါမှမဟုတ် 400 နဲ့ ရှင်းရှင်း ပယ်ချပါ။

4. အသုံးပြုမှု မတိုင်းဘဲ ပိတ်တာ

ကြေညာချက်ပဲ ပို့ပြီး ရက်ပြည့်တာနဲ့ ပိတ်လိုက်ရင် မဖတ်ခဲ့တဲ့ client တွေကို ရုတ်ချည်း ချိုးပါတယ်။ Version အလိုက် log၊ Deprecation header၊ brownout တို့နဲ့ ကြိုသတိပေးပြီး ဆက်သုံးနေသူ ကျန်သေးရင် ရက်ဆွဲပါ။

Best Practices

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

  • Additive ပြောင်းလဲမှုတွေကို version တစ်ခုထဲမှာပဲ ဆက်ထားပြီး breaking change ဖြစ်မှသာ version အသစ်ဖွင့်ပါ၊ ဒါမှ version အရေအတွက် မပွားပါဘူး။
  • Internal model တစ်ခုတည်းကို version အလိုက် serializer တွေနဲ့ ထုတ်ပြပါ၊ business logic ကို version အလိုက် ကော်ပီ မလုပ်ပါနဲ့။
  • Client ဘက်မှာ version ကို constant တစ်နေရာတည်းမှာ pin လုပ်ပြီး Deprecation/Sunset header တွေကို logging ထဲ ထည့်ပါ။

Mini challenge

Mini challenge: Accept header ကနေ version ရွေးခြင်း

parseAcceptVersion(accept, supported, fallback) function ကို ရေးပါ။ Accept header ထဲမှာ application/vnd.example.vN+json နဲ့ q value တွေ ပါနိုင်ပါတယ်။ အကြိုက်ဆုံး (q အမြင့်ဆုံး) ကနေ စပြီး ထောက်ပံ့တဲ့ version ကို ပြန်ပေးပါ၊ application/json နဲ့ */* က fallback ကို ရပါမယ်၊ ဘာမှ မကိုက်ရင် null (server က 406 ပို့မယ်) ပြန်ပါ။

  • Accept header မပါရင် fallback ကို ပြန်ရမယ်
  • Comma နဲ့ ခွဲပြီး q (မပါရင် 1) ကို ဖတ်ပြီး q အမြင့်ဆုံးကနေ စမ်းရမယ်၊ q တူရင် ရေးထားတဲ့ အစဉ်အတိုင်း
  • q=0 ကို ကျော်ရမယ်၊ ထောက်ပံ့တဲ့ vnd.example.vN+json version ကို ပြန်ရမယ်၊ မထောက်ပံ့တာကို ကျော်ရမယ်
  • application/json နဲ့ */* က fallback ပြန်ရမယ်၊ ဘာမှ မကိုက်ရင် null ပြန်ရမယ်
  • supported = [1, 2]၊ fallback = 1 နဲ့ ဥပမာ ခုနစ်ခုကို print လုပ်ရမယ်
accept-version.mjs
// Pick an API version from an Accept header such as
//   "application/vnd.example.v3+json, application/vnd.example.v2+json;q=0.8"
// Return a supported version, the fallback, or null (the server would answer 406).

function parseAcceptVersion(accept, supported, fallback) {
  // TODO 1: no Accept header at all -> return fallback
  // TODO 2: split on commas and read each media type with its q value (default 1)
  // TODO 3: order the ranges by q, highest first, keeping the written order on ties
  // TODO 4: skip q=0; return the number from a supported application/vnd.example.vN+json
  // TODO 5: application/json and */* mean "any contract": return fallback
  // TODO 6: nothing acceptable -> return null
}

// TODO: with supported = [1, 2] and fallback = 1, print the result for seven values

ရလဒ်: ဥပမာ ခုနစ်ခုအတွက် ရလဒ် 1, 2, 2, 1, null, null, 1 ဆိုပြီး ဖော်ပြပါမယ်: (no Accept header) -> 1 application/vnd.example.v2+json -> 2 application/vnd.example.v3+json, application/vnd.example.v2+json;q=0.8 -> 2 application/json -> 1 application/vnd.example.v9+json -> null text/html -> null application/vnd.example.v2+json;q=0, application/json -> 1

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

Header တစ်ခုချင်းစီကို ";" နဲ့ ထပ်ခွဲပြီး q=... ကို regular expression နဲ့ ဖတ်ပါ။ sort လုပ်တဲ့အခါ q ကို အရင်နှိုင်းယှဉ်၊ တူရင် မူလ index နဲ့ နှိုင်းယှဉ်ပါ။ Version မကိုက်ရင် return မလုပ်ဘဲ loop ဆက်သွားရပါမယ်။

အဖြေ ကြည့်ရန်
accept-version.mjs
const MEDIA = /^application\/vnd\.example\.v(\d+)\+json$/;

function parseAcceptVersion(accept, supported, fallback) {
  if (!accept) return fallback;
  const ranges = accept.split(",").map((part, index) => {
    const [type, ...params] = part.split(";").map((piece) => piece.trim());
    const q = params.map((param) => param.match(/^q=([0-9.]+)$/)).find(Boolean);
    return { type: type.toLowerCase(), q: q ? Number(q[1]) : 1, index };
  });
  ranges.sort((a, b) => b.q - a.q || a.index - b.index);

  for (const { type, q } of ranges) {
    if (q === 0) continue;
    const match = type.match(MEDIA);
    if (match) {
      const version = Number(match[1]);
      if (supported.includes(version)) return version;
    } else if (type === "application/json" || type === "*/*") {
      return fallback;
    }
  }
  return null;
}

const supported = [1, 2];
const cases = [
  undefined,
  "application/vnd.example.v2+json",
  "application/vnd.example.v3+json, application/vnd.example.v2+json;q=0.8",
  "application/json",
  "application/vnd.example.v9+json",
  "text/html",
  "application/vnd.example.v2+json;q=0, application/json",
];
for (const accept of cases) {
  const shown = accept ?? "(no Accept header)";
  console.log(shown.padEnd(72) + " -> " + parseAcceptVersion(accept, supported, 1));
}

Quick Quiz 1 / 3

Version အသစ် မဖွင့်ဘဲ v1 ထဲမှာပဲ ထုတ်လွှင့်လို့ ရတဲ့ ပြောင်းလဲမှုက ဘယ်ဟာလဲ။

Quick Quiz 2 / 3

Header တွေ မသုံးတတ်တဲ့ client အဟောင်းတစ်ခုက version ပါတဲ့ အချက်အလက် မပို့ဘဲ GET /users/1 ကို ခေါ်ပါတယ်။ ဘယ် default က သူ့ကို ကာကွယ်ပေးလဲ။

Quick Quiz 3 / 3

Response ထဲက Sunset: Fri, 15 Jan 2027 00:00:00 GMT header က client ကို ဘာပြောတာလဲ။

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

  • Version က contract ကို နာမည်ပေးတာပါ။ URL path၊ header၊ media type ထဲက တစ်ခုရွေးပြီး နေရာတိုင်းမှာ တူအောင်သုံးပါ၊ version မပါတဲ့ request အတွက် အဟောင်းဆုံး ထောက်ပံ့တဲ့ contract ကို default လုပ်ပါ။
  • Optional field ထပ်ထည့်တာက additive၊ field ဖယ်တာ၊ type ပြောင်းတာ၊ required parameter အသစ်၊ error format ပြောင်းတာတွေက breaking ဖြစ်တတ်ပါတယ်။
  • Version အဟောင်းကို Deprecation၊ Sunset (HTTP-date) နဲ့ Link header တွေ၊ usage တိုင်းတာမှု၊ brownout တို့နဲ့ ပိတ်သိမ်းပြီး ရက်ပြည့်ရင် 410 Gone ပြန်ပါ။
  • Client က version ကို pin လုပ်ပြီး Deprecation/Sunset header တွေကို စောင့်ကြည့်ကာ မသိတဲ့ field တွေကို လျစ်လျူရှုပါ။

OpenAPI Specification — OpenAPI Initiative

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

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

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

API Versioning | Thuta Learning