Jump to content

Python entityUpdateRecord xmlmc parameters


Recommended Posts

Being fairly new to Python I am trying to work out how to populate the xmlmc object with the required Input Parameters in order to call the 'entityUpdateRecord' in particular the 'primaryEntityData' . I can see what is needs via the API documentation but there is no Python code examples.

https://api.hornbill.com/data/?op=entityUpdateRecord

I am wanting to be be able to populate a number of custom field values on a request.

Has anyone got any Python code examples that would help?

Cheers

Martyn

Link to comment
Share on other sites

Hi @Martyn Houghton,

I've just had a look at the Python API Library, and it appears that it doesn't support complex parameters - which makes it pretty useless :( I'll add a re-write of that to the list, but for the time being I've knocked up a very basic example of how you can call that API in Python without the XMLMC module (or indeed XML at all), using a small function to make the call using the requests module:

# entityUpdateRecord.py
# This script is an example of how to call a Hornbill API in Python

import requests
import sys

apiKey = 'apikey'           # This is the (case sensitive) API key to use to authenticate the API calls against the Hornbill instance
instanceId = 'instanceid'   # This is the (case sensitive) ID of your Hornbill instance

# Get API endpoint from the instanceId
URL = 'https://files.hornbill.com/instances/{instanceId}/zoneinfo'.format(instanceId=instanceId)
try:
    endpoint = requests.get(url = URL).json()["zoneinfo"]["endpoint"]
except requests.exceptions.RequestException as e:
    sys.exit('Unexpected response when attempting to retrieve Hornbill Zone Information: ' + e)

# Define a function to make API calls
def invokeXmlmc(service, method, params):
    xmlmcEndpoint = endpoint + "xmlmc/{service}/?method={method}" 
    URL = xmlmcEndpoint.format(service=service, method=method)
    headers = {
        'Content-type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'ESP-APIKEY ' + apiKey
    }
    data = {
        'methodCall':{
            '@service':service,
            '@method':method,
            'params':params
        }
    }
    try:
        response = requests.post(url=URL, json=data, headers=headers)
        if 200 >= response.status_code <= 299:
            return response.json()
        else:
            return {
                '@status': False,
                'state': {
                    'error': 'Unexpected status returned from call to {service}::{method}: {statusCode}'.format(service=service, method=method, statusCode=response.status_code)
                }
            } 
    except requests.exceptions.RequestException as e:
        return {
            '@status': False,
            'state': {
                'error': 'Unexpected response from call to {service}::{method}: {errorString}'.format(service=service, method=method, errorString=e)
            }
        }
    
# Build API Params object in JSON
paramsJson = {
    'application': 'com.hornbill.servicemanager',
    'entity': 'Requests',
    'primaryEntityData': {
        'record': {
            'h_pk_reference': 'IN00013316',
            'h_summary': 'This is the summary, as set by my Python script'
        }
    }
}

# Make API Call
apiResponse = invokeXmlmc('data', 'entityUpdateRecord', paramsJson)
if apiResponse['@status'] != True:
    sys.exit(apiResponse['state']['error'])

# Print the response to show it worked
print(apiResponse)

Hope that helps, let me know if you need any of it explaining.

Cheers,

Steve

Link to comment
Share on other sites

Hi @Martyn Houghton,

The Hornbill Python API Library has been updated on Github to support complex input parameters. Here's an example of its use:

from xmlmc import XmlmcService
from xmlmc import XmlmcHelper

xmlmc = XmlmcService("yourinstanceid")
xmlmc.set_api_key("yourapikey")
xmlmc.add_param("application", "com.hornbill.servicemanager")
xmlmc.add_param("entity", "Requests")
elem = xmlmc.add_param("primaryEntityData")
childElem = elem.add_child("record")
childElem.add_child("h_pk_reference", "IN00013316")
childElem.add_child("h_summary", "This is the summary, as set by my Python script")
json_string = xmlmc.invoke("data", "entityUpdateRecord")
if XmlmcHelper.is_call_success(json_string):
    auditTransactionId = XmlmcHelper.get_param_value(json_string, "params/auditTransactionId")
    print(auditTransactionId)
else:
    print(XmlmcHelper.get_error_message(json_string))

Cheers,

Steve

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...