-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_script.py
108 lines (97 loc) · 3.59 KB
/
my_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import requests
import sys
def get_token(client_id, client_secret):
print 'Get Token...'
url = 'https://connect-api.cloud.huawei.com/api/oauth2/v1/token'
body = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
response = requests.post(url, json=body)
if response.status_code == 200:
json = response.json()
return json['access_token']
else:
print('token: ' + str(response.status_code) + ': ' + response.reason)
def get_upload_url(access_token, client_id, app_id):
print 'Get Upload URL...'
url = 'https://connect-api.cloud.huawei.com/api/publish/v2/upload-url'
params = {
'appId': app_id,
'suffix': 'apk'
}
headers = {
'client_id': client_id,
'Authorization': 'Bearer ' + access_token
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
json = response.json()
return json['uploadUrl'], json['authCode']
else:
print('upload-url: ' + str(response.status_code) + ': ' + response.reason)
def upload_file(upload_url, auth_code, path_file, access_token, client_id, app_id):
print 'Upload File...'
headers = {
"accept": "application/json"
}
body = {
'authCode': auth_code,
'fileCount': '1'
}
with open(path_file, 'rb') as f:
response = requests.post(upload_url, files={'file_name': f}, data=body, headers=headers)
if response.status_code == 200:
json = response.json()
fileInfoList = json['result']['UploadFileRsp']['fileInfoList'][0]
update_app_file_info(file_url=fileInfoList['fileDestUlr'],
file_size=fileInfoList['size'],
client_id=client_id,
access_token=access_token,
app_id=app_id)
else:
print('upload-file: ' + str(response.status_code) + ': ' + response.reason)
def update_app_file_info(file_url, file_size, client_id, access_token, app_id):
print 'Update App File Info...'
url = 'https://connect-api.cloud.huawei.com/api/publish/v2/app-file-info'
headers = {
'client_id': client_id,
'Authorization': 'Bearer ' + access_token
}
body = {
'fileType': 5,
'files': [{
'fileName': 'new_app_release.apk',
'fileDestUrl': file_url,
'size': file_size
}]
}
params = {
'appId': app_id
}
response = requests.put(url, headers=headers, json=body, params=params)
if response.status_code == 200:
json = response.json()
pkgVersion = json['pkgVersion'][0]
msg = json['ret']['msg']
code = json['ret']['code']
print str(pkgVersion) + ', ' + msg + ', ' + str(code)
else:
print('app-file-info: ' + str(response.status_code) + ': ' + response.reason)
app_id = str(sys.argv[1])
client_id = str(sys.argv[2])
client_secret = str(sys.argv[3])
path_file = str(sys.argv[4])
print app_id, client_id, client_secret, path_file
access_token = get_token(client_id=client_id,
client_secret=client_secret)
upload_url, auth_code = get_upload_url(access_token=access_token,
client_id=client_id,
app_id=app_id)
upload_file(upload_url=upload_url,
auth_code=auth_code,
path_file=path_file,
access_token=access_token,
client_id=client_id,
app_id=app_id)