Table of contents

Execution API examples

Presents examples using curl to illustrate how to use the Execution API.

These examples use curl, a command line tool and library for transferring data with URL syntax. You can download curl and read more about it at http://curl.haxx.se.

A request to the Execution API is sent as a SolveConfig job, which specifies the worker type, certain parameters, and the attachments to use in the solve.

Upload data in a container

The following snippet uploads my_file to the specified location.

curl -X POST \
  https://127.0.0.1/v1/dopt-execution-api/data/containers/my_container/my_file/data \
  -H 'content-type: application/octet-stream' \
  -H 'Authorization: <My release token>' \
  --data-binary "@path/to/my_file"

The second line is the URL of the data storage area of the Decision Optimization Execution API. The fourth line contains the authorization token for use with the deployed model.

This returns connection details that can be used as a connection for a SolveConfig.

    {
        "type": "REST",
        "name": "my_file",
        "category": "input",
        "verb": "GET",
        "expectedStatus": 200,
        "url": "https://127.0.0.1/v1/dopt-execution-api/data/containers/my_container/my_file/data",
        "headers": {
            "Authorization": "<My release token>"
        }
    }

To upload more than one file, you use a multipart upload:

curl -X POST \
  https://127.0.0.1/v1/dopt-execution-api/data/containers/my_container/my_file/data \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'Authorization: <My release token>' \
  -F '=@path/to/my_file_1' \
  -F '=@path/to/my_file_2'

SolveConfig

A SolveConfig specifies a docplex worker for models that use the DOcplex Python API. This is the standard worker for models developed in Watson Studio.

You specify in the SolveConfig what connections to use to load input and save output. A connection can be a container, a REST connection, an inline table, or inline text.

Container connection

To load all attachments from solve container:
{
    "type" : "CONTAINER",
    "category" : "input",
    "containerId" : "fce42d14-7162-4639-9afe-11e7b8af4dff"
}
To save all attachments to a solve container:
{
    "type" : "CONTAINER",
    "name" : ".*",
    "category" : "output",
    "containerId" : "fce42d14-7162-4639-9afe-11e7b8af4dff"
  }

REST connection

To load all attachments from solve external REST service:
{
    "type" : "REST",
    "name" : "diet_food_nutrients.csv",
    "category" : "input",
    "verb" : "GET",
    "expectedStatus" : 200,
    "url" : "http://desktop-abc3rth:5040/dd-execution/api/v1/data/containers/job.6882bc97-4f50-482d-b224-77cb9e7a9fd5/diet_food_nutrients.csv/data",
    "headers" : {
      "Authorization" : "<My release token>"
    }
  }
To save all attachments to an external REST service:
{
    "type" : "REST",
    "name" : ".*/.csv",
    "category" : "output",
    "verb" : "POST",
    "expectedStatus" : 201,
    "url" : "http://desktop-abc3rth:5040/dd-execution/api/v1/data/containers/${job_id}/${attachment_name}/data",
    "headers" : {
      "Authorization" : "<My release token>"
    }
  }

Inline table

To add an inline input table:

{
    "type" : "INLINE_TABLE",
    "name" : "diet_nutrients",
    "category" : "input",
    "table" : {
      "name" : "diet_nutrients",
      "rows" : [ {
        "values" : [ "\"name\"", "\"qmin\"", "\"qmax\"" ]
      }, {
        "values" : [ "\"Calories\"", 2000.0, 2500.0 ]
      }, {
        "values" : [ "\"Calcium\"", 800.0, 1600.0 ]
      } ]
    }
  }

To add an inline output table:

{
    "type" : "INLINE_TABLE",
    "name" : ".*/.csv",
    "category" : "output"
  }

Submitting SolveConfig

Submitting a job with an attachment for log output and all files present in my_container:

curl -X POST \
  https://127.0.0.1/v1/dopt-execution-api/data/containers/my_container/my_file/data \
  -H 'content-type: application/octet-stream' \
  -H 'Authorization: <My release token>' \
  -d '  {
        "workerType":"docplex", 
        "solveParameters" : { 
            "oaas.logAttachmentName" : "log.txt"
        },     "type" : "CONTAINER",
        "attachments" : [ 
            {
                        "type": "CONTAINER",
                        "category": "input",
                        "containerId": "my_container",
                }
            ]
    }

This returns a job status URL in the Location header, that can be used to poll status.