通过 Internet 模拟 CRUD API

概览
目标: 通过开发隧道公开 CRUD API
时间: 15 分钟
Plugins:CrudApiPlugin
先决条件:设置开发代理开发隧道

使用开发代理可以 模拟 CRUD API ,而无需生成它们。 使用开发代理模拟 API 可以节省时间并加快开发速度。 将 API 与云服务集成时,需要通过 Internet 公开 API,以便云服务可以访问它。 若要在互联网上公开开发人员代理模拟的 CRUD API,请使用 Dev Tunnels。 本文介绍如何使用 Dev Tunnel 将 CRUD API 配置为通过 Internet 公开。

小窍门

本文中的 CRUD API 基于 Northwind 数据库开发代理示例

将 CRUD API 配置为通过 Internet 公开

若要在互联网上公开由 Dev Proxy 模拟的 CRUD API,请先配置 CRUD API。

重要

目前,只能使用开发隧道在 Internet 上公开 HTTP CRUD API。

定义 CRUD API 数据

创建一个名为 orders-data.json的数据文件,用于支持 CRUD API,例如:

文件: orders-data.json

[
  {
    "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:

文件: orders-api.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.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})]"
    }
  ]
}

定义开发代理配置

接下来,创建一个名为 devproxyrc.json 的开发代理配置文件,并启用 CrudApiPlugin。 配置开发代理以侦听为 CRUD API 配置的 URL:

文件: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.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"
  }
}

验证配置

通过运行开发代理并将请求发送到 CRUD API,验证 CRUD API 是否正常工作。

启动开发代理,假设你在当前工作目录中命名 devproxyrc.json 的文件中保存了开发代理配置:

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"
  }
]

通过 Internet 公开 CRUD API

若要通过 Internet 公开 CRUD API,请启动映射到开发代理端口的开发隧道。 配置隧道以使用为 CRUD API 配置的主机名。

警告

允许匿名访问开发隧道意味着 Internet 上的任何人都可以连接到本地服务器(如果他们可以猜测开发隧道 ID)。

$ 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 调用由开发代理通过开发隧道模拟的 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"
  }
]

另请参阅