예제 #1
0
    def prepare_data(self, spec: Spec, **kwargs) -> Tuple[str, str]:
        """ Parse request URL, validates operation, and returns method and URL for outgoing request"""

        # Parse URL kwargs
        pk = kwargs.get('pk')
        model = kwargs.get('model', '').lower()
        path_kwargs = {}
        if kwargs.get('pk') is None:
            path = f'/{model}/'
        else:
            pk_name = 'uuid' if utils.valid_uuid4(pk) else 'id'
            path_kwargs = {pk_name: pk}
            path = f'/{model}/{{{pk_name}}}/'

        # Check that operation is valid according to spec
        operation = spec.get_op_for_request(self._in_request.method, path)
        if not operation:
            raise exceptions.EndpointNotFound(f'Endpoint not found: {self._in_request.method} {path}')
        method = operation.http_method.lower()
        path_name = operation.path_name

        # Build URL for the operation to request data from the service
        url = spec.api_url.rstrip('/') + path_name
        for k, v in path_kwargs.items():
            url = url.replace(f'{{{k}}}', v)

        return method, url
예제 #2
0
    def prepare_data(self, spec: Spec, **kwargs) -> Tuple[str, str]:
        """ Parse request URL, validates operation, and returns method and URL for outgoing request"""

        # Parse URL kwargs
        pk = kwargs.get('pk')
        model = kwargs.get('model', '').lower()
        path_kwargs = {}
        if kwargs.get('pk') is None:
            path = f'/{model}/'
        else:
            pk_name = 'uuid' if utils.valid_uuid4(pk) else 'id'

            # update path kwargs key name
            # example: {"product_uuid" :"db827034-30bb-4062-ac35-f8c24e8f81ad"}
            path_kwargs = {f'{model}_{pk_name}': pk}

            # create path for retrieving individual data example : /product/{{product_uuid}}/
            path_parameter_name = f'{model}_{pk_name}'
            path = f'/{model}/{{{path_parameter_name}}}/'

        # Check that operation is valid according to spec
        operation = spec.get_op_for_request(self._in_request.method, path)
        if not operation:
            raise exceptions.EndpointNotFound(
                f'Endpoint not found: {self._in_request.method} {path}')
        method = operation.http_method.lower()
        path_name = operation.path_name

        # Build URL for the operation to request data from the service
        url = spec.api_url.rstrip('/') + path_name
        for k, v in path_kwargs.items():
            url = url.replace(f'{{{k}}}', v)

        return method, url