Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed out calls to use Iron Core. Ignoring project_id parameter in …

…functions. Left for compatibility.
  • Loading branch information...
commit ebbef9ab6ded51c5ec7c442e979bf875990669c4 1 parent c138116
@odeits odeits authored
Showing with 49 additions and 213 deletions.
  1. +49 −213 iron_mq.py
View
262 iron_mq.py
@@ -1,179 +1,25 @@
-# IronMQ For Python
-import httplib
-
-import urllib2
-import socket
-import ssl
-
-import socket
-import json
-import ConfigParser
-
-
-class HTTPSConnectionV3(httplib.HTTPSConnection):
- def __init__(self, *args, **kwargs):
- httplib.HTTPSConnection.__init__(self, *args, **kwargs)
-
- def connect(self):
- sock = socket.create_connection((self.host, self.port), self.timeout)
- if self._tunnel_host:
- self.sock = sock
- self._tunnel()
- try:
- self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
- ssl_version=ssl.PROTOCOL_SSLv3)
- except ssl.SSLError, e:
- print "Trying SSLv3"
- self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
- ssl_version=ssl.PROTOCOL_SSLv23)
-
-
-class HTTPSHandlerV3(urllib2.HTTPSHandler):
- def https_open(self, req):
- return self.do_open(HTTPSConnectionV3, req)
-
-
-urllib2.install_opener(urllib2.build_opener(HTTPSHandlerV3()))
-
-
-class RequestWithMethod(urllib2.Request):
- def __init__(self, method, url, data=None, headers={},
- origin_req_host=None, unverifiable=False):
- self._method = method
- urllib2.Request.__init__(self, url, data, headers,
- origin_req_host, unverifiable)
-
- def get_method(self):
- if self._method:
- return self._method
- else:
- return urllib2.Request.get_method(self)
-
-
-class IllegalArgumentException(Exception):
- def __init__(self, message):
- self.message = message
-
- def __str__(self):
- return repr(self.message)
-
+import iron_core
+import urllib
+try:
+ import json
+except:
+ import simplejson as json
class IronMQ:
- DEFAULT_HOST = "mq-aws-us-east-1.iron.io"
- USER_AGENT = "IronMQ Python v0.3"
+ NAME = "iron_mq_python"
+ VERSION = "0.3"
+ client = None
+ name = None
- __headers = {
- "Accept": "application/json",
- "User-Agent": "IronMQ Python v0.3",
- }
-
- def __init__(self, token=None, project_id=None, host=DEFAULT_HOST,
- port=443, version=1, protocol='https', config=None,
- app_engine=False):
+ def __init__(self, name=None, **kwargs):
"""Prepare a configured instance of the API wrapper and return it.
- Keyword arguments:
- token -- An API token found on http://hud.iron.io. Defaults to None.
- project_id -- The ID for the project, found on http://hud.iron.io
- host -- The hostname of the API server.
- Defaults to mq-aws-us-east-1.iron.io.
- port -- The port of the API server. Defaults to 443.
- version -- The API version to use. Defaults to 1.
- protocol -- The protocol to use. Defaults to https.
- config -- The config file to draw config values from. Defaults to None.
- app_engine -- Deprecated; does nothing
- """
- self.token = token
- self.version = version
- self.project_id = project_id
- self.protocol = protocol
- self.host = host
- self.port = port
- self.version = version
- if config is not None:
- config_file = ConfigParser.RawConfigParser()
- config_file.read(config)
- try:
- self.token = config_file.get("IronMQ", "token")
- except ConfigParser.NoOptionError:
- self.token = token
- try:
- self.project_id = config_file.get("IronMQ", "project_id")
- except ConfigParser.NoOptionError:
- self.project_id = project_id
- try:
- self.host = config_file.get("IronMQ", "host")
- except ConfigParser.NoOptionError:
- self.host = host
- try:
- self.port = config_file.get("IronMQ", "port")
- except ConfigParser.NoOptionError:
- self.port = port
- try:
- self.version = config_file.get("IronMQ", "version")
- except ConfigParser.NoOptionError:
- self.version = version
- try:
- self.protocol = config_file.get("IronMQ", "protocol")
- except ConfigParser.NoOptionError:
- self.protocol = protocol
- if self.token is None or self.project_id is None:
- raise IllegalArgumentException("Both token and project_id must \
- have a value")
- self.url = "%s://%s:%s/%s/" % (self.protocol, self.host, self.port,
- self.version)
-
- def __req(self, url, method, body=None, headers=None):
- if headers is None:
- headers = IronMQ.__headers
- else:
- headers = dict(headers.items() + IronMQ.__headers.items())
- if self.protocol == "http":
- conn = httplib.HTTPConnection(self.host, self.port)
- conn.request(method, url, body, headers)
- resp = conn.getresponse()
- elif self.protocol == "https":
- #conn = httplib.HTTPSConnection(self.host, self.port)
- req = RequestWithMethod(method, url, body, headers)
- resp = urllib2.urlopen(req)
- body = resp.read()
- if self.protocol == "http":
- conn.close()
- return body
-
- def __get(self, url, headers=None):
- """Execute an HTTP GET request and return the result.
-
- Keyword arguments:
- url -- The url to execute the request against. (Required)
- headers -- A dict of headers to merge with IronMQ.__headers and send
- with the request.
- """
- return self.__req(url, "GET", headers=headers)
-
- def __post(self, url, payload=None, headers=None):
- """Execute an HTTP POST request and return the result.
-
- Keyword arguments:
- url -- The url to execute the request against. (Required)
- payload -- A dict of key-value form data to send with the
- request. Will be urlencoded.
- headers -- A dict of headers to merge with IronMQ.__headers and send
- with the request.
- """
- if headers is None:
- headers = {"Content-Type": "application/json"}
- return self.__req(url, "POST", body=payload, headers=headers)
-
- def __delete(self, url, headers=None):
- """Execute an HTTP DELETE request and return the result.
-
- Keyword arguments:
- url -- The url to execute the request against. (Required)
- headers -- A dict of headers to merge with IronMQ.__headers and send
- with the request.
- """
- return self.__req(url, "DELETE", headers=headers)
+ Keyword arguments are passed directly to iron_core_python; consult its
+ documentation for a full list and possible values."""
+ if name is not None:
+ self.name = name
+ self.client = iron_core.IronClient(name=IronMQ.NAME,
+ version=IronMQ.VERSION, product="iron_mq", **kwargs)
def getQueues(self, page=None, project_id=None):
"""Execute an HTTP request to get a list of queues and return it.
@@ -184,18 +30,18 @@ def getQueues(self, page=None, project_id=None):
page -- The 0-based page to get queues from. Defaults to None, which
omits the parameter.
"""
- if project_id is None:
- project_id = self.project_id
- pageForURL = ""
+ options = {}
if page is not None:
- pageForURL = "&page=%s" % page
- url = "%sprojects/%s/queues?oauth=%s%s" % (self.url, project_id,
- self.token, pageForURL)
- body = self.__get(url)
- queues = json.loads(body)
- return queues
-
- def getQueueDetails(self, queue_name, project_id=""):
+ options['page'] = page
+
+ query = urllib.urlencode(options)
+ url = "queues"
+ if query != "":
+ url = "%s?%s" % (url, query)
+ result = self.client.get(url)
+ return [queue["name"] for queue in result["body"]]
+
+ def getQueueDetails(self, queue_name, project_id=None):
"""Execute an HTTP request to get details on a specific queue, and
return it.
@@ -204,12 +50,10 @@ def getQueueDetails(self, queue_name, project_id=""):
project_id -- The ID of the project the queue belongs to. Defaults to
the project ID set when initialising the wrapper.
"""
- if project_id == "":
- project_id = self.project_id
- url = "%sprojects/%s/queues/%s?oauth=%s" % (self.url, project_id,
- queue_name, self.token)
- body = self.__get(url)
- queue = json.loads(body)
+
+ url = "queues/%s" % (queue_name,)
+ result = self.client.get(url)
+ queue = result["body"]
return queue
def deleteMessage(self, queue_name, message_id, project_id=None):
@@ -222,12 +66,10 @@ def deleteMessage(self, queue_name, message_id, project_id=None):
contains the message. Defaults to the project ID set
when initialising the wrapper.
"""
- if project_id is None:
- project_id = self.project_id
- url = "%sprojects/%s/queues/%s/messages/%s?oauth=%s" % (self.url,
- project_id, queue_name, message_id, self.token)
- body = self.__delete(url)
- return json.loads(body)
+
+ url = "queues/%s/messages/%s" % (queue_name, message_id)
+ result = self.client.delete(url)
+ return result["body"]
def postMessage(self, queue_name, messages=[], project_id=None):
"""Executes an HTTP request to create message on the queue.
@@ -239,18 +81,15 @@ def postMessage(self, queue_name, messages=[], project_id=None):
project_id -- The ID of the project the queue is under. Defaults to
the project ID set when the wrapper was initialised.
"""
- if project_id is None:
- project_id = self.project_id
- url = "%sprojects/%s/queues/%s/messages?oauth=%s" % (self.url,
- project_id, queue_name, self.token)
+
+ url = "queues/%s/messages" % ( queue_name,)
msgs = [{'body':msg} if isinstance(msg, basestring) else msg
for msg in messages]
data = json.dumps({"messages": msgs})
- s = self.__post(url=url, payload=data)
+ result = self.client.post(url=url, body=data, headers= {"Content-Type": "application/json"})
- ret = json.loads(s)
- return ret
+ return result['body']
def getMessage(self, queue_name, max=None, project_id=None):
"""Executes an HTTP request to get a message off of a queue.
@@ -263,15 +102,13 @@ def getMessage(self, queue_name, max=None, project_id=None):
is to be pulled from. Defaults to the project ID set when
the wrapper was initialised.
"""
- if project_id is None:
- project_id = self.project_id
+
n = ""
if max is not None:
n = "&n=%s" % max
- url = "%sprojects/%s/queues/%s/messages?oauth=%s%s" % (self.url,
- project_id, queue_name, self.token, n)
- body = self.__get(url)
- return json.loads(body)
+ url = "queues/%s/messages?%s" % ( queue_name, n)
+ result = self.client.get(url)
+ return result['body']
def clearQueue(self, queue_name, project_id=None):
"""Executes an HTTP request to clear all contents of a queue.
@@ -283,10 +120,9 @@ def clearQueue(self, queue_name, project_id=None):
being cleared. Defaults to the project ID set when the
wrapper was initialised.
"""
- if project_id is None:
- project_id = self.project_id
- url = "%sprojects/%s/queues/%s/clear?oauth=%s" % (self.url, project_id,
- queue_name, self.token)
- body = self.__post(url)
- return json.loads(body)
+
+ url = "queues/%s/clear" % (queue_name,)
+ result = self.client.post(url)
+ return result['body']
+
Please sign in to comment.
Something went wrong with that request. Please try again.