Skip to content

Instantly share code, notes, and snippets.

@joshmarlow
Created June 9, 2015 15:14
Show Gist options
  • Save joshmarlow/ee602ed12971e8a69f11 to your computer and use it in GitHub Desktop.
Save joshmarlow/ee602ed12971e8a69f11 to your computer and use it in GitHub Desktop.
Ambition API - Python Example
#!/bin/env python
#
# ambition_api_python_example.py
# Simple script demonstrating how to upload data to the Ambition Data API.
import json
import sys
import urllib2
def upload_data(endpoint, data_to_upload, auth_token):
# Add the Authorization header
headers = {
'Authorization': 'Token {0}'.format(auth_token),
'Content-Type': 'application/json',
}
data = {
'depot': 'custom_depot',
'data': data_to_upload,
}
request = urllib2.Request(endpoint, headers=headers)
response = urllib2.urlopen(request, json.dumps(data))
content = response.read()
return response.code, content
def main():
auth_token = <AUTH-TOKEN>
if len(sys.argv) < 2:
print 'usage: upload_to_ambition.py <FILENAME>'
return
filename = sys.argv[1]
endpoint = 'https://<SUBDOMAIN>.ambition.com/public-api/integration/v1/data/new/'
with open(filename, 'rb') as fin:
data_to_upload = fin.read()
response = upload_data(endpoint, data_to_upload, auth_token)
if response[0] == 201:
print 'Successfully uploaded'
else:
print 'Failed to upload'
print response[0]
print response[1]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment