Created
December 21, 2015 23:15
-
-
Save olsnacky/2c312ac12e5ec1efdb88 to your computer and use it in GitHub Desktop.
Working code example of uploading an image to the Imagus Web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2, json, cookielib, os, sys | |
try: | |
import requests | |
except: | |
print 'Please install the requests module (pip install requests)' | |
sys.exit() | |
# supress unverified warning from requests | |
requests.packages.urllib3.disable_warnings() | |
# the api's base url | |
BASE_URL = 'https://api.imagus.com.au/api/v1/' | |
# user constants | |
LOGINS_URL = '%slogins' % (BASE_URL) | |
USER_URL = '%suser' % (BASE_URL) | |
# image uploading constants | |
LOCAL_IMAGES_PATH = '' | |
UPLOAD_URL = '%simage/upload' % (BASE_URL) | |
# our credentials | |
USER_ID = 'xxx' | |
USER_PASSWORD = 'xxx' | |
# get the various login urls | |
response = urllib2.urlopen(LOGINS_URL) | |
data = json.load(response)['logins'] | |
# this method will build any request that requires authorisation/authentication | |
# it adds our captured cookie to the request | |
# we'll use this method when we need to use endpoints that require authentication/authorisation | |
def build_authorised_request(url, cookie): | |
request = urllib2.Request(url) | |
request.add_header('Cookie', cookie) | |
return request | |
# this method will do the heavy lifting of uploading a file to the Imagus service | |
def upload_image(file_name, cookie): | |
requests_header = {'Cookie': cookie} | |
probe_path = os.path.join(LOCAL_IMAGES_PATH, file_name) | |
resp = requests.post('%s?file_name=%s' % (UPLOAD_URL, probe_path), data=open(probe_path, 'rb'), headers=requests_header, verify=False) | |
return json.loads(resp.text)['token'] | |
# find the login url that suits our needs | |
auth_url = None | |
LOGIN_TYPE = 'api' | |
LOGIN_NAME = 'imagus' | |
for login in data: | |
if login['type'] == LOGIN_TYPE and login['name'] == LOGIN_NAME: | |
auth_url = login['auth_url'] | |
# if we have the right auth url, we're in business | |
if auth_url is not None: | |
# log in the user | |
auth_url = '%s&username=%s&password=%s' % (auth_url, USER_ID, USER_PASSWORD) | |
response = urllib2.urlopen(auth_url) | |
# get the user's cookie | |
cookie = response.info()['Set-Cookie'] | |
# show the user is logged in | |
# we need to pass the cookie for authorisation/authentication | |
response = urllib2.urlopen(build_authorised_request(USER_URL, cookie)) | |
data = json.load(response) | |
print('Logged in as: %s %s' % (data['first_name'], data['second_name'])) | |
# get the token | |
image_file = 'xxx' | |
token = upload_image(image_file, cookie) | |
print('The token for your uploaded image is: %s' % token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment