管理 API 资源

本页面适用于 ApigeeApigee Hybrid

注册 API

向 API Hub 注册 API 时,您将创建一个表示 API 的实体并进行存储。我们将此实体称为 API 资源。存储在 API Hub 中的 API 资源可搜索,并可提供有关 API 的详细操作和说明信息。无论是在注册 API 时还是之后,您都可以添加和修改 API 资源属性以构建丰富的 API 说明。

控制台

如需注册 API,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入 Apigee API Hub 中的 API 页面。

    转到 API Hub
  2. 点击注册 API
  3. 一般详细信息下,视需要指定 API 的唯一 ID。如果您未提供 ID,则系统会为您生成一个 ID。如果您想输入 ID,请点击 Specify Unique ID

    此 ID 必须为 4-500 个字符,有效字符为 /[A-Z][a-z][0-9]-/.。此 ID 是完全限定的唯一 API 资源名称的最后一个组成部分,格式为:projects/PROJECT/locations/LOCATION/apis/API_ID

  4. (必需)提供 API 资源的显示名称。
  5. (可选)添加其他属性来描述 API。您可以随时返回 API 以添加或更改 API 属性。 请参阅修改 API 资源
    • 所有者信息:(可选)API 所有者的名称和电子邮件地址。
    • 其他详细信息:(可选)指定属性以帮助定义 API 并使其更易于搜索。您可以在“设置”中为其中一些属性定义默认值。如需了解详情,请参阅管理属性
    • 用户定义的属性:(可选)这些是在“设置”中定义的属性。如需了解详情,请参阅管理属性
  6. 点击注册

REST

如需注册 API,请使用 Create API REST API。

curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" -d '{
    "display_name": "DISPLAY_NAME",            # Required attribute
    "description": "DESCRIPTION"               # description is an optional attribute
    }
    'https://apihub.googleapis.com/v1/projects/HUB_PROJECT/locations/HUB_LOCATION/apis?api_id=API_ID

替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。
  • API_ID:(可选)输入 API 的唯一 ID。如果您未提供此查询参数,则系统会为您生成一个唯一 ID。此 ID 必须为 4-500 个字符,有效字符为 /[A-Z][a-z][0-9]-/.。此 ID 是完全限定的唯一 API 资源名称的最后一个组成部分,格式为:projects/PROJECT/locations/LOCATION/apis/API_ID
  • DISPLAY_NAME:(必需)API 资源的显示名称。您可以根据自己所需使用任何名称。
  • DESCRIPTION:(可选)您可以添加多个可选属性来描述 API 资源,包括说明、所有者、API 文档链接等。API 资源定义中列出并介绍了这些可选属性。

列出 API 资源

本部分介绍如何列出 API Hub 中的 API 资源。通过添加过滤条件,您可以限制响应中返回的 API。

控制台

如需列出 API 资源,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入 Apigee API Hub 中的 API 页面。

    转到 API Hub

    API 首页上列出了添加到 API Hub 的所有 API 的列表。

  2. 使用过滤条件指定关键字以过滤 API 列表。如果需要,请使用搜索查找 API。

REST

如需列出 API Hub 项目中的所有 API 资源,请使用列出 API API:

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

请替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。

示例输出:

{
  "apis": [
    {
      "name": "projects/myproject/locations/us-central1/apis/a035eb3b-658e-467a-89d3-9cca2bbacd32-2",
      "displayName": "TestApi2",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/a035eb3b-658e-467a-89d3-9cca2bbacd32-1",
      "displayName": "TestApi1",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/foo9013cfev",
      "displayName": "sample",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/foo90121",
      "displayName": "sample",
      "documentation": {}
    },
    ...
    ],
    "nextPageToken": "f4f4d78b-a974-46db-a214-dc139f8e9c8a"
  }

符合过滤条件的 REST 示例

您可以使用 API 过滤列出结果。在此示例中,我们根据系统定义的属性过滤 API:

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  'https://apihub.googleapis.com/v1/projects/HUB_PROJECT/locations/HUB_LOCATION/apis' \
  -d filter="owner.email=\"testowner3@gmail.com\"" -d page_size=10

请替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。

按所有者过滤的示例输出:

