Python client

Python 3.10 script

To create a service, it helps to have a client available that can test that service.

In this article, a python client is presented to illustrate using this machine scheduling service.

The client has the following steps:

  1. Submit the task.

  2. Poll the task until it is finished.

  3. Obtain task response.

  4. Report the solution.

These steps are detailed in the next sections.

Submit task

According to the AIMMS PRO REST API , a post to the service end point /tasks/{app}/{ver}/{service} is required. Easily achieved using python as follows:

url_submit = sp_server + '/' + sp_api + '/' + sp_scope + '/' + sp_app + '/' + sp_ver + '/' + sp_service

The Python string variables used are:

1sp_server = 'https://chriskuip.aimms.cloud'
2sp_api = 'pro-api/v1'
3sp_scope = 'tasks'
4sp_app = 'MachineSchedulingMIPTimeIndexedSolver'
5sp_ver = '007'
6sp_service = 'equalParallelMachineTimeIndexedMIP'

Remarks:

  1. Line 1: The URL of the AIMMS cloud.

  2. Line 2: The fixed part of the path indicating the AIMMS PRO REST API is used.

  3. Line 3: The scope used.

  4. Line 4: The application name.

  5. Line 5: The application version.

  6. Line 6: The service offered by the application.

Then we need to post the request, which is accomplished using the a POST request with the Requests package as follows:

1Headers = readJson('apikey.txt')
2requestDict = readJson('data/3Par15act.json')
3
4response_submit = requests.post(url_submit, json=requestDict, headers=Headers)

The string variables used are defined as follows:

  1. Headers contains the API key in the format: {"apiKey": "11111111111111111111111111111111111111111111"}.

  2. requestDict contains the request body and is the actual input for the task.

Polling task status

 1# Get the response id
 2sp_id = response.json()['id']
 3
 4# Wait until the task is finished, polling every second
 5url_poll = sp_server + '/' + sp_api + '/' + sp_scope + '/' + sp_id
 6
 7status = ""
 8print("STATUS:")
 9while status != "finished":
10    time.sleep(1)
11    response = requests.get(url_poll, headers=Headers)
12    status = response.json()['status']
13    print(status)

Remarks:

  1. Line 2: sp_id is the id of the task, and takes the form of a GUID string.

  2. Line 5: The service end point to poll for the status is: /tasks/{id}

  3. Line 10: A GET on this service end point.

  4. Line 11: The values status can take on are enumerated here

Obtain task response

1# Finished. Obtain the final result.
2url_response = sp_server + '/' + sp_api + '/' + sp_scope + '/' + sp_id + '/' + 'response'
3
4response = requests.get(url_response, headers=Headers)
5return json.loads(response.text)

Remarks:

  1. Line 2: A get on the service end point /task/{id}/response is used to obtain the task response.

Further reading

  1. Publishing an AIMMS service

  2. More on the new REST service for ‘Tasks’

  3. REST Service for running solve jobs and other asynchronous jobs

  4. Various Python packages:

    1. Requests

    2. Pandas

    3. Plotly