<s> import xml . sax <EOL> import time <EOL> import boto <EOL> from boto . connection import AWSAuthConnection <EOL> from boto import handler <EOL> from boto . cloudfront . distribution import Distribution , DistributionSummary , DistributionConfig <EOL> from boto . cloudfront . distribution import StreamingDistribution , StreamingDistributionSummary , StreamingDistributionConfig <EOL> from boto . cloudfront . identity import OriginAccessIdentity <EOL> from boto . cloudfront . identity import OriginAccessIdentitySummary <EOL> from boto . cloudfront . identity import OriginAccessIdentityConfig <EOL> from boto . cloudfront . invalidation import InvalidationBatch , InvalidationSummary , InvalidationListResultSet <EOL> from boto . resultset import ResultSet <EOL> from boto . cloudfront . exception import CloudFrontServerError <EOL> class CloudFrontConnection ( AWSAuthConnection ) : <EOL> DefaultHost = '<STR_LIT>' <EOL> Version = '<STR_LIT>' <EOL> def __init__ ( self , aws_access_key_id = None , aws_secret_access_key = None , <EOL> port = None , proxy = None , proxy_port = None , <EOL> host = DefaultHost , debug = <NUM_LIT:0> , security_token = None , <EOL> validate_certs = True , profile_name = None , https_connection_factory = None ) : <EOL> super ( CloudFrontConnection , self ) . __init__ ( host , <EOL> aws_access_key_id , aws_secret_access_key , <EOL> True , port , proxy , proxy_port , debug = debug , <EOL> security_token = security_token , <EOL> validate_certs = validate_certs , <EOL> https_connection_factory = https_connection_factory , <EOL> profile_name = profile_name ) <EOL> def get_etag ( self , response ) : <EOL> response_headers = response . msg <EOL> for key in response_headers . keys ( ) : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> return response_headers [ key ] <EOL> return None <EOL> def _required_auth_capability ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> def _get_all_objects ( self , resource , tags , result_set_class = None , <EOL> result_set_kwargs = None ) : <EOL> if not tags : <EOL> tags = [ ( '<STR_LIT>' , DistributionSummary ) ] <EOL> response = self . make_request ( '<STR_LIT:GET>' , '<STR_LIT>' % ( self . Version , <EOL> resource ) ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status >= <NUM_LIT> : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> rs_class = result_set_class or ResultSet <EOL> rs_kwargs = result_set_kwargs or dict ( ) <EOL> rs = rs_class ( tags , ** rs_kwargs ) <EOL> h = handler . XmlHandler ( rs , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return rs <EOL> def _get_info ( self , id , resource , dist_class ) : <EOL> uri = '<STR_LIT>' % ( self . Version , resource , id ) <EOL> response = self . make_request ( '<STR_LIT:GET>' , uri ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status >= <NUM_LIT> : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> d = dist_class ( connection = self ) <EOL> response_headers = response . msg <EOL> for key in response_headers . keys ( ) : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> d . etag = response_headers [ key ] <EOL> h = handler . XmlHandler ( d , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return d <EOL> def _get_config ( self , id , resource , config_class ) : <EOL> uri = '<STR_LIT>' % ( self . Version , resource , id ) <EOL> response = self . make_request ( '<STR_LIT:GET>' , uri ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status >= <NUM_LIT> : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> d = config_class ( connection = self ) <EOL> d . etag = self . get_etag ( response ) <EOL> h = handler . XmlHandler ( d , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return d <EOL> def _set_config ( self , distribution_id , etag , config ) : <EOL> if isinstance ( config , StreamingDistributionConfig ) : <EOL> resource = '<STR_LIT>' <EOL> else : <EOL> resource = '<STR_LIT>' <EOL> uri = '<STR_LIT>' % ( self . Version , resource , distribution_id ) <EOL> headers = { '<STR_LIT>' : etag , '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> response = self . make_request ( '<STR_LIT>' , uri , headers , config . to_xml ( ) ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> return self . get_etag ( response ) <EOL> def _create_object ( self , config , resource , dist_class ) : <EOL> response = self . make_request ( '<STR_LIT:POST>' , '<STR_LIT>' % ( self . Version , <EOL> resource ) , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , <EOL> data = config . to_xml ( ) ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status == <NUM_LIT> : <EOL> d = dist_class ( connection = self ) <EOL> h = handler . XmlHandler ( d , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> d . etag = self . get_etag ( response ) <EOL> return d <EOL> else : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> def _delete_object ( self , id , etag , resource ) : <EOL> uri = '<STR_LIT>' % ( self . Version , resource , id ) <EOL> response = self . make_request ( '<STR_LIT>' , uri , { '<STR_LIT>' : etag } ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status != <NUM_LIT> : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> def get_all_distributions ( self ) : <EOL> tags = [ ( '<STR_LIT>' , DistributionSummary ) ] <EOL> return self . _get_all_objects ( '<STR_LIT>' , tags ) <EOL> def get_distribution_info ( self , distribution_id ) : <EOL> return self . _get_info ( distribution_id , '<STR_LIT>' , Distribution ) <EOL> def get_distribution_config ( self , distribution_id ) : <EOL> return self . _get_config ( distribution_id , '<STR_LIT>' , <EOL> DistributionConfig ) <EOL> def set_distribution_config ( self , distribution_id , etag , config ) : <EOL> return self . _set_config ( distribution_id , etag , config ) <EOL> def create_distribution ( self , origin , enabled , caller_reference = '<STR_LIT>' , <EOL> cnames = None , comment = '<STR_LIT>' , trusted_signers = None ) : <EOL> config = DistributionConfig ( origin = origin , enabled = enabled , <EOL> caller_reference = caller_reference , <EOL> cnames = cnames , comment = comment , <EOL> trusted_signers = trusted_signers ) <EOL> return self . _create_object ( config , '<STR_LIT>' , Distribution ) <EOL> def delete_distribution ( self , distribution_id , etag ) : <EOL> return self . _delete_object ( distribution_id , etag , '<STR_LIT>' ) <EOL> def get_all_streaming_distributions ( self ) : <EOL> tags = [ ( '<STR_LIT>' , StreamingDistributionSummary ) ] <EOL> return self . _get_all_objects ( '<STR_LIT>' , tags ) <EOL> def get_streaming_distribution_info ( self , distribution_id ) : <EOL> return self . _get_info ( distribution_id , '<STR_LIT>' , <EOL> StreamingDistribution ) <EOL> def get_streaming_distribution_config ( self , distribution_id ) : <EOL> return self . _get_config ( distribution_id , '<STR_LIT>' , <EOL> StreamingDistributionConfig ) <EOL> def set_streaming_distribution_config ( self , distribution_id , etag , config ) : <EOL> return self . _set_config ( distribution_id , etag , config ) <EOL> def create_streaming_distribution ( self , origin , enabled , <EOL> caller_reference = '<STR_LIT>' , <EOL> cnames = None , comment = '<STR_LIT>' , <EOL> trusted_signers = None ) : <EOL> config = StreamingDistributionConfig ( origin = origin , enabled = enabled , <EOL> caller_reference = caller_reference , <EOL> cnames = cnames , comment = comment , <EOL> trusted_signers = trusted_signers ) <EOL> return self . _create_object ( config , '<STR_LIT>' , <EOL> StreamingDistribution ) <EOL> def delete_streaming_distribution ( self , distribution_id , etag ) : <EOL> return self . _delete_object ( distribution_id , etag , <EOL> '<STR_LIT>' ) <EOL> def get_all_origin_access_identity ( self ) : <EOL> tags = [ ( '<STR_LIT>' , <EOL> OriginAccessIdentitySummary ) ] <EOL> return self . _get_all_objects ( '<STR_LIT>' , tags ) <EOL> def get_origin_access_identity_info ( self , access_id ) : <EOL> return self . _get_info ( access_id , '<STR_LIT>' , <EOL> OriginAccessIdentity ) <EOL> def get_origin_access_identity_config ( self , access_id ) : <EOL> return self . _get_config ( access_id , <EOL> '<STR_LIT>' , <EOL> OriginAccessIdentityConfig ) <EOL> def set_origin_access_identity_config ( self , access_id , <EOL> etag , config ) : <EOL> return self . _set_config ( access_id , etag , config ) <EOL> def create_origin_access_identity ( self , caller_reference = '<STR_LIT>' , comment = '<STR_LIT>' ) : <EOL> config = OriginAccessIdentityConfig ( caller_reference = caller_reference , <EOL> comment = comment ) <EOL> return self . _create_object ( config , '<STR_LIT>' , <EOL> OriginAccessIdentity ) <EOL> def delete_origin_access_identity ( self , access_id , etag ) : <EOL> return self . _delete_object ( access_id , etag , <EOL> '<STR_LIT>' ) <EOL> def create_invalidation_request ( self , distribution_id , paths , <EOL> caller_reference = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( paths , InvalidationBatch ) : <EOL> paths = InvalidationBatch ( paths ) <EOL> paths . connection = self <EOL> uri = '<STR_LIT>' % ( self . Version , <EOL> distribution_id ) <EOL> response = self . make_request ( '<STR_LIT:POST>' , uri , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , <EOL> data = paths . to_xml ( ) ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT> : <EOL> h = handler . XmlHandler ( paths , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return paths <EOL> else : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> def invalidation_request_status ( self , distribution_id , <EOL> request_id , caller_reference = None ) : <EOL> uri = '<STR_LIT>' % ( self . Version , <EOL> distribution_id , <EOL> request_id ) <EOL> response = self . make_request ( '<STR_LIT:GET>' , uri , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> : <EOL> paths = InvalidationBatch ( [ ] ) <EOL> h = handler . XmlHandler ( paths , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return paths <EOL> else : <EOL> raise CloudFrontServerError ( response . status , response . reason , body ) <EOL> def get_invalidation_requests ( self , distribution_id , marker = None , <EOL> max_items = None ) : <EOL> """<STR_LIT>""" <EOL> uri = '<STR_LIT>' % distribution_id <EOL> params = dict ( ) <EOL> if marker : <EOL> params [ '<STR_LIT>' ] = marker <EOL> if max_items : <EOL> params [ '<STR_LIT>' ] = max_items <EOL> if params : <EOL> uri += '<STR_LIT>' % params . popitem ( ) <EOL> for k , v in params . items ( ) : <EOL> uri += '<STR_LIT>' % ( k , v ) <EOL> tags = [ ( '<STR_LIT>' , InvalidationSummary ) ] <EOL> rs_class = InvalidationListResultSet <EOL> rs_kwargs = dict ( connection = self , distribution_id = distribution_id , <EOL> max_items = max_items , marker = marker ) <EOL> return self . _get_all_objects ( uri , tags , result_set_class = rs_class , <EOL> result_set_kwargs = rs_kwargs ) </s>
