Leases tasks from a pull queue for ``lease_duration``.

        This method is invoked by the worker to obtain a lease. The worker must
        acknowledge the task via ``AcknowledgeTask`` after they have performed
        the work associated with the task.

        The ``payload`` is intended to store data that the worker needs to
        perform the work associated with the task. To return the payloads in the
        ``response``, set ``response_view`` to ``FULL``.

        A maximum of 10 qps of ``LeaseTasks`` requests are allowed per queue.
        ``RESOURCE_EXHAUSTED`` is returned when this limit is exceeded.
        ``RESOURCE_EXHAUSTED`` is also returned when
        ``max_tasks_dispatched_per_second`` is exceeded.

        Example:
            >>> from google.cloud import tasks_v2beta2
            >>>
            >>> client = tasks_v2beta2.CloudTasksClient()
            >>>
            >>> parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]')
            >>>
            >>> # TODO: Initialize `lease_duration`:
            >>> lease_duration = {}
            >>>
            >>> response = client.lease_tasks(parent, lease_duration)

        Args:
            parent (str): Required.

                The queue name. For example:
                ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID``
            lease_duration (Union[dict, ~google.cloud.tasks_v2beta2.types.Duration]): After the worker has successfully finished the work associated with the
                task, the worker must call via ``AcknowledgeTask`` before the
                ``schedule_time``. Otherwise the task will be returned to a later
                ``LeaseTasks`` call so that another worker can retry it.

                The maximum lease duration is 1 week. ``lease_duration`` will be
                truncated to the nearest second.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.tasks_v2beta2.types.Duration`
            max_tasks (int): The maximum number of tasks to lease.

                The system will make a best effort to return as close to as
                ``max_tasks`` as possible.

                The largest that ``max_tasks`` can be is 1000.
            response_view (~google.cloud.tasks_v2beta2.types.View): The response\_view specifies which subset of the ``Task`` will be
                returned.

                By default response\_view is ``BASIC``; not all information is retrieved
                by default because some data, such as payloads, might be desirable to
                return only when needed because of its large size or because of the
                sensitivity of data that it contains.

                Authorization for ``FULL`` requires ``cloudtasks.tasks.fullView``
                `Google IAM <https://cloud.google.com/iam/>`___ permission on the
                ``Task`` resource.
            filter_ (str): ``filter`` can be used to specify a subset of tasks to lease.

                When ``filter`` is set to ``tag=<my-tag>`` then the ``response`` will
                contain only tasks whose ``tag`` is equal to ``<my-tag>``. ``<my-tag>``
                must be less than 500 characters.

                When ``filter`` is set to ``tag_function=oldest_tag()``, only tasks
                which have the same tag as the task with the oldest ``schedule_time``
                will be returned.

                Grammar Syntax:

                -  ``filter = "tag=" tag | "tag_function=" function``

                -  ``tag = string``

                -  ``function = "oldest_tag()"``

                The ``oldest_tag()`` function returns tasks which have the same tag as
                the oldest task (ordered by schedule time).

                SDK compatibility: Although the SDK allows tags to be either string or
                `bytes <https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/taskqueue/TaskOptions.html#tag-byte:A->`__,
                only UTF-8 encoded tags can be used in Cloud Tasks. Tag which aren't
                UTF-8 encoded can't be used in the ``filter`` and the task's ``tag``
                will be displayed as empty in Cloud Tasks.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.tasks_v2beta2.types.LeaseTasksResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.

