a
    !f/                     @   s:  d Z ddlZzLddlmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ ddlmZ W n. ey   d	Zd ZZd Z
Zd ZZY n0 d
ZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ eo$ej%&dduZ'G dd deZ(dS )z?Client for interacting with the Google Stackdriver Logging API.    N)ConfigServiceV2Api)LoggingServiceV2Api)MetricsServiceV2Api)_LoggingAPI)_MetricsAPI)	_SinksAPIFT)
JSONClient)
Connection)ProtobufEntry)StructEntry)	TextEntryLoggerMetricSinkZGCLOUD_ENABLE_GAXc                   @   s   e Zd ZdZeZd Z ZZe	dd Z
e	dd Ze	dd Zd	d
 Zdd ZdddZdddZdddZdddZdddZdS )Clienta  Client to bundle configuration needed for API requests.

    :type project: str
    :param project: the project which the client acts on behalf of.
                    If not passed, falls back to the default inferred
                    from the environment.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for the connection
                        owned by this client. If not passed (and if no ``http``
                        object is passed), falls back to the default inferred
                        from the environment.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests. If not passed, an
                 ``http`` object is created that is bound to the
                 ``credentials`` for the current object.
    Nc                 C   s2   | j du r,tr t }t|| _ nt| j| _ | j S )zHelper for logging-related API calls.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
        N)_logging_api_USE_GAXGeneratedLoggingAPIGAXLoggingAPIJSONLoggingAPI
connectionself	generated r   V/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/logging/client.pylogging_apiM   s    
zClient.logging_apic                 C   s2   | j du r,tr t }t|| _ nt| j| _ | j S )zHelper for log sink-related API calls.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks
        N)
_sinks_apir   GeneratedSinksAPIGAXSinksAPIJSONSinksAPIr   r   r   r   r   	sinks_api]   s    
zClient.sinks_apic                 C   s2   | j du r,tr t }t|| _ nt| j| _ | j S )zHelper for log metric-related API calls.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics
        N)_metrics_apir   GeneratedMetricsAPIGAXMetricsAPIJSONMetricsAPIr   r   r   r   r   metrics_apil   s    
zClient.metrics_apic                 C   s   t || dS )zCreates a logger bound to the current client.

        :type name: str
        :param name: the name of the logger to be constructed.

        :rtype: :class:`gcloud.logging.logger.Logger`
        :returns: Logger created with the current client.
        clientr   )r   namer   r   r   logger{   s    	zClient.loggerc                 C   sN   d|v rt || |S d|v r,t|| |S d|v rBt|| |S tddS )aj  Detect correct entry type from resource and instantiate.

        :type resource: dict
        :param resource: one entry resource from API response

        :type loggers: dict or None
        :param loggers: A mapping of logger fullnames -> loggers.  If not
                        passed, the entry will have a newly-created logger.

        :rtype: One of:
                :class:`gcloud.logging.entries.TextEntry`,
                :class:`gcloud.logging.entries.StructEntry`,
                :class:`gcloud.logging.entries.ProtobufEntry`
        :returns: the entry instance, constructed via the resource
        ZtextPayloadZjsonPayloadZprotoPayloadzCannot parse log entry resourceN)r   from_api_reprr   r
   
