예제 #1
0
    def request_resource(self, path, sub_id, req_hdrs):
        # Prepare resource request
        res_req = ResourceRequest(path, "subscribe", sub_id=sub_id)
        url = urlunparse((self._django_url.scheme, self._django_url.netloc, res_req.path, "", "", ""))
        res_req.prepare(client_headers=req_hdrs)
        http = urllib3.PoolManager()
        resp = http.urlopen("POST", url, body=res_req.to_json(), headers=res_req.get_headers())

        if resp.status == 200:
            # Check returned data has the requested content type
            Resource.validate_content_type(resp.headers.get("Content-Type", None))

            # Return Resource object
            res_json = resp.data.decode("utf-8")
            return Resource.from_json(res_json)
        else:
            raise ResourceError(resp.status)
예제 #2
0
    def request_resource(self, path, sub_id, req_hdrs):
        # Prepare resource request
        res_req = ResourceRequest(path, 'subscribe', sub_id=sub_id)
        url = urlunparse((self._django_url.scheme, self._django_url.netloc,
                          res_req.path, '', '', ''))
        res_req.prepare(client_headers=req_hdrs)
        http = urllib3.PoolManager()
        resp = http.urlopen('POST',
                            url,
                            body=res_req.to_json(),
                            headers=res_req.get_headers())

        if resp.status == 200:
            # Check returned data has the requested content type
            Resource.validate_content_type(
                resp.headers.get('Content-Type', None))

            # Return Resource object
            res_json = resp.data.decode('utf-8')
            return Resource.from_json(res_json)
        else:
            raise ResourceError(resp.status)
예제 #3
0
    def request_resource(self, path, request, sub_id):
        # Prepare resource request
        res_req = ResourceRequest(path, 'subscribe',
            sub_id=sub_id
        )
        res_req.prepare(client_headers=request.headers)

        # Build resource URL
        if self._django_url.scheme == 'http+unix':
            conn = aiohttp.UnixConnector(path=self._django_url.path)
            # aiohttp expects a hostname in the URL, even when requesting over a domain socket; correct host should be present in header
            url = urlunparse(('http', 'unknown', res_req.path, '', '', ''))
        else:
            conn = None
            url = urlunparse((self._django_url.scheme, self._django_url.netloc, res_req.path, '', '', ''))

        # Make request
        logger.debug('Requesting subscription from %s%s' % (url,
            ' over Unix socket %s' % (self._django_url.path,) if conn else '')
        )
        resp = yield from aiohttp.post(url,
            data=res_req.to_json().encode('utf-8'),
            headers=res_req.get_headers(),
            connector=conn
        )
        try:
            if resp.status == 200:
                # Check returned data has the requested content type
                Resource.validate_content_type(resp.headers.get('Content-Type', None))

                # Return Resource object
                res_json = yield from resp.text()
                return Resource.from_json(res_json)
            else:
                raise ResourceError(resp.status)
        finally:
            # Ensure response is closed
            yield from resp.release()
예제 #4
0
    def request_resource(self, path, request, sub_id):
        # Prepare resource request
        res_req = ResourceRequest(path, 'subscribe', sub_id=sub_id)
        res_req.prepare(client_headers=request.headers)

        # Build resource URL
        if self._django_url.scheme == 'http+unix':
            conn = aiohttp.UnixConnector(path=self._django_url.path)
            # aiohttp expects a hostname in the URL, even when requesting over a domain socket; correct host should be present in header
            url = urlunparse(('http', 'unknown', res_req.path, '', '', ''))
        else:
            conn = None
            url = urlunparse((self._django_url.scheme, self._django_url.netloc,
                              res_req.path, '', '', ''))

        # Make request
        logger.debug('Requesting subscription from %s%s' %
                     (url, ' over Unix socket %s' %
                      (self._django_url.path, ) if conn else ''))
        resp = yield from aiohttp.post(url,
                                       data=res_req.to_json().encode('utf-8'),
                                       headers=res_req.get_headers(),
                                       connector=conn)
        try:
            if resp.status == 200:
                # Check returned data has the requested content type
                Resource.validate_content_type(
                    resp.headers.get('Content-Type', None))

                # Return Resource object
                res_json = yield from resp.text()
                return Resource.from_json(res_json)
            else:
                raise ResourceError(resp.status)
        finally:
            # Ensure response is closed
            yield from resp.release()