Exemplo n.º 1
0
    def _request(self, route_uri, method, body="", headers=None,
                 multipart=False):

        try:
            if multipart:
                return self._multipart_req(route_uri, method, body, headers)
            retcode, content, status_code, resp = \
                req_by_msb(route_uri, method, body, headers)
            if retcode != 0:
                # Execptions are handled within req_by_msb
                logger.error("Status code is %s, detail is %s.",
                             status_code, content)

        except exceptions.NotFound as e:
            return HttpResponse(str(e), status=status.HTTP_404_NOT_FOUND)

        except Exception as e:
            content = e
            status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
            logger.exception("exception: %s" % e)

        response = HttpResponse(content, status=status_code)
        if retcode == 0:
            for k in getHeadersKeys(resp):
                response[k] = resp[k]
        return response
    def forwarder(self, *remainder, **kwargs):
        """ Forward any requests that don't have a specific match """

        # TODO(xiaohhui): Add validator for vim_id.
        vim_id = remainder[0]
        request = pecan.request
        try:
            vim_url = syscomm.getMultivimDriver(vim_id, full_path=request.path)

            # NOTE: Not sure headers should be set here. According to original
            # code, headers are discarded.
            retcode, content, status_code, resp = restcall.req_by_msb(
                vim_url, request.method, content=request.body)
        except exceptions.NotFound as e:
            pecan.abort(404, detail=str(e))
        except Exception as e:
            pecan.abort(500, detail=str(e))

        if retcode:
            # Execptions are handled within req_by_msb
            logger.error("Status code is %s, detail is %s.", status_code,
                         content)
        response = pecan.Response(body=content, status=status_code)
        if retcode == 0:
            for k in syscomm.getHeadersKeys(resp):
                response.headers[k] = resp[k]

        return response
 def test_getHeadersKeys_with_connection(self):
     resp = {
         "connection": "aa,bb",
         "keep-alive": "yes",
         "aa": "vaa",
         "bb": "vbb",
         "cc": "vcc"
     }
     self.assertEqual(["cc"], syscomm.getHeadersKeys(resp))
 def test_getHeadersKeys_no_connection(self):
     resp = {}
     self.assertEqual([], syscomm.getHeadersKeys(resp))