Created
May 23, 2012 15:43
-
-
Save benjchristensen/2776021 to your computer and use it in GitHub Desktop.
Common HTTP Client Functions for Python CLI
This file contains hidden or 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 argparse | |
import os | |
import sys | |
import urllib2 | |
import json | |
import collections | |
# initialize the argParser with common arguments such as environment | |
def initArgParser(scriptDescription): | |
parser = argparse.ArgumentParser(description=scriptDescription) | |
parser.add_argument('--environment', '-e', metavar='TEST/PROD', | |
help='The environment to execute against.') | |
parser.add_argument('--host', metavar='http((s)://host:port (if environment not defined))', | |
help='If a specific host rather than environment is desired.') | |
return parser | |
# validate and parse the arguments and retrieve back an object containing the arguments | |
# Default arguments included are: | |
def validateAndGetArguments(argParser): | |
args = argParser.parse_args() | |
#print args | |
if args.host == None and args.environment == None: | |
# no arguments passed in so let's try a system environment variable | |
if os.environ['API_HOSTNAME'] != None: | |
args.host = os.environ['API_HOSTNAME'] | |
else: | |
print "ERROR: No hostname or environment defined." | |
argParser.print_usage() | |
sys.exit(-1) | |
return args | |
# Return the the base URL configured for the environment defined by arguments | |
# For example: https://api-test.domain.com/ | |
# Each script will append to this URL whatever suffix is desired and then pass it to createRequest. | |
def getBaseURL(args): | |
# get hostname | |
if args.host != None: | |
url = args.host | |
elif args.environment != None: | |
if args.environment == 'PROD': | |
url = "https://api.domain.com/" | |
elif args.environment == 'TEST': | |
url = "https://api-test.domain.com/" | |
return url; | |
# Create the HTTP Request object for the given url and args. | |
# It sets up the defaults for security credentials and JSON types | |
# If the Content-Type needs to be different, override it on the request object returned from this method. | |
# For example: | |
# request.add_header("Content-Type", "text/csv") | |
def createRequest(url, args, method): | |
request = urllib2.Request(url) | |
# headers and cookies | |
request.add_header("Accept", "application/json") | |
request.add_header("Content-Type", "application/json") | |
request.add_header('Cookie', 'securityToken=' + args.securityToken) | |
if method != None: | |
# urllib2 somehow doesn't natively support setting methods so we override it | |
request.get_method = lambda: method | |
else: | |
method = 'GET' # so we can use it in the curl command below | |
if(args.outputCurl): | |
print "Curl Command => " | |
print " curl -v -X " + method + " -H \"Accept:application/json\" -H \"Content-Type:application/json\" -w \"\\n\\n\" -b \"securityToken=" + args.securityToken + ";\" " + request.get_full_url() | |
print "" | |
return request | |
# If a file is being posted, it can be added via this | |
def addFileToRequest(request, filePath): | |
try: | |
fileString = open(filePath).read() | |
except Exception as e: | |
print "Error => Can not read from file: " + filePath | |
sys.exit(-1) | |
# attach file | |
request.add_data(fileString) | |
# Execute the request, perform error handling if needed and print out the result as formatted JSON. | |
def executeRequest(request): | |
# execute request | |
print "Executing " + request.get_method() + " => " + request.get_full_url() + " ..." | |
print "" | |
try: | |
response = urllib2.urlopen(request) | |
output = response.read() | |
print "Success => Response Body:" | |
print(json.dumps(json.loads(output, object_pairs_hook=collections.OrderedDict), indent=4)) | |
print "" | |
except Exception as e: | |
if hasattr(e, 'code'): | |
print "Error => HTTP Status Code: " + str(e.code) | |
else: | |
print "Error => Unknown Exception Occurred: " + str(e) | |
if hasattr(e, 'read'): | |
errorOutput = e.read() | |
try: | |
print "Error => Response Body:" | |
print "" | |
print(json.dumps(json.loads(errorOutput, object_pairs_hook=collections.OrderedDict), indent=4)) | |
print "" | |
except Exception as e2: | |
print "Error => No JSON Response Body" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment