Skip to main content

Swagger

在本示例中,您将创建一个 swaggerPath 蓝图,该蓝图将使用 Port'sAPIwebhook functionality 的组合来引用 swagger.json 文件中的所有 API 路径。

要将 API 路径引用到 Port,需要使用一个脚本,根据 webhook 配置发送路径信息。

先决条件

创建以下蓝图定义和 webhook 配置:

Swagger path blueprint
{
"identifier": "swaggerPath",
"description": "This blueprint represents a Swagger path in our software catalog",
"title": "Swagger API Paths",
"icon": "Swagger",
"schema": {
"properties": {
"method": {
"type": "string",
"title": "Method",
"default": "get",
"enum": ["get", "post", "delete", "put", "patch"],
"enumColors": {
"get": "yellow",
"post": "green",
"delete": "red",
"put": "purple",
"patch": "purple"
}
},
"host": {
"type": "string",
"title": "API Base URL",
"format": "url"
},
"path": {
"title": "Path",
"type": "string"
},
"parameters": {
"items": {
"type": "object"
},
"title": "Parameters",
"type": "array"
},
"responses": {
"title": "Responses",
"type": "object"
},
"description": {
"title": "Description",
"type": "string"
},
"version": {
"title": "Version",
"type": "string"
},
"summary": {
"title": "Summary",
"type": "string"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Swagger path webhook configuration
{
"identifier": "swaggerPathMapper",
"title": "Swagger Path Mapper",
"description": "A webhook configuration to map Swagger JSON file",
"icon": "Swagger",
"mappings": [
{
"blueprint": "swaggerPath",
"itemsToParse": ".body.paths",
"entity": {
"identifier": ".item.id | sub(\"[^A-Za-z0-9@_.:/=-]\"; \"-\"; \"g\")",
"title": ".item.method + .item.path",
"properties": {
"method": ".item.method",
"host": ".item.host",
"path": ".item.path",
"parameters": ".item.parameters",
"responses": ".item.responses",
"description": ".item.description",
"version": ".item.version",
"summary": ".item.summary"
}
}
}
],
"enabled": true,
"security": {}
}

使用 Port 的 API 和 Bash 脚本

下面的示例片段展示了如何使用 Python 和 Bash 将 Port 的 API 和 Webhook 与现有管道集成:

在版本库中创建以下 Python 脚本,以创建或更新 Port 实体,作为管道的一部分:

Python script example
## Import the needed libraries

import requests
import json
import os

# Get environment variables using the config object or os.environ["KEY"]
WEBHOOK_URL = os.environ['WEBHOOK_URL'] ## the value of the URL you receive after creating the Port webhook
PATH_TO_SWAGGER_JSON_FILE = os.environ["PATH_TO_SWAGGER_JSON_FILE"]

def add_entity_to_port(entity_object):
"""A function to create the passed entity in Port using the webhook URL

Params
--------------
entity_object: dict
The entity to add in your Port catalog

Returns
--------------
response: dict
The response object after calling the webhook
"""
headers = {"Accept": "application/json"}
response = requests.post(WEBHOOK_URL, json=entity_object, headers=headers)
return response.json()

def read_swagger_file(swagger_json_path):
"""This function takes a swagger.json file path, converts the "paths" property into a
JSON array and then sends this data to Port

Params
--------------
swagger_json_path: str
The path to the swagger.json file relative to the project's root folder

Returns
--------------
response: dict
The response object after calling the webhook
"""
with open(swagger_json_path) as file:
data = json.load(file)

project_info = data.get("info")
project_title = project_info.get("title")
project_version = project_info.get("version")
hosted_url = data.get("host")
base_path = data.get("basePath")

paths = data.get('paths', {})
path_list = []
index = 1
for path, methods in paths.items():
for method, method_info in methods.items():
path_id = f"{project_title}-{index}"
path_info = {
"id": path_id,
"path": path,
"method": method,
"summary": method_info.get('summary'),
"description": method_info.get('description'),
"parameters": method_info.get("parameters"),
"responses": method_info.get("responses"),
"project": project_title,
"version": project_version,
"host": "https://" + hosted_url + base_path
}
path_list.append(path_info)
index+=1

entity_object = {
"paths": path_list
}
webhook_response = add_entity_to_port(entity_object)
return webhook_response

response = read_swagger_file(PATH_TO_SWAGGER_JSON_FILE)
print(response)