V
V
Victor L2020-03-19 16:14:15
Python
Victor L, 2020-03-19 16:14:15

Sending reports to api?

I use this project here The server is unrolled api works.
In the examples there is a python script, it is working, I just wanted to call it in the task, but they said that it’s not “boyish” to rewrite it to groovy,
rewrote it from the server, 200 is returned, but I don’t see any reports in Allure, tell me what I missed. Maybe there is a ready-made solution and I went in the wrong direction.

Python

import os, requests, json, base64

# This directory is where you have all your results locally, generally named as `allure-results`
allureResultsDirectory = 'allure-results'
# This url is where the Allure container is deployed. We are using localhost as example
allureServer = 'http://127.0.0.1:5050'


currentDirectory = os.path.dirname(os.path.realpath(__file__))
resultsDirectory = currentDirectory + allureResultsDirectory
print('RESULTS DIRECTORY PATH: ' + resultsDirectory)

files = os.listdir(resultsDirectory)

print('FILES:')
results = []
for file in files:
    result = {}

    filePath = resultsDirectory + "/" + file
    print(filePath)

    if os.path.isfile(filePath):
        try:
            with open(filePath, "rb") as f:
                content = f.read()
                if content.strip():
                    b64Content = base64.b64encode(content)
                    result['file_name'] = file
                    result['content_base64'] = b64Content.decode('UTF-8')
                    results.append(result)
                else:
                    print('Empty File skipped: '+ filePath)
        finally :
            f.close()
    else:
        print('Directory skipped: '+ filePath)

headers = {'Content-type': 'application/json'}
requestBody = {
    "results": results
}
jsonRequestBody = json.dumps(requestBody)

response = requests.post(allureServer + '/send-results', headers=headers, data=jsonRequestBody)
print("RESPONSE:")
jsonResponseBody = json.loads(response.content)
jsonPrettierResponseBody = json.dumps(jsonResponseBody, indent=4, sort_keys=True)
print(jsonPrettierResponseBody)
print("STATUS CODE:")
print(response.status_code)


my groovy code

task sendReports {
    group "Work Allure Server api"
    description "send results test in remote server"
    doLast {
        /* Settings */
        String reportPath = 'build/allure-results'
        String allureServer = 'http://127.0.0.1:5050/send-results'
        /* get files in build */
        def dh = new File(reportPath)
        def request = new Request()
        dh.eachFile {
            Reports files = new Reports(
              file_name: it.name.replaceFirst(~/\.[^\.]+$/, ''),
              content_base64: it.text.bytes.encodeBase64()
            )
            request.results.add(files)
        }
        //println JsonOutput.toJson(request)
        /* Send report to api*/
        def req = new URL(allureServer).openConnection()
        req.setRequestMethod("POST")
        req.setRequestProperty("Content-Type", "application/json; charset=UTF-8")
        req.setDoOutput(true)
        req.getOutputStream().write(JsonOutput.toJson(request).getBytes("UTF-8"))
        logger.quiet "Status code: ${req.getResponseCode()}"
        def resp = new JsonSlurper().parseText(req.getInputStream().getText())
        logger.quiet "Response: ${resp}"
    }
}

class Request {
   List<Reports> results = new LinkedList<>()
}
class Reports {
    String file_name
    String content_base64
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor L, 2020-03-20
@Fzero0

In general, the code works, you just need to send the full file name along with the extension

task sendReports {
    group "Work Allure Server api"
    description "send results test in remote server"
    doLast {
        /* Settings */
        String reportPath = 'build/allure-results'
        String allureServer = 'http://127.0.0.1:5050/send-results'
        /* get files in build */
        def dh = new File(reportPath)
        def request = new Request()
        dh.eachFile {
            Reports files = new Reports(
              file_name: it.name,
              content_base64: it.text.bytes.encodeBase64()
            )
            request.results.add(files)
        }
        //println JsonOutput.toJson(request)
        /* Send report to api*/
        def req = new URL(allureServer).openConnection()
        req.setRequestMethod("POST")
        req.setRequestProperty("Content-Type", "application/json; charset=UTF-8")
        req.setDoOutput(true)
        req.getOutputStream().write(JsonOutput.toJson(request).getBytes("UTF-8"))
        logger.quiet "Status code: ${req.getResponseCode()}"
        def resp = new JsonSlurper().parseText(req.getInputStream().getText())
        logger.quiet "Response: ${resp}"
    }
}

class Request {
   List<Reports> results = new LinkedList<>()
}
class Reports {
    String file_name
    String content_base64
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question