<s> import json <EOL> import urllib <EOL> import logging <EOL> import urlparse <EOL> import requests <EOL> logger = logging . getLogger ( __name__ ) <EOL> class CTS ( object ) : <EOL> def __init__ ( self , username , password , base_url ) : <EOL> self . auth = ( username , password ) <EOL> self . base_url = base_url <EOL> if not self . base_url . endswith ( "<STR_LIT:/>" ) : <EOL> self . base_url += "<STR_LIT:/>" <EOL> def get_project ( self , project_id ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" % project_id <EOL> instance_data = self . _request ( url ) <EOL> return Project ( instance_data , self ) <EOL> def next_service_request ( self , queue_name , service_type ) : <EOL> """<STR_LIT>""" <EOL> q = { '<STR_LIT>' : queue_name , '<STR_LIT>' : service_type } <EOL> d = self . _request ( "<STR_LIT>" , "<STR_LIT>" , data = q ) <EOL> if not d : <EOL> return None <EOL> return ServiceRequest ( d , self ) <EOL> def get_service_requests ( self , queue_name , service_type ) : <EOL> """<STR_LIT>""" <EOL> q = { '<STR_LIT>' : queue_name , '<STR_LIT>' : service_type } <EOL> d = self . _request ( "<STR_LIT>" , params = q ) <EOL> for request in d [ '<STR_LIT>' ] : <EOL> sr = ServiceRequest ( request , self ) <EOL> sr . reload ( ) <EOL> yield sr <EOL> def get_service_request ( self , key ) : <EOL> """<STR_LIT>""" <EOL> d = self . _request ( "<STR_LIT>" % key ) <EOL> return ServiceRequest ( d , self ) <EOL> def get_bag_instance ( self , key ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" % key <EOL> instance_data = self . _request ( url ) <EOL> return BagInstance ( instance_data , self ) <EOL> def _request ( self , url , method = '<STR_LIT>' , params = { } , data = None ) : <EOL> headers = { "<STR_LIT>" : "<STR_LIT:application/json>" } <EOL> url = urlparse . urljoin ( self . base_url , url ) <EOL> if data != None : <EOL> data = urllib . urlencode ( data ) <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> r = requests . request ( method , url , <EOL> params = params , <EOL> data = data , <EOL> headers = headers , <EOL> auth = self . auth ) <EOL> if r . status_code == <NUM_LIT:200> : <EOL> return json . loads ( r . content ) <EOL> elif r . status_code == <NUM_LIT> or r . status_code == <NUM_LIT> : <EOL> return self . _request ( r . headers [ '<STR_LIT:location>' ] , method , params , data ) <EOL> elif r . status_code == <NUM_LIT> : <EOL> return None <EOL> else : <EOL> logger . error ( "<STR_LIT>" , method , url , params , <EOL> r . status_code ) <EOL> return None <EOL> class Resource ( object ) : <EOL> def __init__ ( self , json_data , cts ) : <EOL> self . data = json_data <EOL> self . cts = cts <EOL> def reload ( self ) : <EOL> url = self . link ( '<STR_LIT>' ) <EOL> self . data = self . cts . _request ( url ) <EOL> @ property <EOL> def url ( self ) : <EOL> return urlparse . urljoin ( self . cts . base_url , self . link ( '<STR_LIT>' ) ) <EOL> def link ( self , rel ) : <EOL> "<STR_LIT>" <EOL> for link in self . data . get ( '<STR_LIT>' , [ ] ) : <EOL> if link . get ( '<STR_LIT>' , None ) == rel : <EOL> return link [ '<STR_LIT>' ] <EOL> return None <EOL> class ServiceRequest ( Resource ) : <EOL> def requeue ( self ) : <EOL> return self . cts . _request ( self . url , '<STR_LIT>' , data = { "<STR_LIT:action>" : "<STR_LIT>" } ) <EOL> def complete ( self ) : <EOL> q = { "<STR_LIT:action>" : "<STR_LIT>" , "<STR_LIT:result>" : "<STR_LIT>" } <EOL> return self . cts . _request ( self . url , '<STR_LIT>' , data = q ) <EOL> def fail ( self , message ) : <EOL> q = { "<STR_LIT:action>" : "<STR_LIT>" , "<STR_LIT:result>" : "<STR_LIT>" , "<STR_LIT>" : message } <EOL> return self . cts . _request ( self . url , '<STR_LIT>' , data = q ) <EOL> class BagInstance ( Resource ) : <EOL> pass <EOL> class Bag ( Resource ) : <EOL> def get_bag_instances ( self ) : <EOL> instances = self . cts . _request ( self . link ( '<STR_LIT>' ) ) <EOL> for instance_data in instances : <EOL> yield BagInstance ( instance_data , self . cts ) <EOL> class Project ( Resource ) : <EOL> def get_bags ( self ) : <EOL> bags = self . cts . _request ( self . link ( '<STR_LIT>' ) ) <EOL> for bag_data in bags : <EOL> yield Bag ( bag_data , self . cts ) </s>
