将人才收藏导入邮递员

问题描述 投票:0回答:2

如何将talend集合导入到postman?我找到了一种将邮递员导入到talend的方法,但反之则不然。有没有任何脚本或工具可以进行迁移?

postman talend
2个回答
0
投票

由于从 talend api tester 导出的 json 无法直接导入到 postman 中,因此您可以 编写一个转换器来更改其格式,以便可以被 postman 的导入器接受(我希望这将在 github 中得到官方支持,因为它是一个开放问题。)。

Python 中的示例转换器如下:

import json

# Open and read the Talend API Tester collection JSON file
with open('export_file.json', 'r') as f:
    talend_data = json.load(f)["entities"]

# Create new Postman collection objects
for folder in talend_data:
    folder_name = folder["entity"]["name"]
    postman_collection = {
        "info": {
            "name": folder_name,
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [] # all subfolders
    }

    # Loop through the requests in the Talend collection and add them to the Postman collection
    for subfolder in folder["children"]:
        postman_subfolder = {
            "name": subfolder["entity"]["name"],
            "item": []
        }

        for request in subfolder["children"]:
            request = request["entity"]
            name = request["name"]
            method = request["method"]["name"]
            protocol = request["uri"]["scheme"]["name"]
            uri = protocol + "://" + request["uri"]["host"] + request["uri"]["path"]
            headers = []
            for header in request["headers"]:
                header_key = header["name"]
                header_value = header["value"]
                headers.append({
                    "key": header_key,
                    "value" : header_value,
                    "type": "text"
                })


            postman_request = {
                "name": name,
                "protocolProfileBehavior": {
                    "disableBodyPruning": "true"
                },
                "request": {
                    "method": method,
                    "header": headers,
                    "body": {
                        "mode": "formdata",
                        "formdata": []
                    },
                    "url": {
                        "raw": str(uri),
                        "protocol": protocol
                    }
                },
                "response": []
            }
            postman_subfolder["item"].append(postman_request)

        postman_collection["item"].append(postman_subfolder)


    # Write the Postman collection JSON to a file
    with open(str(folder_name) + '.json', 'w') as f:
        json.dump(postman_collection, f)

0
投票

Zhaoyu 张的答案进行了一些改进,以适应我的端点集合,但仍然只提供部分支持,您可能需要根据自己的用例进一步调整它。

import json

# Open and read the Talend API Tester collection JSON file
with open('export_file.json', 'r') as f:
    talend_data = json.load(f)["entities"]

def handleEntity(entity, postman_parent):
    type = entity["entity"]["type"]

    if (type != "Request"):
        postman_subfolder = {
            "name": entity["entity"]["name"],
            "item": []
        }
        postman_parent["item"].append(postman_subfolder)

        for subfolder in entity["children"]:
            handleEntity(subfolder, postman_subfolder)          
    else:
        request = entity["entity"]
        name = request["name"]
        method = request["method"]["name"]
        protocol = request["uri"]["scheme"]["name"]
        uri = protocol + "://" + request["uri"]["host"] + request["uri"]["path"]
        headers = []
        for header in request["headers"]:
        header_key = header["name"]
        header_value = header["value"]
        headers.append({
            "key": header_key,
            "value" : header_value,
            "type": "text"
        })

        bodyMode =  ""
        raw = ""

        bodyType = request["body"]["bodyType"]
        if (bodyType == "Text"):
            bodyMode = "raw"
        if (bodyType == "Form"):
            if (request["body"]["formBody"]["encoding"] == "application/x-www-form-urlencoded"):
                bodyMode = "urlencoded"
            else:
                bodyMode = "formdata"

        if ("textBody" in request["body"]):
            raw = request["body"]["textBody"]

        postman_request = {
        "name": name,
        "protocolProfileBehavior": {
            "disableBodyPruning": "true"
        },
        "request": {
            "method": method,
            "header": headers,
            "body": {
                "mode": bodyMode,
                "raw": raw
            },
            "url": {
                "raw": str(uri),
                "protocol": protocol,
                "host": request["uri"]["host"].split("."),
                "path": request["uri"]["path"].split("/")[1:]
            }
        },
        "response": []
        }
        postman_parent["item"].append(postman_request)


# Create new Postman collection objects
for folder in talend_data:
    folder_name = folder["entity"]["name"]
    postman_collection = {
        "info": {
            "name": folder_name,
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [] # all subfolders
    }

    # Loop through the requests in the Talend collection and add them to the Postman collection
    for subfolder in folder["children"]:
        handleEntity(subfolder, postman_collection)


    # Write the Postman collection JSON to a file
    with open('files/' + str(folder_name) + '.json', 'w') as f:
        json.dump(postman_collection, f)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.