ValueError)r   resourceloggersr   r   r   _entry_from_resource   s    zClient._entry_from_resourcec           	         sJ   |du rj g}jj|||||d\}}i   fdd|D }||fS )a3  Return a page of log entries.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/list

        :type projects: list of strings
        :param projects: project IDs to include. If not passed,
                            defaults to the project bound to the client.

        :type filter_: str
        :param filter_: a filter expression. See:
                        https://cloud.google.com/logging/docs/view/advanced_filters

        :type order_by: str
        :param order_by: One of :data:`gcloud.logging.ASCENDING` or
                         :data:`gcloud.logging.DESCENDING`.

        :type page_size: int
        :param page_size: maximum number of entries to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: str
        :param page_token: opaque marker for the next "page" of entries. If not
                           passed, the API will return the first page of
                           entries.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.logging.entry.TextEntry`, plus a
                  "next page token" string:  if not None, indicates that
                  more entries can be retrieved with another call (pass that
                  value as ``page_token``).
        N)projectsfilter_order_by	page_size
page_tokenc                    s   g | ]} | qS r   )r2   .0r0   r1   r   r   r   
<listcomp>   s   z'Client.list_entries.<locals>.<listcomp>)projectr   list_entries)	r   r3   r4   r5   r6   r7   	resourcestokenentriesr   r:   r   r=      s    "
zClient.list_entriesc                 C   s   t |||| dS )aI  Creates a sink bound to the current client.

        :type name: str
        :param name: the name of the sink to be constructed.

        :type filter_: str
        :param filter_: (optional) the advanced logs filter expression
                        defining the entries exported by the sink.  If not
                        passed, the instance should already exist, to be
                        refreshed via :meth:`Sink.reload`.

        :type destination: str
        :param destination: destination URI for the entries exported by
                            the sink.  If not passed, the instance should
                            already exist, to be refreshed via
                            :meth:`Sink.reload`.

        :rtype: :class:`gcloud.logging.sink.Sink`
        :returns: Sink created with the current client.
        r*   r   )r   r,   r4   destinationr   r   r   sink   s    zClient.sinkc                    s0    j  j||\}} fdd|D }||fS )aU  List sinks for the project associated with this client.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/list

        :type page_size: int
        :param page_size: maximum number of sinks to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: str
        :param page_token: opaque marker for the next "page" of sinks. If not
                           passed, the API will return the first page of
                           sinks.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.logging.sink.Sink`, plus a
                  "next page token" string:  if not None, indicates that
                  more sinks can be retrieved with another call (pass that
                  value as ``page_token``).
        c                    s   g | ]}t | qS r   )r   r.   r8   r   r   r   r;      s   z%Client.list_sinks.<locals>.<listcomp>)r$   
list_sinksr<   )r   r6   r7   r>   r?   Zsinksr   rC   r   rD      s    
zClient.list_sinks c                 C   s   t ||| |dS )a+  Creates a metric bound to the current client.

        :type name: str
        :param name: the name of the metric to be constructed.

        :type filter_: str
        :param filter_: the advanced logs filter expression defining the
                        entries tracked by the metric.  If not
                        passed, the instance should already exist, to be
                        refreshed via :meth:`Metric.reload`.

        :type description: str
        :param description: the description of the metric to be constructed.
                            If not passed, the instance should already exist,
                            to be refreshed via :meth:`Metric.reload`.

        :rtype: :class:`gcloud.logging.metric.Metric`
        :returns: Metric created with the current client.
        )r+   descriptionr   )r   r,   r4   rF   r   r   r   metric   s    zClient.metricc                    s0    j  j||\}} fdd|D }||fS )ae  List metrics for the project associated with this client.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics/list

        :type page_size: int
        :param page_size: maximum number of metrics to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: str
        :param page_token: opaque marker for the next "page" of metrics. If not
                           passed, the API will return the first page of
                           metrics.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.logging.metric.Metric`, plus a
                  "next page token" string:  if not None, indicates that
                  more metrics can be retrieved with another call (pass that
                  value as ``page_token``).
        c                    s   g | ]}t | qS r   )r   r.   r8   rC   r   r   r;   *  s   z'Client.list_metrics.<locals>.<listcomp>)r)   list_metricsr<   )r   r6   r7   r>   r?   Zmetricsr   rC   r   rH     s    
zClient.list_metrics)NNNNN)NN)NN)NrE   )NN)__name__
__module____qualname____doc__r	   Z_connection_classr   r    r%   propertyr   r$   r)   r-   r2   r=   rB   rD   rG   rH   r   r   r   r   r   5   s$   


  
-


r   ))rL   osZ-google.cloud.logging.v2.config_service_v2_apir   r!   Z.google.cloud.logging.v2.logging_service_v2_apir   r   Z.google.cloud.logging.v2.metrics_service_v2_apir   r&   Zgcloud.logging._gaxr   r   r   r'   r   r"   ImportErrorZ	_HAVE_GAXZgcloud.clientr   Zgcloud.logging.connectionr	   r   r(   r#   Zgcloud.logging.entriesr
   r   r   Zgcloud.logging.loggerr   Zgcloud.logging.metricr   Zgcloud.logging.sinkr   environgetr   r   r   r   r   r   <module>   s6   