
    
FZj)                         d Z ddlZddlZddlmZ ddlZddlmZm	Z	m
Z
mZmZ ddlmZmZmZ ddlmZmZ ddlmZmZ  G d	 d
e      Ze	dfdZe	ddfdZy)zs
mygeotab.api_async
~~~~~~~~~~~~~~~~~~~~~~

Async/Await-able public objects and methods wrapping the MyGeotab API.
    N)TimeoutError   )APIDEFAULT_TIMEOUT_processget_api_urlget_headers)AuthenticationExceptionMyGeotabExceptionTimeoutException)camelcaseify_parametersconvert_get_parameters)json_deserializejson_serializec                   f     e Zd ZdZddddeddf fd	Zd Zd Zd Zd Z	d	 Z
d
 Zed        Z xZS )r   zBA simple, asynchronous, and Pythonic wrapper for the MyGeotab API.Nzmy.geotab.comc	           
      4    t         	|   ||||||||       y)a  
        Initialize the asynchronous MyGeotab API object with credentials.

        :param username: The username used for MyGeotab servers. Usually an email address.
        :param password: The password associated with the username. Optional if `session_id` is provided.
        :param database: The database or company name. Optional as this usually gets resolved upon authentication.
        :param session_id: A session ID, assigned by the server.
        :param server: The server ie. my23.geotab.com. Optional as this usually gets resolved upon authentication.
        :param timeout: The timeout to make the call, in seconds. By default, this is 300 seconds (or 5 minutes).
        :param proxies: The proxies dictionary to apply to the request.
        :param cert: The path to client certificate. A single path to .pem file or a Tuple (.cer file, .pem file)
        :raise Exception: Raises an Exception if a username, or one of the session_id or password is not provided.
        )proxiescertN)super__init__)
selfusernamepassworddatabase
session_idservertimeoutr   r   	__class__s
            TC:\Users\Rodrigo\frota-trucks-cliente\python\Lib\site-packages\mygeotab/api_async.pyr   zAPI.__init__   s%    0 	8Xz67\cjno    c                 z  K   |t        d      t        |      }| j                  r&| j                  j                  s| j	                          d|vr3| j                  j                  r| j                  j                         |d<   	 t        | j                  ||| j                  | j                         d{   }|d| _
        |S 7 # t        $ r}|j                  dk(  s|j                  dk(  rd|j                  v r| j                  dk(  r\| j                  j                  rF| xj                  d	z  c_
        | j	                           | j                  |fi | d{  7  cY d}~S t!        | j                  j"                  | j                  j$                  | j                  j&                        | d}~ww xY ww)
a  Makes an async call to the API.

        :param method: The method name.
        :param params: Additional parameters to send (for example, search=dict(id='b123') )
        :return: The JSON result (decoded into a dict) from the server.abs
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server.
        :raise TimeoutException: Raises when the request does not respond after some time.
        NA method name must be specifiedcredentials)
verify_sslr   r   InvalidUserExceptionDbUnavailableExceptionInitializingr   )	Exceptionr   r#   r   authenticate	get_param_query_server_is_verify_ssl_cert_API__reauthorize_countr   namemessager   
call_asyncr
   r   r   r   )r   method
parametersparamsresult	exceptions         r   r2   zAPI.call_async3   s     >=>>(4D$4$4$?$?&4+;+;+F+F$($4$4$>$>$@F=!	!$,,4K^K^eieoeoppF!+,(M q ! 	~~!77"::~QZQbQb?b++q0T5E5E5N5N,,1,%%'!0!F:!FFFF1((1143C3C3L3LdN^N^NeNe$% 	s\   BF;1C 6C7C F;C 	F8BF3E F3"F8#F;(AF33F88F;c           	         K   |D cg c]%  }t        |d   t        |      dkD  r|d   ni       ' }}| j                  d|       d{   S c c}w 7 	w)a  Performs an async multi-call to the API

        :param calls: A list of call 2-tuples with method name and params (for example, ('Get', dict(typeName='Trip')) )
        :return: The JSON result (decoded into a dict) from the server
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server
        :raise TimeoutException: Raises when the request does not respond after some time.
        r   r   )r3   r5   ExecuteMultiCall)callsN)dictlenr2   )r   r:   callformatted_callss       r   multi_call_asynczAPI.multi_call_asyncW   s]      fkkej]a4tAw#d)a-tAwUWXejk__%7_OOO lOs   A*AA	A

