API calls examples in Python

With the fetchtoken.py program created on the previous step, you can now do a simple API call.

Creating a user

  1. Start with creating a user in a new file. Create a new file named CreateUser.py and be sure to set URL to your OpenIAM instance URL. The program should look as follows.
import requests
import json
from fetchtoken import getToken
url = "http://your-openiam-instance/webconsole/rest/api/prov/saveUser"
payload = json.dumps({
"emailAddresses":[
{
"email" : "tutorialemail@openiam.com",
"typeId" : "HOME_EMAIL",
"published" : True
}
],
"phones" :[
{
"areaCd": "1",
"phoneNbr": "1",
"phoneExt": "",
"typeId": "CELL_PHONE",
"countryCd": "1",
"isForSMS": True,
"published": True
}
],
"firstName":"user",
"lastName":"tutorial",
"metadataTypeId": "DEFAULT_USER",
"notifyUserViaEmail": True
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {getToken()}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

No you have created a user with the name user tutorial and tutorialemail@openiam.com email.

  1. Run the program. You will get a similar response.
PS C:\Users\maxim\OneDrive\Desktop\openiam\restapi> python CreateUser.py
{"primaryKey":null,"status":200,"errorList":null,"redirectURL":"editUser?id=4028d6bb90cbd50e0190ddd39fdf0690","successToken":{"message":"USER_INFO_SAVED","params":null},"successMessage":"User information was saved successfully<br/>Provisioning:","contextValues":{"checkStatusInProgress":true,"userId":"4028d6bb90cbd50e0190ddd39fdf0690"},"stackTrace":null,"possibleErrors":null,"error":false}

Your user has been successfully created.

Searching for a user

To search for a user, create a new SearchUser.py file. Make sure to set the URL to your OpenIAM instance. The program in the file will look as follows.

import requests
import json
from fetchtoken import getToken
url = "http://your-openiam-instance/webconsole/rest/api/users/search"
payload = json.dumps({
"from": 0,
"size": 20,
"lastName": "tutorial",
"firstName": None,
"principal": "",
"email": "",
"roleIds": [],
"groupIds": [],
"organizationIds": [],
"employeeId": "",
"fromDirectoryLookup": False,
"sortBy": "name",
"orderBy": "ASC"
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {getToken()}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

After running the program, you should get the following response.

PS C:\Users\maxim\OneDrive\Desktop\openiam\restapi> python SearchUser.py
{"error":null,"page":0,"from":0,"size":1,"pageSize":1,"beans":[{"id":"4028d6bb90cbd50e0190ddd39fdf0690","beanType":"UserBean","operation":"NO_CHANGE","classification":null,"middleInit":null,"accessRightIds":null,"name":"user tutorial","phone":"1 (1) 1 ","email":"tutorialemail@openiam.com","userStatus":"PENDING_INITIAL_LOGIN","accountStatus":null,"principal":"user.tutorial","organization":null,"department":null,"division":null,"title":"","employeeId":"","startDate":"","accessRightStartDate":null,"accessRightEndDate":null,"organizations":null,"location":null,"firstName":"user","lastName":"tutorial","nickname":null,"birthdate":null,"birthdateAsStr":null,"maidenName":null,"suffix":null,"sex":null,"gender":null,"jobCodeId":null,"prefix":null,"prefixLastName":null,"partnerName":null,"locationCd":null,"userTypeInd":null,"employeeTypeId":null,"employeeTypeName":null,"costCenter":null,"alternateContactId":null,"alternativeStartDate":null,"alternativeEndDate":null,"certificationDelegateId":null,"certificationDelegateEndDate":null,"certificationDelegateStartDate":null,"supervisorMdTypeName":"DEFAULT_USER","supervisorsNameList":null,"accountTypeName":null,"visible":false,"hasRelatedAccounts":false,"relationship":null,"mdTypeId":"DEFAULT_USER","applicationNames":null,"accessRightStartDateStr":null,"accessRightEndDateStr":null}],"emptySearchBean":false,"beansMap":null}

From the response you can see that the tutorial user is displayed.

Other API calls examples

Delete User

import requests
import json
from fetchtoken import getToken
url = "http://your-openiam-instance/webconsole/rest/api/prov/removeUser"
payload = json.dumps({
"userId" : "user-id-to-be-deleted"
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {getToken()}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

Create role

import requests
import json
from fetchtoken import getToken
url = "http://your-openiam-instance/webconsole/rest/api/roles/save"
payload = json.dumps({
"targetObject": {
"mdTypeId": "ACCESS_ROLE",
"name": "role-name",
"managedSysId": "0"
}
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {getToken()}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

Delete Role

Here, make sure to set roleid in the URL.

import requests
from fetchtoken import getToken
url = "http://your-openiam-instance/webconsole/rest/api/roles/roleid"
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {getToken()}'
}
response = requests.delete(url, headers=headers)
print(response.text)