lambda - ZDAutomotive/ZD-SWAG-SDK GitHub Wiki

Rest API Documentation for ZD-lambda-service

APIs:

  • /workers
    For getting the information of current worker
    url(get) http://{ip of target ZD-Box}:8080/api/lambda/lambda/workers

  • /upload (best matched with IDE)
    For uploading resources used by testcases
    post content {resource, {headers: ...}}
    url(post) http://{ip of target ZD-Box}:8080/api/filemanage/upload?dirname=tmp

  • /
    For running the Javascript testcases and getting the cached lambda logs
    post with {string of testcases}. See the detail in lambdaAPIExample.py
    delete has same function same as stop
    url(get, post, delete) http://{ip of target ZD-Box}:8080/api/lambda/lambda/

  • /script
    To preload the script, similar to the '/' interface above, but not start running automatically
    need to upload script first with upload interface and post with id and filename {filename: 'xxx.js', id: 'xxx'}
    url(get, post, delete) http://{ip of target ZD-Box}:8080/api/lambda/lambda/script

  • /start
    To trigger and start the loaded script with designated id {id: xxxx}
    url(post) http://{ip of target ZD-Box}:8080/api/lambda/lambda/start

  • /stop
    To stop the current running script url(post) http://{ip of target ZD-Box}:8080/api/lambda/lambda/stop

  • /lastJsonReport
    For getting the last json report generated by testcases
    url(get) http://{ip of target ZD-Box}:8080/api/lambda/lambda/lastJsonReport

Python Examples:

#Python 2.7.6
#RestfulClient for service Lambda

import requests
import json
import os
from os import listdir
from os.path import isfile, join


# Replace with the using zd-box ip address
ip = "http://192.168.178.114:8080/api/lambda/lambda"
apiList = ['/start', '/stop', '/', '/workers', '/lastJsonReport']


def getCase():
    url = ip + apiList[2]
    res = requests.get(url)
    # For successful API call, response code will be 200 (OK)
    if(res.ok):
        # Loading the response data into a dict variable
        # json.loads takes in only binary or string variables so using content to fetch binary content
        # Loads (Load String) takes a Json file and converts into python data structure (dict or list, depending on JSON)
        jData = json.loads(res.content)
        # get All cached js script in JSON format with api '/'
        # You can also use Websocket with port (7003) to get real time reponse 
        # import websockets 
        # websockets.connect
        # websockets.WebSocketApp('wss://ip port', on_message) .......
        # print json.dumps(jData, indent=4, sort_keys=True)
        with open('getcase.txt', 'w') as f:
            json.dump(jData, f, indent=4, sort_keys=True)
    # get last JSON report from ZD-Box from api 'lastJsonReport'
    url = ip + apiList[4]
    resReport = requests.get(url)
    if(resReport.ok):
        report = json.loads(resReport.content)
        # print json.dumps(report, indent=4, sort_keys=True)
        with open('testReport.txt', 'w') as f2:
            json.dump(report, f2, indent=4, sort_keys=True)        


def runCase(testScript):
    url = ip + apiList[2]
    # To post the test script and run the testcase in ZD-Box with api '/'
    res = requests.post(url, data=testScript, headers={'Content-Type': 'text/plain'})
    if(res.ok):
        jData = json.loads(res.content)
        # print json.dumps(jData, indent=4, sort_keys=True)
        with open('runcase.txt', 'w') as f:
            json.dump(jData, f, indent=4, sort_keys=True)


def upload():
    # parse the resource directory
    # upload resource once and can be used in other scripts
    onlyfiles = [f for f in listdir('resource') if isfile(join('resource', f))]
    for image_path in onlyfiles:
        # upload files one by one
	multipart_form_data = {
	    'file': (image_path, open(join('resource', image_path), 'r')),
	}
        res = requests.post('http://192.168.178.114:8080/api/filemanage/upload?dirname=tmp', files=multipart_form_data)
        print(res.text)


def main():
    # script should be stringified Javascript format 
    # script = "logger.info('Hello World!')"
    # should first upload files in resource folder
    upload()
    # load js script script = readFile('xxxx.js')
    runCase(script)
    getCase()
    upload()


main()