<s> """<STR_LIT>""" <EOL> import json <EOL> import requests <EOL> from freezer . apiclient import exceptions <EOL> class SessionManager ( object ) : <EOL> def __init__ ( self , client , verify = True ) : <EOL> self . client = client <EOL> self . endpoint = self . client . endpoint + '<STR_LIT>' <EOL> self . verify = verify <EOL> @ property <EOL> def headers ( self ) : <EOL> return { '<STR_LIT>' : self . client . auth_token } <EOL> def create ( self , doc , session_id = '<STR_LIT>' ) : <EOL> session_id = session_id or doc . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> endpoint = self . endpoint + session_id <EOL> r = requests . post ( endpoint , <EOL> data = json . dumps ( doc ) , <EOL> headers = self . headers , <EOL> verify = self . verify ) <EOL> if r . status_code != <NUM_LIT> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> session_id = r . json ( ) [ '<STR_LIT>' ] <EOL> return session_id <EOL> def delete ( self , session_id ) : <EOL> endpoint = self . endpoint + session_id <EOL> r = requests . delete ( endpoint , headers = self . headers , <EOL> verify = self . verify ) <EOL> if r . status_code != <NUM_LIT> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> def list_all ( self , limit = <NUM_LIT:10> , offset = <NUM_LIT:0> , search = None ) : <EOL> data = json . dumps ( search ) if search else None <EOL> query = { '<STR_LIT>' : int ( limit ) , '<STR_LIT>' : int ( offset ) } <EOL> r = requests . get ( self . endpoint , headers = self . headers , <EOL> params = query , data = data , verify = self . verify ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> return r . json ( ) [ '<STR_LIT>' ] <EOL> def list ( self , limit = <NUM_LIT:10> , offset = <NUM_LIT:0> , search = { } ) : <EOL> new_search = search . copy ( ) <EOL> new_search [ '<STR_LIT>' ] = search . get ( '<STR_LIT>' , [ ] ) <EOL> return self . list_all ( limit , offset , new_search ) <EOL> def get ( self , session_id ) : <EOL> endpoint = self . endpoint + session_id <EOL> r = requests . get ( endpoint , headers = self . headers , verify = self . verify ) <EOL> if r . status_code == <NUM_LIT:200> : <EOL> return r . json ( ) <EOL> if r . status_code == <NUM_LIT> : <EOL> return None <EOL> raise exceptions . ApiClientException ( r ) <EOL> def update ( self , session_id , update_doc ) : <EOL> endpoint = self . endpoint + session_id <EOL> r = requests . patch ( endpoint , <EOL> headers = self . headers , <EOL> data = json . dumps ( update_doc ) , <EOL> verify = self . verify ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> return r . json ( ) [ '<STR_LIT:version>' ] <EOL> def add_job ( self , session_id , job_id ) : <EOL> endpoint = '<STR_LIT>' . format ( self . endpoint , session_id , job_id ) <EOL> r = requests . put ( endpoint , <EOL> headers = self . headers , verify = self . verify ) <EOL> if r . status_code != <NUM_LIT> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> return <EOL> def remove_job ( self , session_id , job_id ) : <EOL> endpoint = '<STR_LIT>' . format ( self . endpoint , session_id , job_id ) <EOL> retry = <NUM_LIT:5> <EOL> r = '<STR_LIT>' <EOL> while retry : <EOL> r = requests . delete ( endpoint , <EOL> headers = self . headers , verify = self . verify ) <EOL> if r . status_code == <NUM_LIT> : <EOL> return <EOL> retry -= <NUM_LIT:1> <EOL> raise exceptions . ApiClientException ( r ) <EOL> def start_session ( self , session_id , job_id , session_tag ) : <EOL> """<STR_LIT>""" <EOL> endpoint = '<STR_LIT>' . format ( self . endpoint , session_id ) <EOL> doc = { "<STR_LIT:start>" : { <EOL> "<STR_LIT>" : job_id , <EOL> "<STR_LIT>" : session_tag <EOL> } } <EOL> r = requests . post ( endpoint , <EOL> headers = self . headers , <EOL> data = json . dumps ( doc ) , <EOL> verify = self . verify ) <EOL> if r . status_code != <NUM_LIT> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> return r . json ( ) <EOL> def end_session ( self , session_id , job_id , session_tag , result ) : <EOL> """<STR_LIT>""" <EOL> endpoint = '<STR_LIT>' . format ( self . endpoint , session_id ) <EOL> doc = { "<STR_LIT:end>" : { <EOL> "<STR_LIT>" : job_id , <EOL> "<STR_LIT>" : session_tag , <EOL> "<STR_LIT:result>" : result <EOL> } } <EOL> r = requests . post ( endpoint , <EOL> headers = self . headers , <EOL> data = json . dumps ( doc ) , <EOL> verify = self . verify ) <EOL> if r . status_code != <NUM_LIT> : <EOL> raise exceptions . ApiClientException ( r ) <EOL> return r . json ( ) </s>
