Condividi tramite


Simulare un'API CRUD in Internet

Dev Proxy consente di simulare API CRUD senza doverli compilare. La simulazione di API con Dev Proxy consente di risparmiare tempo e velocizzare lo sviluppo. Integrando l'API con i servizi cloud, è necessario esporre l'API in Internet in modo che il servizio cloud possa accedervi. Per esporre un'API CRUD simulata da Dev Proxy in Internet, usare Dev Tunnels. Questo articolo illustra come configurare un'API CRUD da esporre in Internet usando Dev Tunnels.

Suggerimento

L'API CRUD in questo articolo si basa sull'esempio di proxy di sviluppo del database Northwind .

Configurare l'API CRUD per essere esposta attraverso Internet

Per esporre un'API CRUD simulata da Dev Proxy in Internet, iniziare configurando l'API CRUD.

Importante

In questo momento, è possibile esporre solo le API CRUD HTTP in Internet usando Dev Tunnels.

Definire i dati dell'API CRUD

Creare un file di dati denominato orders-data.json, che esegue il backup dell'API CRUD, ad esempio:

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

Configurare l'API CRUD

Creare quindi il file di configurazione dell'API denominato orders-api.json, in cui si specificano l'URL dell'API CRUD, le relative operazioni e il file di dati. Assicurarsi di specificare un URL HTTP nella proprietà baseUrl:

{
  "$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})]"
    }
  ]
}

Definire la configurazione del proxy di sviluppo

Creare quindi un file di configurazione di Dev Proxy denominato devproxyrc.json con il CrudApiPlugin abilitato. Configurare Dev Proxy per ascoltare l'URL configurato per la tua API CRUD

{
  "$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"
  }
}

Verificare la configurazione

Verificare che l'API CRUD funzioni correttamente eseguendo Dev Proxy e inviando richieste all'API CRUD.

Avviare Dev Proxy, supponendo di aver salvato la configurazione del proxy di sviluppo in un file denominato devproxyrc.json nella directory di lavoro corrente:

devproxy

Chiamare l'API CRUD usando curl:

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

Esporre l'API CRUD su Internet

Per esporre l'API CRUD in Internet, avviare un tunnel di sviluppo mappato alla porta del proxy di sviluppo. Configurare il tunnel per usare il nome host configurato per l'API CRUD.

Avvertimento

Consentire l'accesso anonimo a un tunnel di sviluppo significa che chiunque su Internet è in grado di connettersi al server locale, se può indovinare l'ID del tunnel di sviluppo.

$ 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

Chiamare l'API CRUD simulata da Dev Proxy tramite dev tunnel usando curl:

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