依赖项简介

本页面适用于 ApigeeApigee Hybrid

API Hub 依赖项可帮助您查看和管理 API 操作之间的关系。查找和跟踪 API 的依赖项有助于您更高效地管理 API 生态系统。例如,依赖项可帮助您发现更改或弃用 API 的潜在影响、排查问题或是分析对费用或合规性法规的遵守情况。API 可以存储在 API Hub 中,也可以存储在 API Hub 外部。

什么是依赖项?

API Hub 将依赖项建模为 API 操作之间的方向关系,其中一个 API 操作是提供方,另一个是使用方。例如,假设 PetStore API 调用 Location API 中的操作。在这种情况下,PetStore API 依赖于 Location API 提供的操作。在 API Hub 中,我们将 PetStore 称为使用方,将 Location 称为提供方。

图 1 说明了 API 操作之间的提供方/使用方关系。Payment API 的 /debit 操作会使用 Fraud Detection API 的 /detect 操作。在此关系中,扣款操作是使用方,欺诈检测操作是提供方。

同样,/detect 使用 UserProfile API 和 GeoLocation API 提供的操作。在这种情况下,/detect 操作是使用方,另外两个操作是提供方。

API Hub 依赖项 API Hub 依赖项

图 1. API 依赖项的映射

创建依赖项

您可以通过上传的 API 规范外部 API 在添加到 API Hub 的 API 操作之间创建依赖项。

在 API Hub 中创建依赖项的基本步骤如下:

  1. 找到两个可能具有依赖操作的 API。这些操作可以是已注册的 API外部 API
  2. 确定哪个 API 是提供方,哪个 API 是使用方。如前所述,使用方会调用提供方。
  3. 使用 Create Dependency API 在提供方与使用方之间创建依赖项。

如需详细了解如何调用 Create Dependency API 和相关示例,请参阅 Create Dependency API

查找依赖项

List Dependency API 是在 API Hub 中查找依赖项的好方法。该 API 可让您按字段 consumer.operation_resource_nameconsumer.external_api_resource_namesupplier.operation_resource_namesupplier.external_api_resource_name 进行过滤。

查找依赖项的基本步骤如下:

  1. 使用 List Dependency API 列出所有依赖项,或者使用该 API 的过滤功能缩小搜索范围。
  2. 使用 Get Dependency API 深入查看任何损坏的依赖项,以发现更多详细信息(例如提供方名称)。

下面是一个用于列出依赖项的 API 调用示例:

curl "https://apihub.googleapis.com/v1/projects/myproject/locations/us-central1/dependencies"
    -H "Authorization: Bearer: $(gcloud auth print-access-token)" -X GET -H "Content-Type: application/json"

识别损坏的依赖项

如需识别损坏的依赖项,请按照以下基本步骤操作:

  1. 使用 List Dependency API 列出所有依赖项,或者使用该 API 的过滤功能缩小搜索范围。
  2. 确定哪些返回的依赖项设置了错误标志。
  3. 使用 Get Dependency API 深入查看任何损坏的依赖项,以发现更多详细信息(例如提供方名称)。

    下面是一个响应示例,其中的错误条件表示已移除提供方操作:

    {
      "name": "projects/common-dev-15/locations/us-central1/dependencies/user-to-pet",
      "consumer": {
          "displayName": "POST - /v2/user",
          "operationResourceName": "projects/common-dev-15/locations/us-central1/apis/payments-api/versions/2ff89c88-e8b6-48c4-a1c2-bdbb2a929bde/operations/createuser"
      },
      "supplier": {
          "displayName": "POST - /v2/pet",
          "operationResourceName": "projects/common-dev-15/locations/us-central1/apis/payments-api/versions/2ff89c88-e8b6-48c4-a1c2-bdbb2a929bde/operations/addpet"
      },
      "state": "VALIDATED",
      "description": "Dependency from user to pet API",
      "discoveryMode": "MANUAL",
      "createTime": "2024-04-17T19:33:31.215978712Z",
      "updateTime": "2024-04-17T19:33:31.737505297Z",
      {
        "error": "SUPPLIER_NOT_FOUND",
        "errorTime": 2024-05-18T20:23:42.465324Z
      }
    }

其他依赖项操作

创建依赖项后,您可以使用 API Hub API 获取依赖项的详细信息、通过过滤 API Hub 中所有依赖项的列表来查找依赖项、删除依赖项以及更新依赖项的说明。API 参考文档管理依赖项中详细介绍了所有依赖项 API。