B
    cb                 @   sl   d dl Z d dlmZ d dl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 G dd	 d	eZdS )
    N)	urlencode)reverse_lazy)settings)cache)Response)statusc               @   sd   e Zd ZejZejZejZdddZedd Z	dd Z
dd	 Zd
d Zdd Zdd Zdd ZdS )!HttpRequestInterfaceForIoTServiceNc             C   s    || _ || _|| _|  | _d S )N)urlbodyparams_get_or_update_access_tokenaccess_token)selfr	   r
   r    r   ;G:\easytimepro\master/mysite/cloud\minerva_iot_interface.py__init__   s    z*HttpRequestInterfaceForIoTService.__init__c       	      C   s   t d}|s`tj}tj}||d}ddi}tj| j|t	|d }|d }t j
d|dd d|d	}tj||d
 }|S )Ndeveloper_portal_access_token)ZemailpasswordzContent-Typezapplication/json)headersdatar   i  )timeout)zContent-TypeAuthorization)r   )r   getr   ZUSERNAMEZPASSWORDrequestspostDEVELOPER_PORTAL_LOGIN_URLjsondumpsset)	r   Zrequest_urlr   Zusernamer   r
   r   contentZdeveloper_portal_headersr   r   r   &requests_content_from_developer_portal   s    

zHHttpRequestInterfaceForIoTService.requests_content_from_developer_portalc             C   s   t j}t j}t j}tdd }|s|||d}tj| jddi|d	 }|d d }|d d }d
| j|}| | |}	|	d	 d
kr|	d d }
tjd||d tjd||d tjd|
|d ntdddtjdS |S )Nr   )
grant_type	client_idclient_secretzContent-Typezapplication/json)r   r   r   
expires_inz{}/{}ZretSUCCESSid)r   r"   application_idZinvalid_application_idzInvalid application id.)errorZerror_description)r   )r   Z
GRANT_TYPEZ	CLIENT_IDZCLIENT_SECRETr   r   r   r   #SUBSCRIPTION_SERVICE_AUTHENTICATIONr   format DEVELOPER_PORTAL_APPLICATION_URLr    r   r   r   ZHTTP_401_UNAUTHORIZED)r   r!   r"   r#   r   r
   Zauthorization_contentr$   Z developer_portal_url_applicationZapplication_id_contentr'   r   r   r   r   ,   s0    
z=HttpRequestInterfaceForIoTService._get_or_update_access_tokenc             C   s   | j }dd|d}|S )Nzapplication/jsonz	Bearer {})zContent-Typer   )r   r*   )r   r   Zauthenticate_headerr   r   r   generate_authenticate_headerj   s    z>HttpRequestInterfaceForIoTService.generate_authenticate_headerc             C   s    t j| j|  | jd }|S )N)r   r   )r   r   r	   r,   r
   r   )r   r   r   r   r   r   s   s    z&HttpRequestInterfaceForIoTService.postc             C   s    t j| j|  | jd }|S )N)r   r   )r   putr	   r,   r
   r   )r   r   r   r   r   r-   x   s    z%HttpRequestInterfaceForIoTService.putc             C   s    t j| j|  | jd }|S )N)r   r   )r   r   r	   r,   r   r   )r   r   r   r   r   r   }   s    z%HttpRequestInterfaceForIoTService.getc             C   s   t j| j|  d }|S )N)r   )r   deleter	   r,   r   )r   r   r   r   r   r.      s    z(HttpRequestInterfaceForIoTService.delete)NN)__name__
__module____qualname__r   r   r+   r)   r   staticmethodr    r   r,   r   r-   r   r.   r   r   r   r   r      s   
>	r   )r   Zurllib.parser   r   Zdjango.urlsr   Zdjango.confr   Zdjango.core.cacher   Zrest_framework.responser   Zrest_frameworkr   objectr   r   r   r   r   <module>   s   