def application_example():
    # Create the GeotriggerClient using a client_id and client_secret.
    # This will allow you to use the full Geotrigger API to administer your
    # application.
    print 'Creating GeotriggerClient as an application.'
    gt = 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 %d triggers:" % len(triggers['triggers'])
    for t in triggers['triggers']:
        print "- %s (%s)" % (t['triggerId'], ",".join(t['tags']))

    # Add "testing123" tag to all of the triggers that we just fetched.
    triggers_updated = gt.request('trigger/update', {
        'triggerIds': [t['triggerId'] for t in triggers['triggers']],
        'addTags': TAG
    })

    # Print the updated triggers.
    print "\nUpdated %d triggers:" % len(triggers_updated['triggers'])
    for t in triggers_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())
def device_example():
    # Create the GeotriggerClient using only a client_id, a device will be
    # registered for you.
    print 'Creating GeotriggerClient as a device.'
    gt = GeotriggerClient(CLIENT_ID)

    # Get the default tag for our fake device so that we can apply it to
    # the trigger that we'll be creating.
    device_tag = 'device:%s' % gt.session.device_id

    # Create a unique URL for this example on RequestBin, a super handy HTTP
    # request inspection tool. (See: http://requestb.in)
    r = requests.post('http://requestb.in/api/v1/bins', {'private': True})
    if r.status_code == 200:
        bin = r.json()
        requestbin_url = 'http://requestb.in/%s' % bin['name']
    else:
        print "Could not set up a requestb.in URL to use for trigger callback."
        print "(%d) %s" % (r.status_code, r.text)
        sys.exit(1)

    # Build trigger
    esri_hq = {
        'condition': {
            'geo': {
                'latitude': 34.0562,
                'longitude': -117.1956,
                'distance': 100
            },
            'direction': 'enter'
        },
        'action': {
            'callbackUrl': requestbin_url
        },
        'setTags': device_tag
    }

    # Post the trigger to the Geotrigger API
    print 'Creating trigger...'
    trigger_response = gt.request('trigger/create', esri_hq)
    print trigger_response

    # 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 if provided.
    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.
    print 'Sending location update...'
    update_response = gt.request('location/update', location_update)
    print update_response

    # Visit the requestb.in url to inspect the request made to the callback url.
    print 'Check %s?inspect for the trigger callback!' % requestbin_url