共用方式為


模擬網際網路上的 CRUD API

開發人員 Proxy 可讓您 模擬 CRUD API,而不需要建置它們。 使用開發 Proxy 模擬 API 可讓您節省時間並加快開發速度。 在整合 API 與雲端服務時,您必須透過因特網公開 API,讓雲端服務可以存取它。 若要透過因特網公開 Dev Proxy 模擬的 CRUD API,請使用 Dev Tunnels。 本文說明如何配置 CRUD API,使其能夠透過因特網公開,使用 Dev Tunnels。

提示

本文中的 CRUD API 是以 Northwind 資料庫開發 Proxy 範例為基礎。

將 CRUD API 設定為透過因特網公開

若要透過因特網公開 Dev Proxy 模擬的 CRUD API,請從設定 CRUD API 開始。

重要

目前,您只能使用 Dev Tunnel 在因特網上公開 HTTP CRUD API。

定義 CRUD API 資料

建立名為 orders-data.json的數據檔,以支援 CRUD API,例如:

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]

設定 CRUD API

接下來,建立名為 orders-api.json的 API 設定檔,您可以在其中指定 CRUD API URL、其作業和數據檔。 請務必在 baseUrl 屬性中指定 HTTP URL:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/crudapiplugin.apifile.schema.json",
  "baseUrl": "http://api.northwind.com/orders",
  "auth": "none",
  "dataFile": "orders-data.json",
  "actions": [
    {
      "action": "getAll"
    },
    {
      "action": "getOne",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    },
    {
      "action": "create"
    },
    {
      "action": "merge",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    },
    {
      "action": "delete",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    }
  ]
}

定義開發 Proxy 組態

接下來,建立名為 devproxyrc.json 且已啟用 CrudApiPlugin 的 Dev Proxy 配置檔。 設定開發代理伺服器,使其監聽您為 CRUD API 設定的網址:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "CrudApiPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "ordersApi"
    }
  ],
  "urlsToWatch": [
    "http://api.northwind.com/*"
  ],
  "ordersApi": {
    "apiFile": "orders-api.json"
  }
}

驗證組態

執行 Dev Proxy 並將要求傳送至 CRUD API,以確認 CRUD API 正常運作。

啟動開發 Proxy,假設您在目前工作目錄中名為 devproxyrc.json 的檔案中儲存了 Dev Proxy 組態:

devproxy

使用 curl 呼叫 CRUD API:

$ curl -x http://127.0.0.1:8000 http://api.northwind.com/orders

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]

透過因特網公開 CRUD API

若要透過因特網公開 CRUD API,請啟動對應至開發 Proxy 埠的開發通道。 設定通道以使用針對 CRUD API 設定的主機名。

警告

允許匿名存取開發通道表示因特網上的任何人都可以連線到您的本地伺服器,如果他們可以猜測開發通道標識符。

$ devtunnel host -p 8000 -a --host-header api.northwind.com

Hosting port: 8000
Connect via browser: https://vpfm55qw.euw.devtunnels.ms:8000, https://vpfm55qw-8000.euw.devtunnels.ms
Inspect network activity: https://vpfm55qw-8000-inspect.euw.devtunnels.ms

Ready to accept connections for tunnel: vpfm55qw

使用 curl 呼叫 Dev Proxy 透過開發通道模擬的 CRUD API:

$ curl https://vpfm55qw-8000.euw.devtunnels.ms/orders

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]