Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

diff restful api result #26

Open
davideuler opened this issue Aug 24, 2022 · 0 comments
Open

diff restful api result #26

davideuler opened this issue Aug 24, 2022 · 0 comments

Comments

@davideuler
Copy link
Owner

Case:
Need to compare different version of restful api to check if the result are identical.

-*- coding: utf-8
import os
import json
import requests
import traceback

try:
    from urlparse import urlparse
except:
    # Python 3x:
    from urllib.parse import urlparse

test_api_host="xxx-test.com"

expected_api_host="xxx.com"


def generat_output_json_path(imageUrl, apiName, fileNamePostfix):
    # it should not occur
    if not imageUrl:
        return "null_image_url.json"
    path = urlparse(imageUrl).path
    relative_url =  path.lstrip("/").replace("/","_") + fileNamePostfix + ".json"
    
    output_folder = "output/" + apiName.lower()
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        
    return output_folder + "/" + relative_url

def request_url_with_json(http_path, json):
    headers = {"Content-Type": "application/json", "x-sdk-invoke-type":"common", "Accept":"application/json", "PK":"123456789"}
    response = requests.post(http_path, data=json, headers=headers)
    return response.json()

def submit_request_to_url(serviceUrl, serviceUrlPostfix, imageUrl, jsonTemplateFile):

    if serviceUrl and not serviceUrl.startswith("http"):
        serviceUrl = "http://" + serviceUrl

    request_image_url = imageUrl
    request_json_template = open(jsonTemplateFile).read()
    request_json =  request_json_template.replace("$IMAGE_URL", request_image_url)
    
    response = None
    httpCode = None
    status = ""
    message = ""
        
    try:
        response =  request_url_with_json(serviceUrl + serviceUrlPostfix, request_json)
        if not response:
            print("Error request url: %s" % serviceUrl + serviceUrlPostfix)
            return None
        
        ## ignore fields:
        if "request_id" in response:
            del response["request_id"]
        
    except:
        # printing stack trace
        traceback.print_exc()

    return response


def diff_api_service_resullt(serviceUrlPostfix, imageUrl, jsonTemplateFile):
    
    test_api_response   = submit_request_to_url(test_api_host, serviceUrlPostfix, imageUrl, jsonTemplateFile)
    expected_api_response = submit_request_to_url(expected_api_host,  serviceUrlPostfix, imageUrl, jsonTemplateFile)
    
    test_json_file = generat_output_json_path(url, "detect", "_test")
    expected_json_file = generat_output_json_path(url, "detect", "_expected")
    diff_output_file = generat_output_json_path(url, "detect", "_diff")
    
    outfile = open(test_json_file,"w")
    output_json = json.dumps(test_api_response, indent=4, sort_keys=True, ensure_ascii=False)
    outfile.write(output_json)
    outfile.close()
    
    outfile = open(expected_json_file,"w")
    expected_json = json.dumps(expected_api_response, indent=4, sort_keys=True, ensure_ascii=False)
    outfile.write(expected_json)
    outfile.close()
    
    if not expected_json == output_json:
        # 保存差异到 diff 文件
        cmd = "python3 ./diff2html %s %s" % (test_json_file, expected_json_file)
        print("executing: %s" % cmd)
        diff_stdout = os.popen(cmd, "r")
        diff_output = diff_stdout.readlines()
    
        diff_outputfile_path = "./report/" + diff_output_file.replace("/","_") + ".html"
        dirname = os.path.dirname(diff_outputfile_path)

        if not os.path.exists(dirname):
            os.makedirs(dirname)
        
        diff_outfile = open(diff_outputfile_path,"w")
        diff_outfile.write("".join(diff_output))
        diff_outfile.close()
        
        diff_stdout.close()
        
        
if __name__ == "__main__":
    url = "https://www.demo.com/a.jpeg"
    diff_api_service_resullt("/api/face/detect", url, "./input/detect.json")

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant