Package: geotrigger-python
A simple Python client library for interacting with the ArcGIS
Geotrigger Service via theGeotriggerClientobject.The Geotrigger Service is a cloud-hosted geofencing platform, which
sends push notifications or notifies a remote server when a device
enters or exits an area.The Geotrigger API manages Application and Device information and
permissions, as well as providing access to create, update, and list
information about Triggers, Tags, and Device Locations.For more information please refer to theGeotrigger Service
Documentation.FeaturesHandles authentication and refreshing of credentialsSupports making requests as an application, allowing for full
management of devices, triggers, tags, and permissionsAlso supports making requests as a device, which can be useful for
testing purposesDependenciesRequests (>= 2.1.0)For running tests, you’ll also need:Mock (>= 1.0.1)InstallationYou can installgeotrigger-pythonfrom PyPI using the following
command:pipinstallgeotrigger-pythonIt’s also possible to install from a clone of this repository by runningsetup.py installorsetup.py develop.ExamplesUsing the GeotriggerClient as an applicationThis method of using theGeotriggerClientis for server-side apps, acting as
the sole owner of your ArcGIS application.Before continuing, you’ll need to find theclient_idandclient_secretfor your ArcGIS application on theArcGIS for
Developerssite.
You’ll find them in theAPI Accesssection of your applications
details.Please make sure to keep your client_secret secure.If a third
party obtains your client secret, the will be able to do anything they
want to your Geotrigger application. Yourclient_secretshould only
be included in server-side applications and should never be distributed
as part of a client-side web or mobile application.You will need to fill in values for the variable names given in
all-caps.fromgeotriggerimportGeotriggerClient# Create a GeotriggerClient as an Applicationgt=GeotriggerClient(CLIENT_ID,CLIENT_SECRET)# Fetch a list of all triggers in this application.triggers=gt.request('trigger/list')# Print all the triggers and any tags applied to them.print"\nFound%dtriggers:"%len(triggers['triggers'])fortintriggers['triggers']:print"-%s(%s)"%(t['triggerId'],",".join(t['tags']))# Add "testing123" tag to all of the triggers that we fetched above.triggers_updated=gt.request('trigger/update',{'triggerIds':[t['triggerId']fortintriggers['triggers']],'addTags':TAG})# Print the updated triggers.print"\nUpdated%dtriggers:"%len(triggers_updated['triggers'])fortintriggers_updated['triggers']:print"-%s(%s)"%(t['triggerId'],",".join(t['tags']))# Delete the "testing123" tag from the application.tags_deleted=gt.request('tag/delete',{'tags':TAG})print'\nDeleted tags: "%s"'%", ".join(tags_deleted.keys())Using the GeotriggerClient as a DeviceTheGeotriggerClientcan also be used as if it were a device, which
will allow you to send location updates and fire triggers, but you will
not be able to receive any Geotrigger notifications, because they are sent as
push messages to actual mobile devices. You can use thetrigger/historyAPI route or configure your triggers with acallbackUrlin order to
observe that triggers are being fired.You’ll only need theclient_idfor your application in order to use
theGeotriggerClientas a device.For testing callback triggers, you can use the handyRequestBinservice. Create a new bin and
provide its URL as thecallbackUrlwhen creating a trigger.You will need to fill in values for the variable names given in
all-caps.fromgeotriggerimportGeotriggerClient# Create a GeotriggerClient as a devicegt=GeotriggerClient(CLIENT_ID)# Default tags are created for all devices and triggers. Device default tags# can be used when you want to allow devices to create triggers that only they# can fire. Default tags look like: 'device:device_id' or 'trigger:trigger_id'device_tag='device:%s'%gt.session.device_id# Build a callback trigger, using your default tag and RequestBin URL.esri_hq={'condition':{'geo':{'latitude':34.0562,'longitude':-117.1956,'distance':100},'direction':'enter'},'action':{'callbackUrl':CALLBACK_URL},'setTags':device_tag}# Post the trigger to the Geotrigger APItrigger=gt.request('trigger/create',esri_hq)printtrigger# Construct a fake location update to send to the Geotrigger API.# Supplying a previous location is not strictly required, but will speed up# trigger processing by avoiding a database lookup.location_update={'previous':{'timestamp':datetime.now().isoformat(),'latitude':45.5165,'longitude':-122.6764,'accuracy':5,},'locations':[{'timestamp':datetime.now().isoformat(),'latitude':34.0562,'longitude':-117.1956,'accuracy':5,}]}# Send the location update.update_response=gt.request('location/update',location_update)printupdate_responseShortly after running the above code, you will see a POST to your
callback url.Advanced GeotriggerClient usageIf you already have an ArcGIS Applicationaccess_tokenthat you’d
like to use to create aGeotriggerClient, pass in aGeotriggerApplicationas thesessionkwarg. You may want to do this if
you are integrating Geotrigger functionality into an application that
already obtains credentials from ArcGIS Online.Similarly, if you want to impersonate an existing device for which you
already have aclient_id,device_id,access_token, andrefresh_token, you can create your ownGeotriggerDeviceto pass
into theGeotriggerClient. This can be used to debug apps that are
being developed with the Geotrigger SDKs for Android and iOS.fromgeotriggerimportGeotriggerClient,GeotriggerApplication,GeotriggerDeviceapp=GeotriggerApplication(CLIENT_ID,CLIENT_SECRET,ACCESS_TOKEN)app_client=GeotriggerClient(session=app)device=GeotriggerDevice(CLIENT_ID,DEVICE_ID,ACCESS_TOKEN,REFRESH_TOKEN)device_client=GeotriggerClient(session=device)IssuesFind a bug or want to request a new feature? Please let us know by submitting an issue.ContributingEsri welcomes contributions from anyone and everyone. Please see ourguidelines for contributing.LicensingCopyright 2013 EsriLicensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.A copy of the license is available in the LICENSE file.