def lease_tasks(
        self,
        parent,
        lease_duration,
        max_tasks=None,
        response_view=None,
        filter_=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Leases tasks from a pull queue for ``lease_duration``.

        This method is invoked by the worker to obtain a lease. The worker must
        acknowledge the task via ``AcknowledgeTask`` after they have performed
        the work associated with the task.

        The ``payload`` is intended to store data that the worker needs to
        perform the work associated with the task. To return the payloads in the
        ``response``, set ``response_view`` to ``FULL``.

        A maximum of 10 qps of ``LeaseTasks`` requests are allowed per queue.
        ``RESOURCE_EXHAUSTED`` is returned when this limit is exceeded.
        ``RESOURCE_EXHAUSTED`` is also returned when
        ``max_tasks_dispatched_per_second`` is exceeded.

        Example:
            >>> from google.cloud import tasks_v2beta2
            >>>
            >>> client = tasks_v2beta2.CloudTasksClient()
            >>>
            >>> parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]')
            >>>
            >>> # TODO: Initialize `lease_duration`:
            >>> lease_duration = {}
            >>>
            >>> response = client.lease_tasks(parent, lease_duration)

        Args:
            parent (str): Required.

                The queue name. For example:
                ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID``
            lease_duration (Union[dict, ~google.cloud.tasks_v2beta2.types.Duration]): After the worker has successfully finished the work associated with the
                task, the worker must call via ``AcknowledgeTask`` before the
                ``schedule_time``. Otherwise the task will be returned to a later
                ``LeaseTasks`` call so that another worker can retry it.

                The maximum lease duration is 1 week. ``lease_duration`` will be
                truncated to the nearest second.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.tasks_v2beta2.types.Duration`
            max_tasks (int): The maximum number of tasks to lease.

                The system will make a best effort to return as close to as
                ``max_tasks`` as possible.

                The largest that ``max_tasks`` can be is 1000.
            response_view (~google.cloud.tasks_v2beta2.types.View): The response\_view specifies which subset of the ``Task`` will be
                returned.

                By default response\_view is ``BASIC``; not all information is retrieved
                by default because some data, such as payloads, might be desirable to
                return only when needed because of its large size or because of the
                sensitivity of data that it contains.

                Authorization for ``FULL`` requires ``cloudtasks.tasks.fullView``
                `Google IAM <https://cloud.google.com/iam/>`___ permission on the
                ``Task`` resource.
            filter_ (str): ``filter`` can be used to specify a subset of tasks to lease.

                When ``filter`` is set to ``tag=<my-tag>`` then the ``response`` will
                contain only tasks whose ``tag`` is equal to ``<my-tag>``. ``<my-tag>``
                must be less than 500 characters.

                When ``filter`` is set to ``tag_function=oldest_tag()``, only tasks
                which have the same tag as the task with the oldest ``schedule_time``
                will be returned.

                Grammar Syntax:

                -  ``filter = "tag=" tag | "tag_function=" function``

                -  ``tag = string``

                -  ``function = "oldest_tag()"``

                The ``oldest_tag()`` function returns tasks which have the same tag as
                the oldest task (ordered by schedule time).

                SDK compatibility: Although the SDK allows tags to be either string or
                `bytes <https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/taskqueue/TaskOptions.html#tag-byte:A->`__,
                only UTF-8 encoded tags can be used in Cloud Tasks. Tag which aren't
                UTF-8 encoded can't be used in the ``filter`` and the task's ``tag``
                will be displayed as empty in Cloud Tasks.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.tasks_v2beta2.types.LeaseTasksResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "lease_tasks" not in self._inner_api_calls:
            self._inner_api_calls[
                "lease_tasks"
            ] = google.api_core.gapic_v1.method.wrap_method(
                self.transport.lease_tasks,
                default_retry=self._method_configs["LeaseTasks"].retry,
                default_timeout=self._method_configs["LeaseTasks"].timeout,
                client_info=self._client_info,
            )

        request = cloudtasks_pb2.LeaseTasksRequest(
            parent=parent,
            lease_duration=lease_duration,
            max_tasks=max_tasks,
            response_view=response_view,
            filter=filter_,
        )
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("parent", parent)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header
            )
            metadata.append(routing_metadata)

        return self._inner_api_calls["lease_tasks"](
            request, retry=retry, timeout=timeout, metadata=metadata
        )