# Server. The token contains a bearer token code once authenticated, and this is used in all
# subsequent REST API calls. This code also demonstrates that the token can be cached
# and reused if not expired.
###########################################################################################

#############################################
# Change the following to suit your instance
#############################################

base_uri = "http://<Your Cherwell Host here>"
username = "******"
password = "******"
api_key = "<Your Cherwell REST API Client Key here>"

# Create a new CherwellClient connection
cherwell_client = CherwellClient.Connection(base_uri, api_key, username,
                                            password)

# Show the current token
print("Current Token is: {}\n".format(cherwell_client.token))

# The token is only retrieved when a api call is made for the first time,
# to do so - get an existing Business Object ID - assuming here its not already cached
print("Incident Business Object Id is: {}".format(
    cherwell_client.get_business_object_id("Incident2")))

# Show the current token - if authenticated, should now have a value
print("Token is now: {}\n".format(cherwell_client.token))

# Check whether the token is expired or not
print("Current Token Expiry time in GMT is: {}".format(
    cherwell_client.token.token_expiry_gmt()))
#########################################################################################
# This example demonstrates how the Cherwell API Connection object can be used to
# create a new business object using the 'BusinessObject Class
###########################################################################################

#############################################
# Change the following to suit your instance
#############################################

base_uri = "http://<Your Cherwell Host here>"
username = "******"
password = "******"
api_key = "<Your Cherwell REST API Client Key here>"

# Create a new CherwellClient connection
cherwell_client = CherwellClient.Connection(base_uri, api_key, username,
                                            password)

# Create a new instance of an Incident
incident = cherwell_client.get_new_business_object("Incident")

# show there is no Record saved currently
print("########################")
print("Before Saving new Record")
print("########################")
print("BusObId: {}".format(incident.busObId))
print("RecId: {}".format(incident.busObRecId))
print("PublicId: {}\n".format(incident.busObPublicId))

# Set the properties of the new incident
incident.CustomerDisplayName = "John Allard"
incident.Description = "This is a test incident"
#########################################################################################
# This example demonstrates how the CherwellAPI Connection object can be used to
# search for and retrieve one or more business objects matching a search query
###########################################################################################

#############################################
# Change the following to suit your instance
#############################################

base_uri = "http://<Your Cherwell Host here>"
username = "******"
password = "******"
api_key = "<Your Cherwell REST API Client Key here>"

#Create Encrypted Credentials (Only Have to Run Once)
CherwellCredentials.create_encrypted_cherwell_credentials(password,api_key)

# Create a new Cherwellclient connection
cherwell_client = CherwellClient.Connection(base_uri, None, username, None)

# Pass the association, scope, saved search name to the CherwellClient's get_saved_search_results
num_records, business_objects = cherwell_client.get_saved_search_results("FederationRegistration","Global","All Active Federation Sources")

# Print number of records returned
print("Number of records: {}".format(num_records))

# Loop through the records returned
for business_object in business_objects:
    print(business_object)