アジュールAPI管理101

12605 ワード


概要
これは、あなたの最初のステップを歩くのノートですAzure Azure API Mamanagement . AzureパイプラインYAMLとのコードサンプルはこちらです.NET 6
  • iac-pipeline-apim.yml
  • azuredeploy-apim.json
  • dotnet-pipeline-travelapi.yml
  • TravelApi (.NET6)

  • TOC
  • Architecture

  • Step by Step
  • 1. Deploy
  • 2. Extract swagger.json
  • 3. Import the API
  • 4. Test the API (Azure Portal)
  • 5. Get a subsctiption key
  • 6. Test the API (Local)
  • API Management purge

  • 建築

    一歩一歩
    配備する
  • Setup BASE_NAME and ENVIRONMENT_SYMBOL in variables-template.yml
  • Azureパイプラインを走らせるiac-pipeline-apim.yml
  • Azureパイプラインを走らせるdotnet-pipeline-travelapi.yml

  • スガー.JSON OpenAPI format in API Management requires [swagger.json] file. You can extract the file by running the app TravelApi ローカル.ファイルの位置は以下のようになりますhttps://localhost:7228/swagger/v1/swagger.jsonスワガーJSONの例
    {
      "openapi": "3.0.1",
      "info": {
        "title": "TravelApi",
        "version": "1.0"
      },
      "paths": {
        "/TravelPlan": {
          "get": {
            "tags": [
              "TravelPlan"
            ],
            "summary": "Get a list of destinations",
            "description": "Sample request:\r\n            \r\n    GET /travelplan",
            "responses": {
              "400": {
                "description": "Bad Request"
              },
              "401": {
                "description": "Unauthorized"
              },
              "500": {
                "description": "Server Error"
              },
              "503": {
                "description": "Server Error"
              },
              "200": {
                "description": "Success",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Destination"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "components": {
        "schemas": {
          "Destination": {
            "type": "object",
            "properties": {
              "country": {
                "type": "string",
                "nullable": true
              },
              "city": {
                "type": "string",
                "nullable": true
              },
              "food": {
                "type": "string",
                "nullable": true
              },
              "sightSeeing": {
                "type": "string",
                "nullable": true
              }
            },
            "additionalProperties": false
          }
        }
      }
    }
    
    スワガーローカルで走るとき


    APIをインポート The code sample already includes a TravelApi api in ARM template azuredeploy-apim.json . これはAPIのコンテンツを持っていない、あなたがインポートし、swaggerを追加する必要があります.それをJSON.
    Azuredeploy APIMJSON
    {
      "type": "Microsoft.ApiManagement/service/apis",
      "apiVersion": "2021-08-01",
      "name": "[concat(variables('apim_name'), '/travelapi')]",
      "dependsOn": [
        "[resourceId('Microsoft.ApiManagement/service', variables('apim_name'))]"
      ],
      "properties": {
        "displayName": "TravelApi",
        "subscriptionRequired": true,
        "serviceUrl": "[variables('apim_service_url')]",
        "protocols": [
          "https"
        ],
        "path": ""
      }
    }
    
    Azureポータルと展開されたAzure API管理に移動します.あなたが表示されますTravelApi ARMテンプレートで既に設定されています.あなたは加えるつもりですswagger.json はい.APIs --> TravelApi --> 輸入

    絹篩で篩うたようOpenAPI
    絹篩で篩うたようswagger.json 前のステップで抽出.絹篩で篩うたようAppend and Postfix , and Import .


    APIをテストする

    Go to API Management --> APIs --> TravelApi --> Test --> GET /TravelPlan , and Send . You will see the successful response from the deployed App Service.



    サブスクリプションキーを取得する

    In order to test the API from your local machine, you need to get a subscription key to access the API.

    Go to API Management --> Subscriptions --> Build-in all-access subscription , and then keep Primary Key for later usage.



    APIをテストする

    Send HTTP request from the terminal like below.

    Bash example

    curl -H "Ocp-Apim-Subscription-Key: {Primary Key}" https://{API Management name}.azure-api.net/travelplan
    

    Example response

    [
      {
        "country": "France",
        "city": "Paris",
        "food": "Wine",
        "sightSeeing": "Eiffel Tower"
      },
      {
        "country": "Japan",
        "city": "Tokyo",
        "food": "Ramen",
        "sightSeeing": "Skytree"
      }
    ]
    

    API管理パージ According to the Microsoft official document 2022年1月現在、API管理インスタンスはソフト削除(Azure Resource Manager API 2010 - 06 - 01 -プレビューまたはそれ以降)です.ソフト削除例の保留間隔は48時間である.また、ハード削除されたインスタンスをハードthe REST API .
    パワーシェルサンプル
    az rest --method delete --header "Accept=application/json" `
    -u 'https://management.azure.com/subscriptions/{Azure Subscription ID}/providers/Microsoft.ApiManagement/locations/{Location}/deletedservices/{API Management name}?api-version=2021-08-01'