| 45 | |
| 46 | |
| 47 | def make_request(command, args, logger, host, port, |
| 48 | apikey, secretkey, protocol, path): |
| 49 | response = None |
| 50 | error = None |
| 51 | |
| 52 | if protocol != 'http' and protocol != 'https': |
| 53 | error = "Protocol must be 'http' or 'https'" |
| 54 | return None, error |
| 55 | |
| 56 | if args is None: |
| 57 | args = {} |
| 58 | |
| 59 | args["command"] = command |
| 60 | args["apiKey"] = apikey |
| 61 | args["response"] = "json" |
| 62 | request = zip(args.keys(), args.values()) |
| 63 | request.sort(key=lambda x: x[0].lower()) |
| 64 | |
| 65 | request_url = "&".join(["=".join([r[0], urllib.quote_plus(str(r[1]))]) |
| 66 | for r in request]) |
| 67 | hashStr = "&".join(["=".join([r[0].lower(), |
| 68 | str.lower(urllib.quote_plus(str(r[1]))).replace("+", |
| 69 | "%20")]) for r in request]) |
| 70 | |
| 71 | sig = urllib.quote_plus(base64.encodebytes(hmac.new(secretkey, hashStr, |
| 72 | hashlib.sha1).digest()).strip()) |
| 73 | request_url += "&signature=%s" % sig |
| 74 | request_url = "%s://%s:%s%s?%s" % (protocol, host, port, path, request_url) |
| 75 | |
| 76 | try: |
| 77 | logger_debug(logger, "Request sent: %s" % request_url) |
| 78 | connection = urllib2.urlopen(request_url) |
| 79 | response = connection.read() |
| 80 | except Exception, e: |
| 81 | error = str(e) |
| 82 | |
| 83 | logger_debug(logger, "Response received: %s" % response) |
| 84 | if error is not None: |
| 85 | logger_debug(logger, error) |
| 86 | |
| 87 | return response, error |
| 88 | |
| 89 | |
| 90 | def monkeyrequest(command, args, isasync, asyncblock, logger, host, port, |