API clients and References.md
The full documentation can be found at https://developer.mindsphere.io/resources/mindsphere-sdk-python/sphinxdoc/index.html
-
- Python version 3.6.x or higher installed where application will be running.
-
- User authorization token or app credentials with required scopes for Mindsphere Service APIs.
- 2.1 Environment variables set up in local machine to run application in local.
- 2.2 When application hosting type is
SELF_HOSTED
, the variables must be configured on server. - 2.3 When hosting an application in Cloud Foundry, the variable must be present as application's environment variables. This is achieved by adding variables in the manifest.yml file.
Application Credentials
Sr. No. | Environment Variable | Description |
---|---|---|
1 | MDSP_OS_VM_APP_VERSION | Store App Version in environment variable named MDSP_OS_VM_APP_VERSION . |
2 | MDSP_OS_VM_APP_NAME | Store App Name in environment variable named MDSP_OS_VM_APP_VERSION . |
3 | MDSP_KEY_STORE_CLIENT_ID | Store App Client ID in environment variable named MDSP_KEY_STORE_CLIENT_ID . |
4 | MDSP_KEY_STORE_CLIENT_SECRET | Store App Client Secret in environment variable named MDSP_KEY_STORE_CLIENT_SECRET . |
5 | MDSP_HOST_TENANT | Store the name of the tenant on which application is hosted in environment variable named MDSP_HOST_TENANT . |
6 | MDSP_USER_TENANT | Store the name of the tenant from which application is being accessed in environment variable named MDSP_USER_TENANT . |
7 | HOST_ENVIRONMENT | Store the region in environment variable named HOST_ENVIRONMENT . If not specified, HOST_ENVIRONMENT defaults to eu1 in region Europe 1 SDK and to cn1 in region China 1 SDK. |
- Above credentials ( App Credentials ) will suffice to use SDKs.
- For more information about credentials please visit Token Handling
App Credentials and Application Credentials refers to same concept. These terms might be used interchangeably in the document.
HOST_ENVIRONMENT: eu1 If not specified, HOST_ENVIRONMENT defaults to eu1 in region Europe 1 SDK and to cn1 in region China 1 SDK.
Download the MindSphere SDK for Python from the Siemens Industry Online Support (SIOS) Portal.
MindSphere provides two ways to host an application - Cloud Foundry Hosted
and Self Hosted
.
For Cloud Foundry Hosted
see 3A - 1 and for Self Hosted
see 3A-2.
The following steps describe way to deploy Python Sample Project on Cloud Foundry. If you want to host your own application instead of sample project then skip to step 3(Push the App to CloudFoundry).
git clone https://github.com/mindsphere/mindsphere-python-sdk-examples.git
- Download Python SDK from Download.
- Unzip the downloaded file.
- Navigate to /mindsphere-python-sdk_1.0.3/modules/
- Copy .whl files of required dependent service/services in 'requirements' folder. (For this project(mindsphere-sdk-python-examples) we will need all the .whl files but you can choose to use only required subset of all available SDKs for your project.)
requirements
folder is already created for your convenience.- For convenience, requirements.txt is populated with relative path to copied dependencies.
There are two versions availalable for timeseries (3.3.2 and 3.4.1). Any of this two versions is suitable for
mindsphere-sdk-python-examples
. Make sure correct version is mentioned in requirements.txt beforepip install
.
-
Navigate to directory where cloned project directory is present. In this case navigate to sample-python-demo-app.
-
In order to push app to CF, user must login to cloudfoundry. To login user can opt for either of two ways.
- Jump to Login to CF
-
At this point you are successfully logged in CF.
-
Prepare manifest.yml file for pushing. File content pertinent to sample project are as :
-
For convenience, sample manifest.yml is added in root directory of project.
-
path
specifies where to look for application. Here in this case, our app is located insidemindsphere-python-sdk-examples
folder. -
Environment variables are listed under
env
. Since sample application demonstrates use of MindSphere SDKs, environemnt variables are only specific for Token Generation. In case of other application, user can append the list with his/her own environment variables. -
As mentioned in 1 - Prerequisites, either of Tenant Credentials/ Application Credentials would suffice for getting token.
-
In sample file variables for App credentials are mentioned but user can choose to either of two types available.
-
As here opting for App Credentials, user will not have values of all environment variables at this point. In this scenario either put some dummy values or do not add variables at all. CF provides command to set environment variables hence they can be set later on.
-
Now run the command
cf push
. -
Once application is successfully deployed check for app status using command
cf app routi
. -
Note down app URL displayed on screen.
During testing most common issue faced is : django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. If you face such issue then run the command
cf set-env <app> DISABLE_COLLECTSTATIC 1
and push the app again bycf push
.
- Self Hosted Applications are deployed by user on desired server.
- User must note down URL where application is hosted.
- Open the Developer Cockpit from the Launchpad and select the Dashboard tab.
- Click on Create new application.
- Select Type Standard and Infrastructure MindSphere Cloud Foundry if you have deployed application in cloud foundry. In case of self-hosted application select Self Hosted.
- Enter an arbitrary Display Name and an Internal Name which will be part of the application URL. The Internal Name cannot be changed after initial creation!
- Enter a version number.
- MindSphere supports a Major.Minor.Patch scheme.
- Versions must start with a major number >= 1.
- The version cannot be changed after saving.
- Upload an icon for your application.(Optional step)
- Enter the component name. The component name must be the same as specified in the manifest.yml file.
- In case of sample project
mindsphere-python-sdk-examples
component name will be routi and component url can be obtained by runningcf app routi
on command line. - In case of Self Hosted Application, component name and URL will be as per customer's deployment strategy.
- In case of sample project
- Add one endpoint for your component using /** to match all of your application paths.
- Set the content-security-policy according to the examples:
- For Europe1 : default-src 'self' *.eu1.mindsphere.io; style-src * 'unsafe-inline'; script-src 'self' 'unsafe-inline' *.eu1.mindsphere.io code.jquery.com cdnjs.cloudflare.com; font-src 'self' 'unsafe-inline' fonts.gstatic.com *.eu1.mindsphere.io; img-src * data:;
- For Europe2: default-src 'self' *.eu1.mindsphere.io *.eu2.mindsphere.io; style-src * 'unsafe-inline'; script-src 'self' 'unsafe-inline' *.eu1.mindsphere.io *.eu2.mindsphere.io code.jquery.com cdnjs.cloudflare.com; font-src 'self' 'unsafe-inline' fonts.gstatic.com *.eu1.mindsphere. *.eu2.mindsphere.io; img-src * data:;
- Click on Save.
- Switch to the Authorization Management tab.
- Select the application you just created.
- Create an application scope, e.g. .subtenant.
- Add the following Core roles to enable access to the respective APIs. For this project -
mindsphere-python-sdk-examples
, you will need following API roles. If required roles are not added then endpoints specific to those services will not work as expected.
- Switch to the Dashboard tab.
- Open the application details.
- Click on Register.
- Switch to the Authorization Management tab.
- Click on App Credentials tab.
- Click on Issue access button.
- Select Read And Write .
- Click on Submit button.
- You will be presented with client ID and client secret for application.
- Store these values at secure location as they are displayed only once.
- In case of App Credentials, at this point you have all the required values for corresponding environment variables -
MDSP_OS_VM_APP_NAME
,MDSP_OS_VM_APP_VERSION
,MDSP_KEY_STORE_CLIENT_ID
,MDSP_KEY_STORE_CLIENT_SECRET
,MDSP_HOST_TENANT
,MDSP_USER_TENANT
.
Sr. No. | Environment Variable | Value |
---|---|---|
1 | MDSP_OS_VM_APP_VERSION | Version you provided while creating application. |
2 | MDSP_OS_VM_APP_NAME | Internal name of your application(Can be seen in Application Details in Developer Cockpit). |
3 | MDSP_KEY_STORE_CLIENT_ID | App Client ID displayed on screen in last step. |
4 | MDSP_KEY_STORE_CLIENT_SECRET | App Client Secret displayed on screen in last step. |
5 | MDSP_HOST_TENANT | Name of the tenant you are currently working upon. |
6 | MDSP_USER_TENANT | This specifies the name of tenant which will use the application. Since we are currently in developing and testing phase, MDSP_USER_TENANT == MDSP_HOST_TENANT . |
- Set the values of this environment variables in Cloud Foundry.
cf set-env routi <VARIABLE-NAME> <VARIABLE-VALUE>
If you have provided any other value for application name then modify the command accordingly. As suggested by Cloud Foundry documentation, restage the app.
cf restage <APP-NAME>
- In case of Self Hosted application, you need to store these values as per deployment strategy. Also make sure this values can be accessed in application.
- Navigate to MindSphere Launchpad -> Settings -> Users
- Select a Developer you want to assign this application to. (You can assign it to yourself as well)
- Scroll down a bit and click on Edit direct assignments.
- In the Application Roles section, search your application by internal name.
- Select checkboxes for both admin and user.
- Click on Next.
- Click on Save.
Now concerned developer should be able to access the application via launchpad.
-
Navigate to MindSphere Launchpad.
-
Click on your application tile.
-
You should see something like :
-
By clicking on any endpoint showing on above image you should see like :
-
By clicking 'Try it out' button you can make API call by putting correct parameters and request body. You will get response like :
-
Domain url is Application URL displayed on Application details page.
- For creating an asset we will first create aspect type. From created aspect type we create Asset type. Next we finally create an asset based on created Asset type.
- First hit the endpoint PUT /assets/putaspect/{id}/{ifmatch}.
- If call is succesful, note down aspect id and aspect name from the response.
- Next, hit PUT /assets/putaassettype /{id}/{ifmatch}. Pass noted aspect id and aspect name value in payload for creating asset type.
- If call is succesful, note down id from the response.
- Next hit GET /assets/root to get root asset of the tenant. Note down id of an asset from the response.
- Finally we will now create an asset. Pass id from step 5 and parent id from step 6 in the payload for creating an asset.
- If asset creation is successful, you should see created asset in the call GET /assets/assets.
- All the created resources (aspect type, asset type and asset) are visible on Asset Manager application on MindSphere Launchpad.
Sample payload for endpoint is provided whenever required. For more information about payload, please refer
<service-name>/sampleinput
file. Fields in the payload can be deleted as long as all mandatory fields are passed. For now, swagger endpoints are provided for asset management, timeseries and event analytics service only. For other services, endpoints can be tried via entering url in browswer.
We require XSRF token for calling PUT, POST/PATCH, DELETE APIs (For GET endpoints, XSRF_TOKEN is not compulsory). The value of XSRF token can be passed in request header. This token is available in cookies by name
XSRF-TOKEN
. We have fetched this token from cache in the application and put it in request header.
The following steps describe the way to set up a sample project to test on local machine. This is to facilitate any bug resolution on local developer setup. Please follow prerequisite section for environment variables, how to get them and how to store them.
git clone https://github.com/mindsphere/mindsphere-python-sdk-examples.git
- Download Python SDK from Download.
- Unzip the downloaded file.
- Navigate to /mindsphere-python-sdk_1.0.3/modules/
- Copy .whl files of required dependent service/services in 'requirements' folder. (For this project(mindsphere-sdk-python-examples) we will need all the .whl files but you can choose to use only required subset of all available SDKs for your project.)
requirements
folder is already created for your convenience.- For convenience, requirements.txt is populated with relative path to copied dependencies.
- Navigate inside the root directory of project if you are not in there.
cd mindsphere-python-sdk-examples
- Run below command to install required dependencies mentioned in requirements.txt file.
pip install -r requirements.txt
If you face errors while
pip install -r requirements.txt
mentioning particular '.whl file not found' then kindly verify dependency file name in requirements folder and that mentioned in requirements.txt file. This could be also due to incorrect relative path mentioned in requirements.txt file. If so then modify path in requirements.txt wherever required.
python manage.py runserver
- Navigate to 'http://127.0.0.1:8000' (You can use any browser of your choice).
- Domain URL in this case will be 'http://127.0.0.1:8000'.
Sample payload for endpoint is provided whenever required. For more information about payload, please refer
<service-name>/sampleinput
file. For now, swagger endpoints are provided for asset management, timeseries and event analytics service only. For other services, endpoints can be tried via entering url in browser.
Please refer Handing over app to Operator Cockpit