Created
August 12, 2013 18:53
-
-
Save zehome/6213872 to your computer and use it in GitHub Desktop.
Django reverse proxy for graphite
This file contains 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
# Requirement: httplib2 | |
import httplib2 | |
class Httplib2ProxyMixin(object): | |
def _proxy(self, method, uri, body=None): | |
assert(method in ("GET", "POST")) | |
http = httplib2.Http() | |
resp, content = http.request(uri, method, | |
headers={'cache-control': 'no-cache'}, | |
body=body) | |
httpresponse = HttpResponse(content, | |
content_type=resp["content-type"], | |
status=resp["status"]) | |
httpresponse["Content-Length"] = resp["content-length"] | |
return httpresponse | |
class GraphiteProxyView(View, Httplib2ProxyMixin): | |
def _geturl(self, path): | |
return urlparse.urljoin(settings.GRAPHITE_URL, path) | |
def get(self, request, path): | |
return self._proxy("GET", self._geturl(path)) | |
def post(self, request, path): | |
body = request.POST.urlencode() | |
return self._proxy("POST", self._geturl(path), body=body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment