模擬一個以 API 金鑰保護的 CRUD API

一目了然
目標: 模擬 CRUD API 與 API 金鑰認證
時間: 10分鐘
插件:CrudApiPlugin
前置條件:設定開發代理

建置應用程式時,您通常會與後端 API 互動。 有時候,這些 API 尚無法使用,或其他小組正在更新它們以符合最新的需求。 為了避免等候,您通常會建立模擬 API 來傳回您需要的數據。 雖然此方法可以解除您的封鎖,但這需要您花時間建置一個最終會被實際 API 取代的暫時性 API。 當你需要用 API 金鑰來保護 API 時,情況會更複雜。 若要避免浪費時間,您可以使用開發 Proxy 來模擬 CRUD API 並加速開發。

您可以使用CrudApiPlugin,透過記憶體內部資料存放區來模擬 CRUD (建立、讀取、更新、刪除) API。 使用簡單的組態檔,您可以定義模擬 API 支援的 URL 及其傳回的數據。 此插件也支援用戶端應用程式的跨域 CORS 使用。 外掛也支援 API 金鑰認證,因此你可以用 API 金鑰保護你的模擬 API,並測試應用程式是否正確傳送金鑰。

劇本

例如,您正在建置可讓使用者管理客戶的應用程式。 若要取得數據,您必須呼叫 /customers 後端 API 的端點。 API 是用 API 金鑰來保護的。 為了避免等待後端小組完成其工作,您決定使用開發 Proxy 來模擬 API 並傳回您需要的數據。

開始之前

您一開始需要建立一個包含客戶數據的模擬 CRUD API。 確認 API 正常運作後,你可以用 API 金鑰來保護它。

範例 1:模擬一個以標頭中 API 金鑰保護的 CRUD API

在第一個例子中,你用一個 API 金鑰來保護整個 API,客戶端會在 HTTP 標頭中傳送。

在檔案中 customers-api.json 加入有關 API 金鑰認證的資訊。

檔案:customers-api.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.0.0/crudapiplugin.apifile.schema.json",
  "baseUrl": "https://api.contoso.com/v1/customers",
  "dataFile": "customers-data.json",
  "auth": "apiKey",
  "apiKeyAuthConfig": {
    "apiKey": "my-secret-key",
    "headerName": "X-API-Key"
  },
  "actions": [
    {
      "action": "getAll"
    },
    {
      "action": "getOne",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "create"
    },
    {
      "action": "merge",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "delete",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    }
  ]
}

auth 屬性設為 apiKey,即可指定該 API 以 API 金鑰保護。 在屬性中 apiKeyAuthConfig ,您可以指定組態詳細數據。 屬性 apiKey 指定有效的 API 金鑰, headerName 屬性則指定外掛尋找金鑰的 HTTP 標頭。

如果你嘗試呼叫未將 X-API-Key 標頭設為 my-secret-key的 API,你會得到 401 Unauthorized 回應。

範例 2:模擬一個以查詢參數中 API 金鑰保護的 CRUD API

在某些 API 中,客戶端會將 API 金鑰作為查詢字串參數傳送。 你可以透過設定 queryParameterName 屬性來模擬這種行為。

更新customers-api.json檔案,如下所示:

檔案:customers-api.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.0.0/crudapiplugin.apifile.schema.json",
  "baseUrl": "https://api.contoso.com/v1/customers",
  "dataFile": "customers-data.json",
  "auth": "apiKey",
  "apiKeyAuthConfig": {
    "apiKey": "my-secret-key",
    "queryParameterName": "api_key"
  },
  "actions": [
    {
      "action": "getAll"
    },
    {
      "action": "getOne",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "create"
    },
    {
      "action": "merge",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "delete",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    }
  ]
}

在這個範例中,外掛會在查詢字串參數中尋找 API 金鑰 api_key 。 例如,呼叫 https://api.contoso.com/v1/customers?api_key=my-secret-key 成功,而呼叫 https://api.contoso.com/v1/customers 則回傳回應 401 Unauthorized

範例 3:模擬一個 CRUD API,該 API 同時接受標頭與查詢參數的 API 金鑰

你也可以將外掛設定為同時從 HTTP 標頭和查詢參數接受 API 金鑰。 外掛會先檢查標頭。 如果標頭沒有包含 API 金鑰,外掛會檢查查詢參數。

更新customers-api.json檔案,如下所示:

檔案:customers-api.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.0.0/crudapiplugin.apifile.schema.json",
  "baseUrl": "https://api.contoso.com/v1/customers",
  "dataFile": "customers-data.json",
  "auth": "apiKey",
  "apiKeyAuthConfig": {
    "apiKey": "my-secret-key",
    "headerName": "X-API-Key",
    "queryParameterName": "api_key"
  },
  "actions": [
    {
      "action": "getAll"
    },
    {
      "action": "getOne",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "create"
    },
    {
      "action": "merge",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    },
    {
      "action": "delete",
      "url": "/{customer-id}",
      "query": "$.[?(@.id == {customer-id})]"
    }
  ]
}

在此範例中,若請求在 X-API-Key 標頭或 api_key 查詢參數任一者中包含 API 金鑰,即會獲得授權。

後續步驟

深入瞭解 CrudApiPlugin。

範例

另請參閱相關的開發 Proxy 範例: