Create Slave App Part 2
Once the main.py file is created, we need to access the scenario parameter to point to the master app. This needs to be done from the Slave app.
- Access the Research tab
- Double click on the “scenario” parameter at the App level
- Select Master, New Study, Scenario (and don’t select dependent, as we’ll execute the Master model, rather than allow Akumen to do it)
- Open the Master app in another window
- Execute the Study by clicking the blue Play button at the top of the screen. A few things will happen
- The scenario parameter “first” in the Master will change to a random number
- The Master app will execute
- Results from the Master app will appear in the logs of the Slave app
Requests
We use Python requests to be able to make API calls to Akumen. We also need to import akumen_api to access the API url and token. The token is generated by the currently executing user, so best practice is not to hard code your own token in the app, but use the currently executing user’s token. This means that scheduled apps will use the runas user, ensuring security is adhered to.
import requests
import akumen_api
We need to add the authorization token to the headers of the requests object. This can be done through a dictionary called headers.
headers = { 'authorization': akumen_api.API_KEY }
And generate a url of the request we want to access, using the details from the incoming input parameter. This could be hardcoded, but by using a scenario parameter, it gives us the flexibility of allowing the users select a scenario. For example, we could have an approved operational scenario they need to run the app against, or a test scenario to validate something.
url = os.path.join(akumen_api.AKUMEN_API_URL, 'models', scenario['model_name'], scenario['study_name'], scenario['scenario_name'], 'input_parameters')
Now we need to actually run the request. Note that we use a get call here. We could use a post or also delete. Look at the API help for the correct call.
response = requests.get(url, headers=headers)
Requests also has additional parameters. If we’re doing a post, and have a json object as part of the request, we need to use json={…} where {…} is a json object to pass in. If it’s a json string, ie json.dumps({…}), we use data=string.
Checking for errors involves checking the response.status_code for 500 or 404. If it’s one of these, we convert the response to json using response_json=json.loads(response.text)
and querying the ’error’ object for the actual error. An exception can be raised in this case, which will abort app execution.
Look here for more information on the Python helpers.