Using the GeoEvent Admin API with Python

4419
5
06-21-2019 01:20 PM

Using the GeoEvent Admin API with Python

Working with customers I've had several use cases to use the GeoEvent Admin API.  For example, a GeoEvent input may be using a URL that contains an API key.  This key may expire after a number of days and needs to be recreated.  Instead of manually editing the GeoEvent input and updating the API key, you can use the GeoEvent Admin API to do this automatically via a Python script.  You can access the GeoEvent Admin API by going to the following URL:

https://<GeoEvent Server Full Qualified Domain Name>:6143/geoevent/admin/api/index.html 

This will provide a list of all supported operations and examples on how to use them.  In this example, I will show how you can update the URL in a GeoEvent Input.  The GeoEvent Input is a Poll an External Website for GeoJSON.

1.  First you will need to get the Name of the Input.  This is a GUID and can be found a couple places.  If you click on the GeoEvent Input, the URL will contain the GUID after the name parameter:

https://geoevent.esri.com:6143/geoevent/manager/input.html?name=125626c5-7f42-4075-8e3b-20903982243a&type=esri-external-geojson-poll&mode=inbound&running=STARTED&clone=false

You can also obtain the input name by going to the GeoEvent REST Services Directory (https://geoevent.esri.com:6143/geoevent/rest/😞

2.  Once you have this, you execute the following python script after updating the # Variables:

import requests, json

# Disable warnings
requests.packages.urllib3.disable_warnings()

# Variables
geoeventServer = 'geoevent.esri.com'
username = "siteadmin"
password = "******"
geoeventInput = '125626c5-7f42-4075-8e3b-20903982243a'
newURL = 'https://api.helios.earth/v1/cameras?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUz'

# Generate Token
tokenURL = 'https://{0}:6443/arcgis/tokens/'.format(geoeventServer)
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://{0}:6143/geoevent/admin'.format(geoeventServer)}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']


# Request URLs
header = {'Content-Type':'application/json', 'GeoEventAuthorization':token}
putURL = 'https://{0}:6143/geoevent/admin/input/'.format(geoeventServer)
inputURL = putURL + geoeventInput


# Get Input URL
r = requests.get(inputURL, headers=header,  verify=False)
response = json.loads(r.content)
response['transport']['properties'][3]['value'] = newURL


# Update Input URL
r = requests.put(putURL, data=json.dumps(response), headers=header, verify=False)
if r.status_code == 200:
    print("Successfully updated input")
else:
    print("Error occurred updating input.  Error: {0}".format(r.status_code))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Variables explained:

VariableExplanation
geoeventServerFully qualified domain name of GeoEvent Server
usernamePrimary Site Administrator username
passwordPrimary Site Administrator password
geoeventInputGUID for GeoEvent input
newURLthe new URL the GeoEvent input will be updated to

Note:  typically there would be additional code within this script to generate the new access_token in the newURL variable.

The # Genereate Token portion of code will generate an ArcGIS Server token using the credentials specified.  Note:  This is the ArcGIS Server instance on the GeoEvent server.

# Generate Token
tokenURL = 'https://{0}:6443/arcgis/tokens/'.format(geoeventServer)
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://{0}:6143/geoevent/admin'.format(geoeventServer)}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

After the token is generated, it is specified in an HTTP header:  GeoEventAuthorization: token. 

A get request is made using the GeoEvent input URL.  This will return JSON, which you can update by specifying the appropriate JSON object, i.e.:

response['transport']['properties'][3]['value'] = newURL‍‍‍‍‍‍

Finally, a put request is made with the updated JSON to update the Input's URL.

# Update Input URL
r = requests.put(putURL, data=json.dumps(response), headers=header, verify=False)
if r.status_code == 200:
    print("Successfully updated input")
else:
    print("Error occurred updating input.  Error: {0}".format(r.status_code))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The script could then be executed by Windows Task Scheduler on a given interval (i.e. once every x days).  The input URL would update and you would no longer have to worry about the access token expiring.

Comments
SatwikMishra
Emerging Contributor

Hi Jake Skinner,

Can we try the same thing for output connectors as well? For example I want to update the URL for an output connector, 'Push JSON to an External Website', what all modifications have to be made in the given code. 

Thanks,

Satwik

AdamRepsher
Frequent Contributor

Hi Jake Skinner‌!

I need to study this one carefully.  It has the potential of helping me to get GeoEvent inputs re-started if the source shuts down for whatever reason.  Right now, I have to remote in at all hours of day or night after GE notifies me that there is no more data being processed, just to restart an input.    Thank you for the little boost of knowledge.

--Adam

by Anonymous User
Not applicable

Thanks Jake,

This post really helps give me an idea in my case. 

What I did for the client was  writing a python script to check the status of the input connectors constantly polling data, if the value of TimeSinceLast exceeds certain value, the script would try to restart the input for a try, if issue still there,  an alert message would export to CSV file into a specified directory. And this directory was watching by an input connector for any new CSV file. As soon as CSV file generated, an output would send email to administrators. 

In addition, the script could update the property of send email output to modify the content of the email body dynamically. 

All in all, your post helpful, but did find your lines code need to be updated in my case. I'm using 10.5.1 GEP.  

1. the token Url need to be changed 

Changing from tokenURL = 'https://{0}:6443/arcgis/admin/generateToken/'.format(geoeventServer

to

tokenURL = 'https://{0}:6443/arcgis/tokens/'.format(geoeventServer)

Otherwise will throw error in subequent requests. 

2. Explicit JSON format in URL

Needs to put the “./json” to return the JSON format response, otherwise, the load()method will throw an error.

 

inputURL = putURL + geoeventInput+"/.json"

Cheers

Minbin

AdamRepsher
Frequent Contributor

Minbin Jiang‌,

Would you mind sharing that python script?

Thank you,

--Adam

JakeSkinner
Esri Esteemed Contributor

Satwik Mishra‌ you will need to update the geoeventInput variable with the output ID, and line 30 with the following:

response['transport']['properties'][2]['value']= newURL
Version history
Last update:
‎12-12-2021 03:50 AM
Updated by: