コード例 #1
0
    def test_wants_metadata(self):

        request = Mock()
        for metadata_state in (True, False):
            request.headers = {
                'Accept': 'application/json;metadata={}'.format(str(metadata_state))
            }
            assert_equal(wants_metadata(request), metadata_state)

            request.headers = {
                'Accept': 'application/json;metadata={}'.format(str(metadata_state).lower())
            }
            assert_equal(wants_metadata(request), metadata_state)

        request.headers = {'Accept:' 'application/json;metadata=wibble'}
        assert_equal(wants_metadata(request), False)
コード例 #2
0
    def test_wants_metadata(self):
        """Test that the wants_metadata fields on the rqeuest object works correctly."""
        request = Mock()
        for metadata_state in (True, False):
            request.headers = {
                'Accept':
                'application/json;metadata={}'.format(str(metadata_state))
            }
            assert wants_metadata(request) == metadata_state

            request.headers = {
                'Accept':
                'application/json;metadata={}'.format(
                    str(metadata_state).lower())
            }
            assert wants_metadata(request) == metadata_state

        request.headers = {'Accept:' 'application/json;metadata=wibble'}
        assert not wants_metadata(request)
コード例 #3
0
ファイル: test_proxy.py プロジェクト: stfc-aeg/odin-control
 def get(self, path=''):
     """Handle GET requests to the test server."""
     try:
         data_ref = self.param_tree.get(path, wants_metadata(self.request))
         self.write(data_ref)
     except ParameterTreeError:
         self.set_status(404)
         self.write_error(404)
     except Exception as other_e:
         logging.error("ProxyTestHandler GET failed: %s", str(other_e))
         self.write_error(500)
コード例 #4
0
ファイル: async_proxy.py プロジェクト: stfc-aeg/odin-control
    async def get(self, path, request):
        """
        Handle an HTTP GET request.

        This async method handles an HTTP GET request, returning a JSON response. The request is
        passed to the adapter proxy and resolved into responses from the requested proxy targets.

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        get_metadata = wants_metadata(request)

        await asyncio.gather(*self.proxy_get(path, get_metadata))
        (response, status_code) = self._resolve_response(path, get_metadata)

        return ApiAdapterResponse(response, status_code=status_code)
コード例 #5
0
ファイル: system_info.py プロジェクト: percival-detector/odin
    def get(self, path, request):
        """Handle an HTTP GET request.

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

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        try:
            response = self.system_info.get(path, wants_metadata(request))
            status_code = 200
        except ParameterTreeError as param_error:
            response = {'error': str(param_error)}
            status_code = 400

        logging.debug(response)
        content_type = 'application/json'

        return ApiAdapterResponse(response, content_type=content_type,
                                  status_code=status_code)
コード例 #6
0
    def get(self, path, request):
        """Handle an HTTP GET request.

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

        :param path: URI path of request
        :param request: HTTP request object
        :return: an ApiAdapterResponse object containing the appropriate response
        """
        try:
            response = self.backplane.get(path, wants_metadata(request))
            status_code = 200
        except ParameterTreeError as e:
            response = {'error': str(e)}
            status_code = 400

        content_type = 'application/json'

        return ApiAdapterResponse(response,
                                  content_type=content_type,
                                  status_code=status_code)
コード例 #7
0
    def get(self, path, request):
        """
        Handle an HTTP GET request.

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

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

        get_metadata = wants_metadata(request)
        # Update the target specified in the path, or all targets if none specified
        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_get(target_path, get_metadata)

        # Build the response from the adapter parameter tree
        try:
            if get_metadata:
                if path_elem == "" or path_elem == "status":
                    # update status tree with metadata
                    self.meta_param_tree.set('status',
                                             self.status_tree.get("", True))
                response = self.meta_param_tree.get(path)

            else:
                response = self.param_tree.get(path)
            status_code = 200
        except ParameterTreeError as param_tree_err:
            response = {'error': str(param_tree_err)}
            status_code = 400

        return ApiAdapterResponse(response, status_code=status_code)