Ac                 X   K    | j                   dd|it        |       d{   S 7 w)a  Gets entities asynchronously using the API. Shortcut for using async_call() with the 'Get' method.

        :param type_name: The type of entity.
        :param parameters: Additional parameters to send.
        :return: The JSON result (decoded into a dict) from the server.
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server.
        :raise TimeoutException: Raises when the request does not respond after some time.
        	type_nameN)Get)r2   r   )r   rA   r4   s      r   	get_asynczAPI.get_asyncb   s.      %T__fifCYZdCeffffs   !*(*c                 F   K   | j                  d||       d{   S 7 w)a  
        Adds an entity asynchronously using the API. Shortcut for using async_call() with the 'Add' method.

        :param type_name: The type of entity.
        :param entity: The entity to add.
        :return: The id of the object added.
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server.
        :raise TimeoutException: Raises when the request does not respond after some time.
        AddrA   entityNr2   r   rA   rG   s      r   	add_asynczAPI.add_asyncm   s#      __Ui_OOOO   !!c                 F   K   | j                  d||       d{   S 7 w)a  Sets an entity asynchronously using the API. Shortcut for using async_call() with the 'Set' method.

        :param type_name: The type of entity
        :param entity: The entity to set
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server
        SetrF   NrH   rI   s      r   	set_asynczAPI.set_asyncy   s#      __Ui_OOOOrK   c                 F   K   | j                  d||       d{   S 7 w)a  Removes an entity asynchronously using the API. Shortcut for using async_call() with the 'Remove' method.

        :param type_name: The type of entity.
        :param entity: The entity to remove.
        :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server.
        :raise TimeoutException: Raises when the request does not respond after some time.
        RemoverF   NrH   rI   s      r   remove_asynczAPI.remove_async   s#      __X6_RRRRrK   c                     t        | j                  | j                  | j                  | j                  | j
                        S )zReturns a new async API object from an existing Credentials object.

        :param credentials: The existing saved credentials.
        :return: A new API object populated with MyGeotab credentials.
        )r   r   r   r   r   )r   r   r   r   r   r   )r#   s    r   from_credentialszAPI.from_credentials   s>      )) )) ))"--%%
 	
r    )__name__
__module____qualname____doc__r   r   r2   r?   rC   rJ   rN   rQ   staticmethodrS   __classcell__)r   s   @r   r   r      s[    L
 p4"H	P	g
PPS 
 
r    r   Tc                    K   | t        d      |t        d      t        |      }t        || |||       d{   S 7 w)a  Makes an asynchronous call to an un-authenticated method on a server.

    :param method: The method name.
    :param server: The MyGeotab server.
    :param timeout: The timeout to make the call, in seconds. By default, this is 300 seconds (or 5 minutes).
    :param verify_ssl: If True, verify the SSL certificate. It's recommended not to modify this.
    :param parameters: Additional parameters to send (for example, search=dict(id='b123') ).
    :return: The JSON result (decoded into a dict) from the server.
    :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server.
    :raise TimeoutException: Raises when the request does not respond after some time.
    Nr"   z/A server (eg. my3.geotab.com) must be specified)r   r$   )r(   r   r+   )r3   r   r   r$   r4   s        r   server_call_asyncr[      sN      ~9::~IJJ(4J
GPZ[[[[s   9AA Ac                   K   t        |       }t        d||      }t               }d}	|s|rft        j                  t        j
                        }	|	j                          t        t        d      r#|	xj                  t        j                  z  c_        |rIt        |t              r|	j                  |       n't        |t              r|\  }
}|	j                  |
|       t        j                  |	      }	 t        j                   |      4 d{   }|j#                  |t%        |      ||d	       d{   }|j'                          |j(                  j+                  d
      }|j-                          d{   }ddd      d{    rd|j5                         vrS t7        t9                    S 7 7 7 B7 4# 1 d{  7  sw Y   DxY w# t.        t0        j.                  f$ r}t3        |       |d}~ww xY ww)a'  Formats and performs the asynchronous query against the API

    :param server: The server to query.
    :param method: The method name.
    :param parameters: A dict of parameters to send
    :param timeout: The timeout to make the call, in seconds. By default, this is 300 seconds (or 5 minutes).
    :param verify_ssl: Whether or not to verify SSL connections
    :param cert: The path to client certificate. A single path to .pem file or a Tuple (.cer file, .pem file)
    :return: The JSON-decoded result from the server
    :raise MyGeotabException: Raises when an exception occurs on the MyGeotab server
    :raise TimeoutException: Raises when the request does not respond after some time.
    :raise aiohttp.ClientResponseError: Raises when there is an HTTP status code that indicates failure.
    )idr3   r5   FOP_ENABLE_MIDDLEBOX_COMPAT)ssl)	connectorNT)dataheadersr   allow_redirectszContent-Typezapplication/json)r   r;   r	   r`   
SSLContextPROTOCOL_TLS_CLIENTload_default_certshasattroptionsr_   
isinstancestrload_cert_chaintupleaiohttpTCPConnectorClientSessionpostr   raise_for_statusrc   gettextr   asyncior   lowerr   r   )r   r3   r4   r   r$   r   api_endpointr5   rc   ssl_contextcerkeyconnsessionresponsecontent_typebodyexcs                     r   r+   r+      s     v&LRz:FmGKTnnS%<%<=&&(3453#A#AAdC ''-e$HC''S1K0D	0((488G$\\>&#97T[mq *  H %%'#++//?L!(D 98 *,2D2D2FF$T*++ 9
 ) 9888 '../ 0v&C/0s   C1H4G F7G #F?5F96AF?8F;9F?=G F=	G *H7G 9F?;F?=G ?GGGG G>-G99G>>H)rW   ru   r`   concurrent.futuresr   rn   apir   SyncAPIr   r   r   r	   
exceptionsr
   r   r   r4   r   r   serializersr   r   r[   r+    r    r   <module>r      sY     
 +  T T T T G 9C
' C
L 5DPT \( 6EQU\` ,,r    