
    Sj&                     X    d dl Z d dlmZmZ d dlmZmZ ddlmZm	Z	m
Z
 dZ G d d      Zy)	    N)	HTTPErrorURLError)Requesturlopen   )EngineClientErrorEngineConnectionErrorEngineResponseErrorz1.0.0c                   @    e Zd ZdZd
dZddZd Zd Zd Zd Z	dd	Z
y)EngineClientz>Small, versioned boundary around the private image engine API.c                 >    |j                  d      | _        || _        y )N/)rstripbase_urltimeout)selfr   r   s      /app/engine_sdk/client.py__init__zEngineClient.__init__   s     ,    Nc                    d }dt         d}|+t        j                  |d      j                  d      }d|d<   t	        | j
                   | |||      }	 t        ||xs | j                        5 }|j                         }	t        j                  |	j                  d            }
d d d        t)        
t*              st        d      |
S # 1 sw Y   &xY w# t        $ rD}|j                         j                  dd	      d d
 }t        d|j                   d|       |d }~wt        t        t         f$ r}t#        d|       |d }~wt$        t        j&                  f$ r}t        d      |d }~ww xY w)Nzapplication/json)AcceptzX-Engine-SDK-VersionF)ensure_asciizutf-8zContent-Type)dataheadersmethodr   replacei  engine_http_z: zengine_unreachable: engine_invalid_jsonengine_response_must_be_object)SDK_VERSIONjsondumpsencoder   r   r   r   readloadsdecoder   r
   coder   TimeoutErrorOSErrorr	   UnicodeDecodeErrorJSONDecodeError
isinstancedict)r   r   pathpayloadr   r   r   requestresultrawresponseerrordetails                r   _requestzEngineClient._request   sc   /U::gE:AA'JD&8GN#}}odV$	

	H'*AT\\Bfkkm::cjj&9: C (D)%&FGG CB  	XZZ\(()<TcBF%UZZL6(&KLRWW,0 	S'*>ug(FGUR"D$8$89 	H%&;<%G	HsH   C 25C'C CC 	E3!?D  E37EE3"E..E3c                 R    | j                  ddt        | j                  d            S )z!Return the engine health payload.GETz/api/health   r   )r7   minr   r   s    r   healthzEngineClient.health-   s"    }}UM3t||Q;O}PPr   c                 &    | j                  dd      S )z2Return the engine-owned, versioned preset catalog.r9   z/api/presetsr7   r<   s    r   list_presetszEngineClient.list_presets1   s    }}UN33r   c                 &    | j                  dd      S )zAReturn engine capabilities, including its current preset catalog.r9   z/api/capabilitiesr?   r<   s    r   capabilitieszEngineClient.capabilities5   s    }}U$788r   c                     	 | j                         }d| j                  |dS # t        $ r#}d| j                  t        |      dcY d}~S d}~ww xY w)z>Return a normalized status object for dashboard health checks.T)	connectedurlr=   F)rD   rE   r5   N)r=   r   r   str)r   r=   r5   s      r   statuszEngineClient.status9   sK    	S[[]F!%dmmvNN  	S!&t}}s5zRR	Ss   " 	AA	A	Ac                     |||d}|j                  |       | j                  dd|      }|j                  d      }|j                  d      dvst        |t              st        d      |S )z8Generate one or more assets through the engine contract.)promptcolorsreferenceProfilePOSTz/api/generateassetrG   )Ncreatedengine_generation_missing_asset)updater7   getr-   r.   r
   )r   rI   rJ   reference_profileoptionsr0   	generatedrM   s           r   generatezEngineClient.generateA   si    #vK\]wMM&/7C	g&=="*;;:eUYCZ%&GHHr   )   )NN)__name__
__module____qualname____doc__r   r7   r=   r@   rB   rG   rU    r   r   r   r      s*    H6Q49Sr   r   )r"   urllib.errorr   r   urllib.requestr   r   
exceptionsr   r	   r
   r!   r   r[   r   r   <module>r_      s%     , + U U > >r   