<s> import dateutil . parser <EOL> import json <EOL> import re <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> class OpenDnsApiException ( Exception ) : pass <EOL> class Api ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , version = <NUM_LIT:1.0> ) : <EOL> self . key = key <EOL> self . version = version <EOL> self . event_time_format = '<STR_LIT>' <EOL> self . base_url = '<STR_LIT>' <EOL> def _get ( self , url_relative_path , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> '<STR_LIT>' : self . key , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> if kwargs is not None : <EOL> for k , v in kwargs . items ( ) : params [ k ] = v <EOL> query_string = urllib . urlencode ( params ) <EOL> url = '<STR_LIT>' . format ( self . base_url , url_relative_path , query_string ) <EOL> response = None <EOL> try : <EOL> response = urllib . urlopen ( url ) <EOL> except Exception , err : <EOL> raise ( OpenDnsApiException ( '<STR_LIT>' . format ( url , err ) ) ) <EOL> results = None <EOL> if response : <EOL> try : <EOL> results = json . load ( response ) <EOL> except Exception , err : <EOL> raise ( OpenDnsApiException ( '<STR_LIT>' . format ( url , err ) ) ) <EOL> return results <EOL> def _post ( self , url_relative_path , data , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> auth_params = { <EOL> '<STR_LIT>' : self . key , <EOL> } <EOL> if kwargs is not None and type ( data ) == type ( { } ) : <EOL> for k , v in kwargs . items ( ) : data [ k ] = v <EOL> url = '<STR_LIT>' . format ( self . base_url , url_relative_path , urllib . urlencode ( auth_params ) ) <EOL> response = None <EOL> try : <EOL> req = urllib2 . Request ( url ) <EOL> req . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> response = urllib2 . urlopen ( req , json . dumps ( data ) ) <EOL> except Exception , err : <EOL> raise ( APIException ( '<STR_LIT>' . format ( url , err ) ) ) <EOL> results = None <EOL> if response : <EOL> try : <EOL> results = json . load ( response ) <EOL> except Exception , err : <EOL> raise ( OpenDnsApiException ( '<STR_LIT>' . format ( url , err ) ) ) <EOL> return results <EOL> def _delete ( self , url_relative_path , params = { } ) : <EOL> """<STR_LIT>""" <EOL> params [ '<STR_LIT>' ] = self . key <EOL> url = '<STR_LIT>' . format ( self . base_url , url_relative_path , urllib . urlencode ( params ) ) <EOL> opener = urllib2 . build_opener ( urllib2 . HTTPHandler ) <EOL> req = urllib2 . Request ( url , None ) <EOL> req . get_method = lambda : '<STR_LIT>' <EOL> req . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> response = None <EOL> try : <EOL> response = urllib2 . urlopen ( req ) <EOL> except urllib2 . HTTPError , err : <EOL> if err . code == <NUM_LIT> : <EOL> response = False <EOL> else : <EOL> raise ( OpenDnsApiException ( '<STR_LIT>' . format ( err ) ) ) <EOL> except Exception , err : <EOL> raise ( OpenDnsApiException ( '<STR_LIT>' . format ( err ) ) ) <EOL> if response and response . getcode ( ) == <NUM_LIT> : <EOL> return True <EOL> else : <EOL> return False <EOL> def add_events ( self , events ) : <EOL> """<STR_LIT>""" <EOL> if type ( events ) != type ( [ ] ) : events = [ events ] <EOL> data = [ ] <EOL> for event in events : data . append ( event . to_json ( ) ) <EOL> response = self . _post ( '<STR_LIT>' , data ) <EOL> if response and response . has_key ( '<STR_LIT:id>' ) : <EOL> return response [ '<STR_LIT:id>' ] <EOL> else : <EOL> return None <EOL> def list_domains ( self , page = <NUM_LIT:1> , get_all = False ) : <EOL> """<STR_LIT>""" <EOL> results = { } <EOL> if get_all : <EOL> more_pages = True <EOL> while more_pages : <EOL> response = self . _get ( '<STR_LIT>' , page = page ) <EOL> for entry in response [ '<STR_LIT:data>' ] : <EOL> results [ entry [ '<STR_LIT:name>' ] ] = entry [ '<STR_LIT:id>' ] <EOL> if response and response [ '<STR_LIT>' ] : <EOL> next_url = urlparse . urlparse ( response [ '<STR_LIT>' ] ) <EOL> page = urlparse . parse_qs ( next_url . query ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" . format ( page ) <EOL> else : <EOL> more_pages = False <EOL> else : <EOL> response = self . _get ( '<STR_LIT>' , page = page ) <EOL> for entry in response [ '<STR_LIT:data>' ] : <EOL> results [ entry [ '<STR_LIT:name>' ] ] = entry [ '<STR_LIT:id>' ] <EOL> return results <EOL> def delete_domain ( self , domain ) : <EOL> """<STR_LIT>""" <EOL> response = None <EOL> m = re . search ( '<STR_LIT>' , domain ) <EOL> if m : <EOL> response = self . _delete ( '<STR_LIT>' . format ( domain ) ) <EOL> else : <EOL> response = self . _delete ( '<STR_LIT>' , { '<STR_LIT>' : domain } ) <EOL> return response </s>
