Python client
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:
Submit the task.
Poll the task until it is finished.
Obtain task response.
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:
Line 1: The URL of the AIMMS cloud.
Line 2: The fixed part of the path indicating the AIMMS PRO REST API is used.
Line 3: The scope used.
Line 4: The application name.
Line 5: The application version.
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:
Headers
contains the API key in the format:{"apiKey": "11111111111111111111111111111111111111111111"}
.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:
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:
Line 2: A get on the service end point
/task/{id}/response
is used to obtain the task response.