Skip to content

Common Examples

How to get a ticket

In these examples, we will be using call_tactic function we defined in the Quick Start example.

let server_url = "https://portal.southpawtech.com"
let site = "trial"
let project = "api_test"
let user = "trial_api"
let password = "tactic123"

let base_endpoint = server_url+ "/" + site + "/" + project + "/REST";

const get_ticket = async () => {

    let url = get_endpoint() + "/" + "get_ticket";
    let headers = {
        Accept: 'application/json',
    }
    let data = {
        'user': user,
        'password': password,
    };
    let r = await fetch( url, {
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data),
    } )
    let ticket = await r.json()
    return ticket;

}

const get_endpoint = () => {
    return base_endpoint;
}

export { get_endpoint, get_ticket };

How to query a particular job

    //query for a particular job
    let job_code = 'JOB00001'

    let ticket = await get_ticket()

    let search_type = "workflow/job"
    let kwargs = {
        search_type: search_type,
        filters: [['code', job_code]],
    };
    let sobjects = await call_tactic("query", kwargs)
    console.log(sobjects)

How to query tasks from a job

    let job_code = 'JOB00001'

    let ticket = await get_ticket()
    let url = get_endpoint()

    // get tasks
    let search_type = "sthpw/task"
    let kwargs = {
        search_type: search_type,
        filters: [['search_code', job_code]],
    };
    let sobjects = await call_tactic("query", kwargs)
    console.log(sobjects);

How to query assets and snapshots

    get_asset_info = async () => {
        let search_text = this.state.search_text // search text from the form

        // First, let's retrieve assets using the keywords search.
        let search_type = "workflow/asset"
        let filters = [
            ['keywords', 'contains', search_text]
        ]
        let kwargs = {
            search_type: search_type,
            filters: filters,
        };
        let ticket = await get_ticket()
        let sobjects = await call_tactic("query", kwargs)

        // nothing found
        if (sobjects.length <= 0) {
            this.setState({assets: []})  // set the state variable.
            return;
        }

        // We will retrieve snapshots (files) for the assets. We are going to use
        // query_snapshots function to get the relevant files that are web-accessible.
        const asset_codes = sobjects.map(element => element.code);

        let asset_codes_str = asset_codes.join("|")

        let project_code = 'api_test'
        let asset_codes_filter = [
            ['search_code', 'in', asset_codes_str], ['is_latest', 'true'], ['project_code', project_code]
        ]

        kwargs = {
            include_paths_dict: true,
            include_web_paths_dict: true,
            filters: asset_codes_filter,
            order_bys: "search_code",
        }

        ticket = await get_ticket()
        let snapshots = await call_tactic("query_snapshots", kwargs)
        console.log(snapshots)

        // we will process the assets and snapshots to be returned or set
        // to the state variable.
        let obj = {}
        sobjects.forEach(function (a) {
            obj[a.code] = a;
        });

        console.log(obj)

        let res = snapshots.map(function (a) {
            return {
                code: obj[a.search_code].code,
                name: obj[a.search_code].name,
                status: obj[a.search_code].status,
                snapshot: a.__web_paths_dict__
            };
        });

        this.setState({assets: res}) // set the state variable.
        return;
    }

Displaying the results in React:

<div className="asset-list">
    {
        this.state.assets.map( (asset, index) => (
            <div key="{asset.code}">
            Asset Code: {asset.code} <br/>
            Name: {asset.name} <br/>
            Status: {asset.status} <br/>
            File:<br/>
            <a href={get_server_url() + asset.snapshot.main[0]} target="_blank">
                {
                    typeof asset.snapshot.web !=='undefined' ?
                    <img src={get_server_url() + asset.snapshot.web[0]}/>
                    : "Download"
                }
            </a>
            <hr/>
            </div>
        ))
    }
</div>