示例#1
0
def build_response(request,
                   status_code,
                   msg,
                   extra_formats=None,
                   headers=None):
    if extra_formats is not None:
        formatters = extra_formats.copy()
        formatters.update(FORMATTERS)
    else:
        formatters = FORMATTERS

    if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
        mimetype = 'application/json; charset=utf-8'
    else:
        mimetype = mimeparser.best_match(
            formatters.keys(), request.META.get('HTTP_ACCEPT', 'text/plain'))

    response = HttpResponse(formatters[mimetype](request, mimetype,
                                                 status_code, msg),
                            mimetype=mimetype,
                            status=status_code)
    if headers is None:
        headers = {}

    for header_name in headers:
        response[header_name] = headers[header_name]

    return response
示例#2
0
    def download(self, name=None, content_type='application/rdf+xml'):

        url = self._repository_url
        opener = urllib2.build_opener()

        if name != None:
            name = name.replace(' ', '')
            url = urljoin(url, self._collection)
            url = urljoin(url, name)

        headers = {'Accept': '*'}
        request = MethodRequest('GET', url, '', headers)

        response = opener.open(request)

        if not (response.code > 199 and response.code < 300):
            raise HTTPError(response.url, response.code, response.msg, None, None)

        allowed_formats = ['text/plain', 'application/rdf+xml', 'text/turtle', 'text/n3']
        resp_content_type = mimeparser.best_match(allowed_formats, response.headers.get('content-type'))

        return {
            'content_type': resp_content_type,
            'data': response.read()
        }
    def download(self, name=None):

        response = self._make_request(requests.get, '', name, headers={'Accept': '*/*'})

        allowed_formats = ['text/plain', 'application/rdf+xml', 'text/turtle', 'text/n3']
        resp_content_type = mimeparser.best_match(allowed_formats, response.headers.get('content-type'))

        return {
            'content_type': resp_content_type,
            'data': response.text
        }
def build_response(request, status_code, msg, extra_formats=None, headers=None):
    if extra_formats is not None:
        formatters = extra_formats.copy()
        formatters.update(FORMATTERS)
    else:
        formatters = FORMATTERS

    if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
        mimetype = 'application/json; charset=utf-8'
    else:
        mimetype = mimeparser.best_match(formatters.keys(), request.META.get('HTTP_ACCEPT', 'text/plain'))

    response = HttpResponse(formatters[mimetype](request, mimetype, status_code, msg), content_type=mimetype, status=status_code)
    if headers is None:
        headers = {}

    for header_name in headers:
        response[header_name] = headers[header_name]

    return response