#!/usr/bin/env python # encoding: utf-8 """ test.py """ # These are required for the actual CLI component import sys import getopt import httplib import urllib import md5 help_message = ''' test.py -h, --help This message -o, --output [output] Where to output data -s, --server=[server_name] Webserver to connect to. Default to https://api.royaltyshare.com -u, --user=[username] The user to connect as -p, --password=[password] The password to connect as -a, --apikey=[sessionId] Use session key instead username/password -c, --command [cmd] Command to execute. -- Any arguments past this point will go through to the command ''' class Usage(Exception): def __init__(self, msg): self.msg = msg # Given a host and login credentials, return the session used for future calls def connect(client, user, password): param = urllib.urlencode({'username': user, 'password': password }) header = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} client.request("POST", "/api/authenticate", param, header) response = client.getresponse() print print ">>>> connect" sessionId = 0 print "HTTP Status/Reason: ", str(response.status) + " / " + response.reason print "Response header: " + str(response.getheaders()) print "Response:" print response.read() cookie = response.getheader('set-cookie') sessionId = 0 if cookie is not None: part1 = cookie.split(';')[0] sessionId = part1.split('=')[1] print "connect: received cookie( " + cookie + " )" print "<<<< connect" print return sessionId def main(argv=None): servername = "portal.royaltyshare.com" # default command = 'none' user = 'none' password = 'none' apikey = 'none' if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "hoscupa:v", ["help", "output=", "server=", "command=", "user=", "password=", "apikey="]) except getopt.error, msg: raise Usage(msg) # option processing for option, value in opts: if option == "-v": verbose = True if option in ("-h", "--help"): raise Usage(help_message) if option in ("-o", "--output"): output = value if option in ("-s", "--server"): servername = value if option in ("-c", "--command"): command = value if option in ("-u", "--user"): user = value if option in ("-p", "--password"): password = value if option in ("-a", "--apikey"): apikey = value except Usage, err: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) print >> sys.stderr, "\t for help use --help" return 2 if user == 'none' and password == 'none': print "Info: Username and password not specified" # Setup the connection to the server, and authenticate try: client = httplib.HTTPSConnection(servername) if apikey == 'none': sessionId = connect(client, user, password) else: sessionId = apikey except httplib.socket.error, err: print >> sys.stderr, "Error opening connection: " + str(err) return 3 # A session is required for everything except user creation if (sessionId == None) and (command != "confirm"): client.close() return -1 # All plugin modules are required to implement the run attribute # which will accept the client handle and any remaining arguments print print ">>>> " + command pkg = __import__('apiv1.' + command) mod = sys.modules['apiv1.' + command] mod.run(client, sessionId, "https://" + servername, args) client.close() print "<<<< " + command print if __name__ == "__main__": sys.exit(main())