http.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import urllib2
  2. from urllib import urlencode
  3. from urlparse import urlparse
  4. from anyjson import deserialize
  5. from celery import __version__ as celery_version
  6. from celery.task.base import Task as BaseTask
  7. from celery.utils.compat import parse_qsl
  8. GET_METHODS = frozenset(["GET", "HEAD"])
  9. class InvalidResponseError(Exception):
  10. """The remote server gave an invalid response."""
  11. class RemoteExecuteError(Exception):
  12. """The remote task gave a custom error."""
  13. class UnknownStatusError(InvalidResponseError):
  14. """The remote server gave an unknown status."""
  15. def maybe_utf8(value):
  16. """Encode to utf-8, only if the value is Unicode."""
  17. if isinstance(value, unicode):
  18. return value.encode("utf-8")
  19. return value
  20. def utf8dict(tup):
  21. """With a dict's items() tuple return a new dict with any utf-8
  22. keys/values encoded."""
  23. return dict((key.encode("utf-8"), maybe_utf8(value))
  24. for key, value in tup)
  25. def extract_response(raw_response):
  26. """Extract the response text from a raw JSON response."""
  27. if not raw_response:
  28. raise InvalidResponseError("Empty response")
  29. try:
  30. payload = deserialize(raw_response)
  31. except ValueError, exc:
  32. raise InvalidResponseError(str(exc))
  33. status = payload["status"]
  34. if status == "success":
  35. return payload["retval"]
  36. elif status == "failure":
  37. raise RemoteExecuteError(payload.get("reason"))
  38. else:
  39. raise UnknownStatusError(str(status))
  40. class MutableURL(object):
  41. """Object wrapping a Uniform Resource Locator.
  42. Supports editing the query parameter list.
  43. You can convert the object back to a string, the query will be
  44. properly urlencoded.
  45. Examples
  46. >>> url = URL("http://www.google.com:6580/foo/bar?x=3&y=4#foo")
  47. >>> url.query
  48. {'x': '3', 'y': '4'}
  49. >>> str(url)
  50. 'http://www.google.com:6580/foo/bar?y=4&x=3#foo'
  51. >>> url.query["x"] = 10
  52. >>> url.query.update({"George": "Costanza"})
  53. >>> str(url)
  54. 'http://www.google.com:6580/foo/bar?y=4&x=10&George=Costanza#foo'
  55. """
  56. def __init__(self, url):
  57. self.parts = urlparse(url)
  58. self.query = dict(parse_qsl(self.parts[4]))
  59. def __str__(self):
  60. scheme, netloc, path, params, query, fragment = self.parts
  61. query = urlencode(utf8dict(self.query.items()))
  62. components = ["%s://" % scheme,
  63. "%s" % netloc,
  64. path and "%s" % path or "/",
  65. params and ";%s" % params or None,
  66. query and "?%s" % query or None,
  67. fragment and "#%s" % fragment or None]
  68. return "".join(filter(None, components))
  69. def __repr__(self):
  70. return "<%s: %s>" % (self.__class__.__name__, str(self))
  71. class HttpDispatch(object):
  72. """Make task HTTP request and collect the task result.
  73. :param url: The URL to request.
  74. :param method: HTTP method used. Currently supported methods are `GET`
  75. and `POST`.
  76. :param task_kwargs: Task keyword arguments.
  77. :param logger: Logger used for user/system feedback.
  78. """
  79. user_agent = "celery/%s" % celery_version
  80. timeout = 5
  81. def __init__(self, url, method, task_kwargs, logger):
  82. self.url = url
  83. self.method = method
  84. self.task_kwargs = task_kwargs
  85. self.logger = logger
  86. def make_request(self, url, method, params):
  87. """Makes an HTTP request and returns the response."""
  88. request = urllib2.Request(url, params, headers=self.http_headers)
  89. request.headers.update(self.http_headers)
  90. response = urllib2.urlopen(request) # user catches errors.
  91. return response.read()
  92. def dispatch(self):
  93. """Dispatch callback and return result."""
  94. url = MutableURL(self.url)
  95. params = None
  96. if self.method in GET_METHODS:
  97. url.query.update(self.task_kwargs)
  98. else:
  99. params = urlencode(utf8dict(self.task_kwargs.items()))
  100. raw_response = self.make_request(str(url), self.method, params)
  101. return extract_response(raw_response)
  102. @property
  103. def http_headers(self):
  104. headers = {"Content-Type": "application/json",
  105. "User-Agent": self.user_agent}
  106. return headers
  107. class HttpDispatchTask(BaseTask):
  108. """Task dispatching to an URL.
  109. :keyword url: The URL location of the HTTP callback task.
  110. :keyword method: Method to use when dispatching the callback. Usually
  111. `GET` or `POST`.
  112. :keyword \*\*kwargs: Keyword arguments to pass on to the HTTP callback.
  113. .. attribute:: url
  114. If this is set, this is used as the default URL for requests.
  115. Default is to require the user of the task to supply the url as an
  116. argument, as this attribute is intended for subclasses.
  117. .. attribute:: method
  118. If this is set, this is the default method used for requests.
  119. Default is to require the user of the task to supply the method as an
  120. argument, as this attribute is intended for subclasses.
  121. """
  122. url = None
  123. method = None
  124. def run(self, url=None, method="GET", **kwargs):
  125. url = url or self.url
  126. method = method or self.method
  127. logger = self.get_logger(**kwargs)
  128. return HttpDispatch(url, method, kwargs, logger).dispatch()
  129. class URL(MutableURL):
  130. """HTTP Callback URL
  131. Supports requesting an URL asynchronously.
  132. :param url: URL to request.
  133. :keyword dispatcher: Class used to dispatch the request.
  134. By default this is :class:`HttpDispatchTask`.
  135. """
  136. dispatcher = HttpDispatchTask
  137. def __init__(self, url, dispatcher=None):
  138. super(URL, self).__init__(url)
  139. self.dispatcher = dispatcher or self.dispatcher
  140. def get_async(self, **kwargs):
  141. return self.dispatcher.delay(str(self), "GET", **kwargs)
  142. def post_async(self, **kwargs):
  143. return self.dispatcher.delay(str(self), "POST", **kwargs)