@@ -26,65 +26,78 @@ def generateToken(self, issuer_id, key_id, private_key):
2626
2727 return encoded_token
2828
29- def uploadNotes (self , app_id , token , whats_new , build_number , platform ):
30- # Create Header
31- HEAD = {
32- 'Authorization' : 'Bearer ' + token
33- }
34-
35- # URLS
29+ def uploadNotes (self , app_id , token , whats_new , build_number , platform ):
30+ HEAD = {'Authorization' : f'Bearer { token } ' }
3631 BASE_URL = 'https://api.appstoreconnect.apple.com/v1/'
3732
38- # Find builds
39- versionId = ""
40- versionIdCounter = 0
41- while versionId == "" and versionIdCounter < 100 :
42-
43- print ("---Finding Build---" )
44- URL = BASE_URL + 'builds?filter[app]=' + app_id + '&filter[version]=' + build_number + '&filter[preReleaseVersion.platform]=' + platform
45- r = requests .get (URL , params = {}, headers = HEAD )
46- try :
47- data = r .json ()['data' ]
48-
49- if len (data ) > 0 :
50- versionId = data [0 ]['id' ]
51- print (f"found versionId: { versionId } " )
52-
53- except Exception as e :
54- print (f"Error: { e } " )
55- time .sleep (60 ) #wait for 60 seconds
56-
57- versionIdCounter += 1
58-
59- # Find localizations
60- localizationId = ""
61- localizationIdCounter = 0
62- while localizationId == "" and localizationIdCounter < 100 :
33+ # --- Find build ---
34+ versionId = None
35+ for attempt in range (10 ): # 10 tries max, not 100
36+ print (f"---Finding Build (Attempt { attempt + 1 } )---" )
37+ URL = f"{ BASE_URL } builds?filter[app]={ app_id } &filter[version]={ build_number } &filter[preReleaseVersion.platform]={ platform } "
38+ r = requests .get (URL , headers = HEAD )
39+
40+ if not r .ok :
41+ print (f"Error: { r .status_code } { r .reason } " )
42+ if r .status_code == 404 :
43+ break # stop early on invalid request
44+ time .sleep (10 )
45+ continue
46+
47+ data = r .json ().get ('data' , [])
48+ if data :
49+ versionId = data [0 ]['id' ]
50+ print (f"Found versionId: { versionId } " )
51+ break
52+
53+ print ("Build not found yet, retrying..." )
54+ time .sleep (10 )
6355
64- print ("---Finding Localizations---" )
65- URL = BASE_URL + 'builds/' + versionId + '/betaBuildLocalizations'
66- r = requests .get (URL , params = {}, headers = HEAD )
67- try :
68- localizationId = r .json ()['data' ][0 ]['id' ]
69- except Exception as e :
70- print (f"Error: { e } " )
71- time .sleep (60 ) #wait for 60 seconds
72-
73- localizationIdCounter += 1
56+ if not versionId :
57+ raise RuntimeError ("Failed to find versionId after 10 attempts." )
58+
59+ # --- Find localizations ---
60+ localizationId = None
61+ for attempt in range (10 ):
62+ print (f"---Finding Localization (Attempt { attempt + 1 } )---" )
63+ URL = f"{ BASE_URL } builds/{ versionId } /betaBuildLocalizations"
64+ r = requests .get (URL , headers = HEAD )
65+
66+ if not r .ok :
67+ print (f"Error: { r .status_code } { r .reason } " )
68+ time .sleep (10 )
69+ continue
70+
71+ data = r .json ().get ('data' , [])
72+ if data :
73+ localizationId = data [0 ]['id' ]
74+ print (f"Found localizationId: { localizationId } " )
75+ break
76+
77+ print ("No localization found yet, retrying..." )
78+ time .sleep (10 )
7479
75- print ("---Update What's New---" )
76- URL = BASE_URL + 'betaBuildLocalizations/' + localizationId
77- data = {
80+ if not localizationId :
81+ raise RuntimeError ("Failed to find localizationId after 10 attempts." )
82+
83+ # --- Update What's New ---
84+ print ("---Updating What's New---" )
85+ URL = f"{ BASE_URL } betaBuildLocalizations/{ localizationId } "
86+ payload = {
7887 "data" : {
7988 "id" : localizationId ,
8089 "type" : "betaBuildLocalizations" ,
81- "attributes" : {
82- "whatsNew" : whats_new [:4000 ] #4000 char limit
83- }
90+ "attributes" : {"whatsNew" : whats_new [:4000 ]},
8491 }
8592 }
86- result = requests .patch (URL , json = data , headers = HEAD )
87- return result .reason
93+
94+ r = requests .patch (URL , json = payload , headers = HEAD )
95+
96+ if not r .ok :
97+ raise RuntimeError (f"Failed to update What's New: { r .status_code } { r .text } " )
98+
99+ print ("Successfully updated What's New." )
100+ return r .reason
88101
89102
90103def main ():
0 commit comments