text
stringlengths
1
93.6k
command = 'Sales.getReport, {0},Subscriber,Detailed,Daily,{1},{2}'.format(args.vendor, args.date, args.version)
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
def itc_get_newsstand_report(args):
command = 'Sales.getReport, {0},Newsstand,Detailed,{1},{2}'.format(args.vendor, args.datetype, args.date)
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
def itc_get_opt_in_report(args):
command = 'Sales.getReport, {0},Sales,Opt-In,Weekly,{1}'.format(args.vendor, args.date)
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command), False) # do not attempt to unzip because it's password protected
def itc_get_pre_order_report(args):
command = 'Sales.getReport, {0},Pre-Order,Summary,{1},{2}'.format(args.vendor, args.datetype, args.date)
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
def itc_get_podcasts_subscription_snapshot_report(args):
command = 'Sales.getReport, {0},apSubscriptionsSnapshot,Summary,Daily,{1}'.format(args.vendor, args.date)
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
def itc_view_token(args):
command = 'Sales.viewToken'
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
def itc_generate_token(args):
command = 'Sales.generateToken'
# generating a new token requires mirroring back a request id to the iTC server, so let's examine the response header...
_, header = post_request(ENDPOINT_SALES, get_credentials(args), command)
service_request_id = header.get('service_request_id')
# ...and post back the request id
result = post_request(ENDPOINT_SALES, get_credentials(args), command, "&isExistingToken=Y&requestId=" + service_request_id)
output_result(result)
# optionally store the new token in Keychain upon success
content, _ = result
if content and args.update_keychain_item:
# extract token for both operation modes (Robot.XML or Normal)
content = content.decode()
token = re.findall('<AccessToken>(.*?)</AccessToken>', content) or re.findall('AccessToken:(.*?)$', content, re.M)
if token:
token = token[0]
keychain.set_generic_password(None, args.update_keychain_item, '', token)
if not args.mode == 'Robot.XML': print("Keychain has been updated.")
def itc_delete_token(args):
command = 'Sales.deleteToken'
output_result(post_request(ENDPOINT_SALES, get_credentials(args), command))
# login credentials
def get_credentials(args):
"""Select App Store Connect login credentials depending on given command line arguments"""
# for most commands an App Store Connect access token is needed - fetched either from the command line or from Keychain...
access_token = keychain.find_generic_password(None, args.access_token_keychain_item, '') if args.access_token_keychain_item else args.access_token
# ...but commands for access token manipulation need the plaintext password of the App Store Connect account
password = keychain.find_generic_password(None, args.password_keychain_item, '') if args.password_keychain_item else args.password
return (args.userid, access_token, password, str(args.account), args.mode)
# HTTP request
def build_json_request_string(credentials, query):
"""Build a JSON string from the urlquoted credentials and the actual query input"""
userid, accessToken, password, account, mode = credentials
request = dict(userid=userid, version=VERSION, mode=mode, queryInput=query)
if account: request.update(account=account) # empty account info would result in error 404
if accessToken: request.update(accesstoken=accessToken)
if password: request.update(password=password)
request = dict(jsonRequest=json.dumps(request))
return urllib.parse.urlencode(request)
def post_request(endpoint, credentials, command, url_params = None):
"""Execute the HTTP POST request"""
command = "[p=Reporter.properties, %s]" % command
request_data = build_json_request_string(credentials, command)
if url_params: request_data += url_params
request = urllib.request.Request(endpoint, request_data.encode())
request.add_header('Accept', 'text/html,image/gif,image/jpeg; q=.2, */*; q=.2')
try:
response = urllib.request.urlopen(request)
content = response.read()
header = response.info()
return (content, header)
except urllib.error.HTTPError as e:
if e.code == 400 or e.code == 401 or e.code == 403 or e.code == 404:
# for these error codes, the body always contains an error message
raise ValueError(e.read().decode())
else:
raise ValueError("HTTP Error %s. Did you choose reasonable query arguments?" % str(e.code))