{
  "apis": [
    {
      "name": "projects/myproject/locations/us-central1/apis/theme-park-management-api",
      "displayName": "Theme Park Management API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/restaurant-api",
      "displayName": "Restaurant API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/casino-management-api",
      "displayName": "Casino Management API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/hotel-boooking-api",
      "displayName": "Hotel Booking API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/test-march-26-api3",
      "displayName": "Test Pets API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/test-march-26-api2",
      "displayName": "Test Pets API",
      "documentation": {}
    },
    {
      "name": "projects/myproject/locations/us-central1/apis/test-march-26-api1",
      "displayName": "Test Pets API",
      "documentation": {}
    }
  ]
}

获取 API 资源详细信息

本部分介绍了如何获取有关 API 资源的详细信息。

控制台

如需查看 API 资源的详细信息,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入 Apigee API Hub 中的 API 页面。

    转到 API Hub
  2. 使用过滤条件指定关键字以过滤 API 列表。如果需要,请使用搜索查找 API。
  3. 点击某个 API 以查看其详情页面。

REST

如需查看 API 资源的详细信息,请使用获取 API 资源详细信息 API:

curl "https://apihub.googleapis.com/v1/projects/HUB_PROJECT/locations/HUB_LOCATION/apis/API_ID"
  -H "Authorization: Bearer: $(gcloud auth print-access-token)" -X GET -H "Content-Type: application/json"

替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。
  • API_ID:API 资源的唯一 ID。

示例输出:

{
  "name": "projects/myproject/locations/us-central1/apis/myapi",
  "displayName": "My Test API",
  "description": "This is a test API.",
  "documentation": {},
  "owner": {
    "displayName": "Test API,
    "email": "testowner@gmail.com"
  },
  "versions": [
    "projects/myproject/locations/us-central1/apis/myapi/versions/myapi-version1"
  ],
  "createTime": "2024-03-26T10:13:15.668867002Z",
  "updateTime": "2024-03-26T10:13:16.700716401Z"
}

删除 API 资源

本部分介绍了如何删除 API 资源。

控制台

如需删除 API 资源,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入 Apigee API Hub 中的 API 页面。

    转到 API Hub
  2. 使用过滤条件指定关键字以过滤 API 列表。如果需要,请使用搜索查找要删除的 API。
  3. 操作菜单(API 所在行的右侧)中,点击删除

REST

如需从 API Hub 中删除 API,请使用 Delete API API:

curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  'https://apihub.googleapis.com/v1/projects/HUB_PROJECT/locations/HUB_LOCATION/apis/API_ID'

替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。
  • API_ID:要删除的 API 资源的唯一 ID。

修改 API 资源

本部分介绍了如何修改 API 资源。并非所有 API 资源属性都可以修改。如需查看可修改的属性列表,请参阅修补 API 参考文档。

控制台

如需修改 API,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入 Apigee API Hub 中的 API 页面。

    转到 API Hub
  2. 使用过滤条件指定关键字以过滤 API 列表。如果需要,请使用搜索查找要修改的 API。
  3. 点击某个 API 以查看其详情页面。
  4. 点击修改 API
  5. 修改 API 页面上进行更改。点击继续以前往下一组可修改的属性。
    • 一般详细信息:您可以修改 API 的显示名称。
    • 所有者信息:API 所有者的名称和电子邮件地址。
    • 其他详细信息:这些是系统定义的属性。您可以在管理属性中为这些属性指定自定义默认值。
    • 用户定义的属性:这些是用户定义的自定义属性。您可以在管理属性中指定自定义属性。
  6. 点击保存

REST

如需修改 API 资源,请使用 Patch API 资源 API

curl -X PATCH -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" -d '{
          "display_name": "Updated Test Docs API",        # Example request body with changes
          "description": "This is the updated test API.",
          "owner": {
            "display_name": "Updated Docs Owner",
            "email": "updateddocsowner@gmail.com"
            }
          }
          ' https://apihub.googleapis.com/v1/projects/HUB_PROJECT/locations/HUB_LOCATION/apis/API_ID

替换以下内容:

  • HUB_PROJECT:API Hub 宿主项目的名称。预配 API Hub 时选择了宿主项目。
  • HUB_LOCATION:宿主项目的位置。该位置是在预配 API Hub 时选择的。
  • API_ID:要更新的 API 的唯一 ID。
  • 请求正文:使用请求正文指定要更改的属性。请参阅 API 请求正文说明