<s> import json <EOL> import requests <EOL> from . import receipt <EOL> from . import exceptions <EOL> from . environment import Environment <EOL> RECEIPT_PRODUCTION_VALIDATION_URL = "<STR_LIT>" <EOL> RECEIPT_SANDBOX_VALIDATION_URL = "<STR_LIT>" <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , receipt_data , password = None , proxy_url = None ) : <EOL> self . receipt_data = receipt_data <EOL> self . password = password <EOL> self . proxy_url = proxy_url <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . receipt_data [ : <NUM_LIT:20> ] ) <EOL> @ property <EOL> def request_content ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . password is not None : <EOL> request_content = { '<STR_LIT>' : self . receipt_data , '<STR_LIT:password>' : self . password } <EOL> else : <EOL> request_content = { '<STR_LIT>' : self . receipt_data } <EOL> return request_content <EOL> def verify_from ( self , url , verify_ssl = True ) : <EOL> """<STR_LIT>""" <EOL> post_body = json . dumps ( self . request_content ) <EOL> try : <EOL> if self . proxy_url : <EOL> protocol = self . proxy_url . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> http_response = requests . post ( url , post_body , verify = verify_ssl , proxies = { protocol : self . proxy_url } ) <EOL> else : <EOL> http_response = requests . post ( url , post_body , verify = verify_ssl ) <EOL> except requests . exceptions . RequestException as e : <EOL> raise exceptions . ItunesServerNotReachable ( exc = e ) <EOL> if http_response . status_code != <NUM_LIT:200> : <EOL> raise exceptions . ItunesServerNotAvailable ( http_response . status_code , http_response . content ) <EOL> response = receipt . Response ( json . loads ( http_response . content . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> if response . status != <NUM_LIT:0> : <EOL> raise exceptions . InvalidReceipt ( response . status , response = response ) <EOL> return response <EOL> def verify ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> env = Environment . current ( ) <EOL> use_production = options . get ( '<STR_LIT>' , env . use_production ) <EOL> use_sandbox = options . get ( '<STR_LIT>' , env . use_sandbox ) <EOL> verify_ssl = options . get ( '<STR_LIT>' , env . verify_ssl ) <EOL> assert ( env . use_production or env . use_sandbox ) <EOL> response = None <EOL> if use_production : <EOL> try : <EOL> response = self . verify_from ( RECEIPT_PRODUCTION_VALIDATION_URL , verify_ssl ) <EOL> except exceptions . InvalidReceipt : <EOL> if not use_sandbox : <EOL> raise <EOL> if not response and use_sandbox : <EOL> try : <EOL> response = self . verify_from ( RECEIPT_SANDBOX_VALIDATION_URL , verify_ssl ) <EOL> except exceptions . InvalidReceipt : <EOL> raise <EOL> return response </s>
