def close(self): Response.close(self) if self._socket is not None: self._socket.close() self._socket = None if self._httplib_resp is not None: self._httplib_resp.close() self._httplib_resp = None
def service_rsd(req): """Serves and RSD definition (really simple discovery) so that blog frontends can query the apis that are available. :URL endpoint: ``blog/service_rsd`` """ return Response(generate_rsd(req.app), mimetype='application/xml')
def oncall(request, *args, **kwargs): use_cache = get_cache_context(vary, True, request)[1] response = None if use_cache: cache_key = key + request.path.encode('utf-8') response = request.app.cache.get(cache_key) if response is None: response = f(request, *args, **kwargs) # make sure it's one of our request objects so that we # have the `make_conditional` method on it. Response.force_type(response) if use_cache and response.status_code == 200: response.freeze() request.app.cache.set(key, response, timeout) response.make_conditional(request) return response
def rss_feed(req, author=None, year=None, month=None, day=None, category=None, tag=None, post=None): feed = RssFeed( req.app.cfg['blog_title'], req.app.cfg['blog_url'], "", # Description not supported subtitle=req.app.cfg['blog_tagline'], feed_url=req.url) results = populate_feed(req, feed, author, year, month, day, category, tag, post) return Response(results, mimetype="application/rss+xml")
def json_service(req, identifier): """Handle a JSON service req.""" handler = req.app._services.get(identifier) if handler is None: raise NotFound() #! if this event returns a handler it is called instead of the default #! handler. Useful to intercept certain requests. for callback in iter_listeners('before-json-service-called'): rv = callback(identifier, handler) if rv is not None: handler = rv result = handler(req) #! called right after json callback returned some data with the identifier #! of the req method and the result object. Note that events *have* #! to return an object, even if it's just changed in place, otherwise the #! return value will be `null` (None). for callback in iter_listeners('after-json-service-called'): result = callback(identifier, result) return Response(dump_json(result), mimetype='text/javascript')
def get_resource(app, filename): """Get a resource as response object.""" parts = filename.split('/') # no folder or file with a leading dot or the special is allowed for part in parts: if part[:1] == '.': return base = _find_path(app, parts) for filename in os.path.join(base, app.cfg['language'], *parts), \ os.path.join(base, 'en', *parts): if os.path.isfile(filename): f = file(filename, 'rb') try: return Response(f.read(), mimetype=guess_type(filename)[0] or 'application/octet-stram') finally: f.close()
def __init__(self, url, body, status=200, headers=None): Response.__init__(self, body, status, headers) self.url = url
def open(self, data=None): """Return a `URLResponse` object.""" return Response()