<s> import httplib2 <EOL> import json <EOL> import datetime <EOL> from urlparse import urljoin <EOL> from Cookie import SimpleCookie <EOL> from error import AuthenticationError , GerritError , NotSignedInError <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , verify = True ) : <EOL> self . host = host <EOL> self . cookie = '<STR_LIT>' <EOL> self . xsrf_key = None <EOL> self . http = httplib2 . Http ( disable_ssl_certificate_validation = <EOL> not verify ) <EOL> self . http . follow_redirects = False <EOL> self . http . follow_all_redirects = False <EOL> def request ( self , url , body = None , method = '<STR_LIT:POST>' ) : <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . cookie , <EOL> } <EOL> response , content = self . http . request ( url , method , <EOL> body , headers = headers ) <EOL> if '<STR_LIT>' in response : <EOL> self . cookie = response [ '<STR_LIT>' ] <EOL> cookie = SimpleCookie ( ) <EOL> cookie . load ( self . cookie ) <EOL> self . xsrf_key = cookie [ '<STR_LIT>' ] . value <EOL> return response , content <EOL> class Service ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> self . url = urljoin ( self . connection . host + '<STR_LIT:/>' , self . __class__ . __name__ ) <EOL> def _request ( self , * args , ** kwargs ) : <EOL> return self . connection . request ( * args , ** kwargs ) <EOL> def _call ( self , method , * params ) : <EOL> body = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : method , '<STR_LIT>' : params } <EOL> if self . connection . xsrf_key : <EOL> body [ '<STR_LIT>' ] = self . connection . xsrf_key <EOL> body = json . dumps ( body ) <EOL> data = json . loads ( self . _request ( self . url , body ) [ <NUM_LIT:1> ] ) <EOL> if '<STR_LIT:error>' in data : <EOL> error = GerritError <EOL> if data [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] == '<STR_LIT>' : <EOL> error = NotSignedInError <EOL> raise error ( data [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] ) <EOL> return data [ '<STR_LIT:result>' ] <EOL> class ChangeListService ( Service ) : <EOL> def allQueryNext ( self , search = '<STR_LIT>' , last_seen = '<STR_LIT:z>' , page_size = <NUM_LIT> ) : <EOL> data = self . _call ( '<STR_LIT>' , search , last_seen , page_size ) <EOL> return data [ '<STR_LIT>' ] <EOL> class ChangeDetailService ( Service ) : <EOL> def changeDetail ( self , change_id ) : <EOL> return self . _call ( '<STR_LIT>' , change_id . to_json ( ) ) <EOL> def patchSetDetail ( self , patchset_id ) : <EOL> return self . _call ( '<STR_LIT>' , patchset_id . to_json ( ) , None , None ) <EOL> class PatchDetailService ( Service ) : <EOL> def publishComments ( self , patchset_id , comment , votes ) : <EOL> return self . _call ( '<STR_LIT>' , <EOL> patchset_id . to_json ( ) , <EOL> comment , <EOL> votes ) <EOL> def patchScript ( self , patch_id , patchset_id , diff_base_id = None , <EOL> diff_prefs = None ) : <EOL> return self . _call ( '<STR_LIT>' , <EOL> patch_id . to_json ( ) , <EOL> diff_base_id . to_json ( ) if diff_base_id else None , <EOL> patchset_id . to_json ( ) , <EOL> None ) <EOL> def saveDraft ( self , patch_id , line , message , side = <NUM_LIT:1> , timestamp = None ) : <EOL> if not timestamp : <EOL> timestamp = datetime . datetime . now ( ) <EOL> return self . _call ( '<STR_LIT>' , { <EOL> '<STR_LIT:key>' : { '<STR_LIT>' : patch_id . to_json ( ) } , <EOL> '<STR_LIT>' : line , <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : str ( timestamp ) , <EOL> '<STR_LIT:status>' : '<STR_LIT:d>' , <EOL> '<STR_LIT>' : side , <EOL> } ) <EOL> class ProjectAdminService ( Service ) : <EOL> def visibleProjects ( self ) : <EOL> return self . _call ( '<STR_LIT>' ) <EOL> class AccountService ( Service ) : <EOL> def myAccount ( self ) : <EOL> return self . _call ( '<STR_LIT>' ) <EOL> class UserPassAuthService ( Service ) : <EOL> def authenticate ( self , username , password ) : <EOL> data = self . _call ( '<STR_LIT>' , username , password ) <EOL> if not data [ '<STR_LIT:success>' ] : <EOL> raise AuthenticationError ( ) <EOL> class BecomeAuthService ( Service ) : <EOL> def authenticate ( self , username ) : <EOL> response , content = self . _request ( <EOL> urljoin ( self . connection . host , '<STR_LIT>' + username ) , <EOL> method = '<STR_LIT:GET>' ) <EOL> if response . status != <NUM_LIT> : <EOL> raise AuthenticationError ( content ) </s>
