API Adapter is a component to define a function template for the API Parser and API Server to work with a third-party system.

To define a new API adapter, complete the following steps.

1.Log in to the System Management page.

2.In the System Management page, select Operations > API Adapters from the quick access toolbar.

3.Enter a name in the Adapter Name field, such as ServiceNow Incidents.

4.Enter a description of the API adapter in the Description field.

5.Enter the API script in the Script field. The sample code can be:

import requests
import json
import pythonutil
  
def extract_param(param):
    # The NetBrain initial parameters with customized fields.
    if isinstance(param, str):
        param = json.loads(param)  
    #username, password, endpoint are build-in keywords in initial param.
    username = ''
    password = ''
    endpoint = ''
    #callParam is customized fields.
    api_param = {}
    apiServerId = ''
    servInfo = {}
    if 'apiServerId' in param:
        apiServerId = param['apiServerId']
        servInfo = pythonutil.GetApiServerInfo(apiServerId)
        username = servInfo['username']
        password = servInfo['password']
        endpoint = servInfo['endpoint']
        api_params = param['api_params']
    else:
        username = param["username"]
        password = param["password"]
        endpoint = param["endpoint"]
        api_params = param['api_params']
    return (endpoint, username, password, api_params)
 
def get_data(param):
    # headers = {"Content-Type": "application/json", "Accept": "application/json"}
    headers = {}
    endpoint, username, password, api_params = extract_param(param)
    full_url = endpoint + api_params['api_uri']
    url_params = {}
    if 'url_params' in param['api_params']:
        url_params = api_params['url_params']
    url_params['username'] = username
    url_params['password'] = password
    try:            
        response = requests.get(full_url, headers=headers, params=url_params, verify=False)
        if response.status_code == 200:
            json_response = response.json()
            return json_response['sensors']
        else:
            return response.text
    except Exception as e:
        return str(e)
 
# API Domain Manager Test function definition.
def _test(param):
    test_param = json.loads(param)
    test_param["api_params"] = {'api_uri':'/api/table.json?content=sensors&columns=objid&filter_device=US-BOS-R1'}
    result = json.dumps(get_data(test_param))
    #"isFailed" and "msg" key fileds are the required.
    rtn = {"isFailed":False, "msg":result}
    return json.dumps(rtn)

6.Click Save to save the definition.

Tip: The system provides a built-in API adapter for ServiceNow instance, which contains HTTP Get function (get_data()). The HTTP request uses Basic Authentication to be authenticated by PRTG for each API call. The username and password information are inherited from an API Server instance defined in NetBrain API Server Manager.

 

See also:

Adding an API Server