Object oriented implementation for REST API in Python

The object oriented program for REST API is given below.

The following file name is OpenIAM.py.

import requests
import json
class OpenIAM:
def __init__(self, url, clientid, clientsecret):
self.url = url
self.clientid = clientid
self.clientsecret = clientsecret
self.token = None
def setToken(self):
# Config
token_url = f'http://{self.url}/idp/oauth2/token'
client_id = self.clientid
client_secret = self.clientsecret
# Request token
response = requests.post(token_url, data={
'grant_type': 'client_credentials'
}, auth=(client_id, client_secret))
if response.status_code == 200:
token_data = response.json()
access_token = token_data.get('access_token')
self.token = access_token
if not access_token:
print('Error: Access token not found in the response')
else:
print(f'Error: {response.status_code}, {response.text}')
def createUser(self, firstname, lastname, email):
url = f"http://{self.url}/webconsole/rest/api/prov/saveUser"
payload = json.dumps({
"emailAddresses":[
{
"email" : f"{email}",
"typeId" : "HOME_EMAIL",
"published" : True
}
],
"phones" :[
{
"areaCd": "1",
"phoneNbr": "1",
"phoneExt": "",
"typeId": "CELL_PHONE",
"countryCd": "1",
"isForSMS": True,
"published": True
}
],
"firstName":f"{firstname}",
"lastName":f"{lastname}",
"metadataTypeId": "DEFAULT_USER",
"notifyUserViaEmail": True
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def searchUser(self, firstname, lastname):
url = f"http://{self.url}/webconsole/rest/api/users/search"
payload = json.dumps({
"from": 0,
"size": 20,
"lastName": f"{lastname}",
"firstName": f"{firstname}",
"principal": "",
"email": "",
"roleIds": [],
"groupIds": [],
"organizationIds": [],
"employeeId": "",
"fromDirectoryLookup": False,
"sortBy": "name",
"orderBy": "ASC"
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def deleteUser(self, userid):
url = f"http://{self.url}/webconsole/rest/api/prov/removeUser"
payload = json.dumps({
"userId" : f"{userid}"
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def createRole(self, name):
url = f"http://{self.url}/webconsole/rest/api/roles/save"
payload = json.dumps({
"targetObject": {
"mdTypeId": "ACCESS_ROLE",
"name": f"{name}",
"managedSysId": "0"
}
})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def deleteRole(self, roleid):
url = f"http://{self.url}/webconsole/rest/api/roles/{roleid}"
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.token}'
}
response = requests.delete(url, headers=headers)
print(response.text)
def getURL(self):
return self.url
def setURL(self, newURL):
self.url = newURL
def getClientid(self):
return self.clientid
def setClientid(self, newclientid):
self.clientid = newclientid
def getClientSecret(self):
return self.clientsecret
def setClientSecret(self, newclientsecret):
self.clientsecret = newclientsecret

Another example is for the file tester.py.

from OpenIAM import OpenIAM
# Initialize OpenIAM object (openiam-instance-url, clientid, clientsecret)
test = OpenIAM("url", 'clientid', 'clientsecret')
test.setToken() # Generates auth token while also limiting the number of token calls, be sure to run this
# Create user with firstname, lastname, email
#test.createUser("apitest1", "bigtester", "bigtest@openiam.com")
# Search user by lastname
#test.searchUser("", "bigtester")
# Delete a user
#test.deleteUser("userid")
# Create a role
#test.createRole("roletest2")
# Delete a role
#test.deleteRole("roleid")
# Setters
#test.setClientid("newid")
#test.setClientSecret("newclientsecret")
#test.setURL("newURL")
# Getters
#print(test.getClientid())
#print(test.getClientSecret())
#print(test.getURL())