예제 #1
0
 def test_decode_request_body_type_error(self):
     """Test that a body type mismatch returns the body unchanged."""
     request = Mock
     request.headers = {'Content-Type': 'application/json'}
     request.body = {"pi": 2.56}
     response = util.decode_request_body(request)
     assert response == request.body
예제 #2
0
 def test_decode_request_body(self):
     """Test that the body a a request is correctly decoded."""
     request = Mock
     request.headers = {'Content-Type': 'application/json'}
     request.body = '{"pi":2.56}'
     response = util.decode_request_body(request)
     assert response == {"pi": 2.56}
예제 #3
0
 def test_decode_request_body_not_json(self):
     """Test that the body of a native request is correctly decoded."""
     request = Mock
     request.headers = {'Content-Type': 'application/vnd.odin-native'}
     request.body = {"pi": 2.56}
     response = util.decode_request_body(request)
     assert response == request.body
예제 #4
0
    def put(self, path, request):
        """Handle an HTTP PUT request.

        This method handles an HTTP PUT request, returning a JSON response.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """

        content_type = 'application/json'

        try:
            data = convert_unicode_to_string(decode_request_body(request))
            self.qem_detector.set(path, data)
            response = self.qem_detector.get(path)
            status_code = 200
        except QemDetectorError as e:
            response = {'error': str(e)}
            status_code = 400
        except (TypeError, ValueError) as e:
            response = {
                'error': 'Failed to decode PUT request body: {}'.format(str(e))
            }
            status_code = 400

        logging.debug(response)

        return ApiAdapterResponse(response,
                                  content_type=content_type,
                                  status_code=status_code)
예제 #5
0
    def put(self, path, request):
        """
        Handle an HTTP PUT request.

        This method handles an HTTP PUT request, returning a JSON response. The request is
        passed to the adapter proxy to set data on the remote targets and resolved into responses
        from those targets.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        # Decode the request body from JSON, handling and returning any errors that occur. Otherwise
        # send the PUT request to the remote target
        try:
            body = decode_request_body(request)
        except (TypeError, ValueError) as type_val_err:
            response = {
                'error':
                'Failed to decode PUT request body: {}'.format(
                    str(type_val_err))
            }
            status_code = 415
        else:
            self.proxy_set(path, body)
            (response, status_code) = self._resolve_response(path)

        return ApiAdapterResponse(response, status_code=status_code)
예제 #6
0
    def put(self, path, request):
        """
        Handle an HTTP PUT request.

        This method handles an HTTP PUT request, returning a JSON response.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object with the appropriate response
        """
        content_type = 'application/json'
        status_code = 200
        response = {}
        checkAdapters = True if len(path) > 0 else False
        requestSent = False
        try:
            if checkAdapters:
                for name, adapter in self.adapters.items():
                    if path.startswith(name):

                        relative_path = path.split(name + '/')
                        reply = adapter.put(path=relative_path[1],
                                            request=request)
                        requestSent = True
                        if reply.status_code != 200:
                            status_code = reply.status_code
                            response = reply.data
                            logging.debug(response)
                            return ApiAdapterResponse(
                                response,
                                content_type=content_type,
                                status_code=status_code)

            # Only pass request to Hexitec member if no matching adapter found
            if requestSent is False:
                data = convert_unicode_to_string(decode_request_body(request))
                self.hexitec.set(path, data)
                response = self.hexitec.get(path)
        except HexitecError as e:
            response = {'error': str(e)}
            status_code = 400
        except (TypeError, ValueError) as e:
            response = {
                'error': 'Failed to decode PUT request body: {}'.format(str(e))
            }
            status_code = 400

        logging.debug(response)

        return ApiAdapterResponse(response,
                                  content_type=content_type,
                                  status_code=status_code)
예제 #7
0
    def put(self, path, request):
        """Handle a HTTP PUT request.

        Calls the put method of each other adapter that has been loaded, and returns the responses
        in a dictionary.
        """
        logging.debug("IAC DUMMY PUT")
        body = decode_request_body(request)
        response = {}
        request = ApiAdapterRequest(body)

        for key, value in self.adapters.items():
            logging.debug("Calling Put of %s", key)
            response[key] = value.put(path="", request=request).data
        content_type = "application/json"
        status_code = 200

        logging.debug(response)

        return ApiAdapterResponse(response, content_type=content_type,
                                  status_code=status_code)
예제 #8
0
    def put(self, path, request):
        """
        Handle an HTTP PUT request.

        This method handles an HTTP PUT request, returning a JSON response.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        # Update the target specified in the path, or all targets if none specified

        try:
            body = decode_request_body(
                request
            )  # ensure request body is JSON. Will throw a TypeError if not
            if "/" in path:
                path_elem, target_path = path.split('/', 1)
            else:
                path_elem = path
                target_path = ""
            for target in self.targets:
                if path_elem == '' or path_elem == target.name:
                    target.remote_set(target_path, body)

            response = self.param_tree.get(path)
            status_code = 200
        except ParameterTreeError as param_tree_err:
            response = {'error': str(param_tree_err)}
            status_code = 400
        except (TypeError, ValueError) as type_val_err:
            response = {
                'error':
                'Failed to decode PUT request body: {}'.format(
                    str(type_val_err))
            }
            status_code = 415

        return ApiAdapterResponse(response, status_code=status_code)
예제 #9
0
파일: dummy.py 프로젝트: dw96/odin-control
    def put(self, path, request):
        """Handle a HTTP PUT request.

        Calls the put method of each other adapter that has been loaded, and returns the responses
        in a dictionary.
        """
        logging.debug("IAC DUMMY PUT")
        body = decode_request_body(request)
        response = {}
        request = ApiAdapterRequest(body)

        for key, value in self.adapters.items():
            logging.debug("Calling Put of %s", key)
            response[key] = value.put(path="", request=request).data
        content_type = "application/json"
        status_code = 200

        logging.debug(response)

        return ApiAdapterResponse(response,
                                  content_type=content_type,
                                  status_code=status_code)
예제 #10
0
    async def put(self, path, request):
        """Handle an HTTP PUT request.

        This method handles an HTTP PUT request, decoding the request and attempting to set values
        in the asynchronous parameter tree as appropriate.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        content_type = 'application/json'

        try:
            data = decode_request_body(request)
            await self.param_tree.set(path, data)
            response = await self.param_tree.get(path)
            status_code = 200
        except ParameterTreeError as param_error:
            response = {'error': str(param_error)}
            status_code = 400

        return ApiAdapterResponse(response,
                                  content_type=content_type,
                                  status_code=status_code)
예제 #11
0
 def test_decode_request_body_type_error(self):
     request = Mock
     request.headers = {'Content-Type': 'application/json'}
     request.body = {"pi": 2.56}
     response = util.decode_request_body(request)
     assert_equal(response, request.body)
예제 #12
0
 def test_decode_request_body_not_json(self):
     request = Mock
     request.headers = {'Content-Type': 'application/vnd.odin-native'}
     request.body = {"pi": 2.56}
     response = util.decode_request_body(request)
     assert_equal(response, request.body)