正在显示
1 个修改的文件
包含
197 行增加
和
197 行删除
1 | -import requests | |
2 | -import time | |
3 | -import oss2 | |
4 | -from common.log import * | |
5 | - | |
6 | -logger = BotLogger(os.path.basename(__file__), logname=1).getLogger() | |
7 | - | |
8 | - | |
9 | -def handle_error(res, normal_status_code): | |
10 | - """ | |
11 | - 统一处理workai平台返回的error | |
12 | - :param res: 调用接口的返回 | |
13 | - :param normal_status_code: 正常返回的status code | |
14 | - :return: 返回错误信息 | |
15 | - """ | |
16 | - if res is None or res.status_code == normal_status_code: | |
17 | - return '' | |
18 | - | |
19 | - logger.info('The res is %s' % res) | |
20 | - err = '网络或服务器繁忙!' | |
21 | - try: | |
22 | - if 'message' in res.json(): | |
23 | - err = res.json()['message'] | |
24 | - except Exception as e: | |
25 | - logger.error(str(e)) | |
26 | - | |
27 | - return err | |
28 | - | |
29 | - | |
30 | -def get_token_by_password(username, password, tenant_id, tenant_type): | |
31 | - """ | |
32 | - 密码方式获取access_token | |
33 | - :param username: 系统管理员用户 | |
34 | - :param password: 系统管理员密码 | |
35 | - :param tenant_id: 客户在workai云平台唯一表示 | |
36 | - :return: 返回access_token, refresh_token 以及error, access_token用于访问接口的令牌, refresh_token | |
37 | - 用于刷新获取新的access_token, error为返回的错误信息 | |
38 | - """ | |
39 | - data = { | |
40 | - 'grant_type': 'password', | |
41 | - 'username': username, | |
42 | - 'password': password, | |
43 | - 'scope': 'global_access:tenant_admin,tenant:%s,tenant_type:%s' % (tenant_id, tenant_type), | |
44 | - } | |
45 | - | |
46 | - path = '%s/uaa/v1/auth/tokens' % WACLOUD_API_ENDPOINT | |
47 | - try: | |
48 | - res = requests.post(path, json=data, timeout=10) | |
49 | - err = handle_error(res, 200) | |
50 | - if err != '': | |
51 | - logger.error('Could not get the token by password for user: %s, tenant: %s' % (username, tenant_id)) | |
52 | - return '', '', err, None | |
53 | - | |
54 | - ret = res.json() | |
55 | - company, username, play_type = '', '', '' | |
56 | - if tenant_type == '0': | |
57 | - play_type = 'HRO' | |
58 | - elif tenant_type == '1': | |
59 | - play_type = 'HR' | |
60 | - | |
61 | - try: | |
62 | - company = ret['tenant']['name'] | |
63 | - username = ret['user']['name'] | |
64 | - except Exception as e: | |
65 | - logger.error(str(e)) | |
66 | - | |
67 | - return ret['access_token'], ret['refresh_token'], '', (company, username, play_type) | |
68 | - except Exception as e: | |
69 | - logger.error(str(e)) | |
70 | - return '', '', '网络或服务器异常,请稍后再试!', None | |
71 | - | |
72 | - | |
73 | -def refresh_token(rtoken, tenant_id): | |
74 | - """ | |
75 | - 刷新token方式获取access_token | |
76 | - :return: 返回access_token, refresh_token 以及error, access_token用于访问接口的令牌, refresh_token | |
77 | - 用于刷新获取新的access_token, error为返回的错误信息 | |
78 | - """ | |
79 | - data = { | |
80 | - 'grant_type': 'refresh_token', | |
81 | - 'refresh_token': rtoken, | |
82 | - 'scope': 'global_access:tenant_admin,tenant:%s' % tenant_id, | |
83 | - } | |
84 | - | |
85 | - path = '%s/uaa/v1/auth/tokens' % WACLOUD_API_ENDPOINT | |
86 | - res = requests.post(path, json=data) | |
87 | - | |
88 | - err = handle_error(res, 200) | |
89 | - if err != '': | |
90 | - logger.error('Could not get the token by refresh token for tenant: %s' % (tenant_id,)) | |
91 | - return '', '', err | |
92 | - | |
93 | - ret = res.json() | |
94 | - return ret['access_token'], ret['refresh_token'], '' | |
95 | - | |
96 | - | |
97 | -def upload_file(ctx, local_path, tenant_type, file_name): | |
98 | - """上传附件""" | |
99 | - object_path = "" | |
100 | - if ctx is None or local_path is None: | |
101 | - return 401, object_path | |
102 | - | |
103 | - headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
104 | - path = '%s/filemeta/v2/inits' % (WACLOUD_API_ENDPOINT,) | |
105 | - if tenant_type == "0": | |
106 | - data = {"object_type": "hro", "access_type": "sdk"} | |
107 | - elif tenant_type == "1": | |
108 | - data = {"object_type": "hr", "access_type": "sdk"} | |
109 | - else: | |
110 | - return 401, '配置文件设置tenant_type的值有误' | |
111 | - | |
112 | - res = None | |
113 | - try: | |
114 | - res = requests.post(path, json=data, headers=headers, timeout=120) | |
115 | - if res.status_code == 401: | |
116 | - access_token, _refresh_token, err = refresh_token(ctx['refresh_token'], ctx['tenant_id']) | |
117 | - if err != '': | |
118 | - return 401, object_path | |
119 | - ctx['access_token'] = access_token | |
120 | - ctx['refresh_token'] = _refresh_token | |
121 | - headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
122 | - res = requests.post(path, json=data, headers=headers, timeout=120) | |
123 | - | |
124 | - err = handle_error(res, 200) | |
125 | - if err != '': | |
126 | - return res.status_code, object_path | |
127 | - except Exception as e: | |
128 | - logger.error(str(e)) | |
129 | - return 401, str(e) | |
130 | - | |
131 | - try: | |
132 | - datas = res.json() | |
133 | - # 使用临时token中的认证信息初始化StsAuth实例。 | |
134 | - # auth = oss2.Auth(datas['access_key_id'], datas['access_key_secret']) | |
135 | - auth = oss2.StsAuth(datas['access_key_id'], datas['access_key_secret'], datas['security_token']) | |
136 | - # 使用StsAuth实例初始化存储空间。 | |
137 | - bucket = oss2.Bucket(auth, datas['domain'], datas['bucket']) | |
138 | - object_path = datas['object_path'] + "/" + file_name | |
139 | - result = bucket.put_object_from_file(object_path, local_path) | |
140 | - return result.status, object_path | |
141 | - except Exception as e: | |
142 | - logger.error(str(e)) | |
143 | - return 403, str(e) | |
144 | - | |
145 | - | |
146 | -def update_object_path(ctx, tenant_type, object_path, types, file_name): | |
147 | - """上传文件路径到saas平台""" | |
148 | - if tenant_type == "0" or tenant_type == "1": | |
149 | - path = '%s/payroll/v1/do-import-data?type=%s' % (WACLOUD_API_ENDPOINT, types) | |
150 | - else: | |
151 | - return False, '平台类型设置有误' | |
152 | - | |
153 | - headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
154 | - bucket = '/hro/' | |
155 | - if 'hx' in WACLOUD_API_ENDPOINT: | |
156 | - bucket = '/hxhro/' | |
157 | - elif "xsb" in WACLOUD_API_ENDPOINT: | |
158 | - bucket = "/xsbhro/" | |
159 | - elif "engma" in WACLOUD_API_ENDPOINT: | |
160 | - bucket = "/engmahro/" | |
161 | - elif "anbo" in WACLOUD_API_ENDPOINT: | |
162 | - bucket = "/anbohro/" | |
163 | - elif "teda" in WACLOUD_API_ENDPOINT: | |
164 | - bucket = "/tedahro/" | |
165 | - elif "hwapi" in WACLOUD_API_ENDPOINT: | |
166 | - bucket = "/hro/" | |
167 | - elif "jincao" in WACLOUD_API_ENDPOINT: | |
168 | - bucket = "/jchro/" | |
169 | - elif 'jincao' in WACLOUD_API_ENDPOINT: | |
170 | - bucket = '/jchro/' | |
171 | - elif 'bhgn' in WACLOUD_API_ENDPOINT: | |
172 | - bucket = '/bhgnhro/' | |
173 | - elif 'gongyuanhr' in WACLOUD_API_ENDPOINT: | |
174 | - bucket = '/szgyhro/' | |
175 | - | |
176 | - | |
177 | - object_path = bucket + object_path | |
178 | - data = { | |
179 | - "object_path": object_path, | |
180 | - "file_name": file_name | |
181 | - } | |
182 | - | |
183 | - try: | |
184 | - res = requests.post(path, json=data, headers=headers, timeout=120) | |
185 | - if res.status_code == 401: | |
186 | - time.sleep(2) | |
187 | - res = requests.post(path, json=data, headers=headers, timeout=120) | |
188 | - | |
189 | - err = handle_error(res, 200) | |
190 | - if err != '': | |
191 | - logger.error("上传失败:{file}, 原因:{reason}".format(file=object_path, reason=err)) | |
192 | - if 'Internal Server Error' in err: | |
193 | - err = '数据助手sheet附表内容错误' | |
194 | - return False, err | |
195 | - return True, '任务办理成功' | |
196 | - except Exception as e: | |
197 | - logger.error(str(e)) | |
1 | +import requests | |
2 | +import time | |
3 | +import oss2 | |
4 | +from common.log import * | |
5 | + | |
6 | +logger = BotLogger(os.path.basename(__file__), logname=1).getLogger() | |
7 | + | |
8 | + | |
9 | +def handle_error(res, normal_status_code): | |
10 | + """ | |
11 | + 统一处理workai平台返回的error | |
12 | + :param res: 调用接口的返回 | |
13 | + :param normal_status_code: 正常返回的status code | |
14 | + :return: 返回错误信息 | |
15 | + """ | |
16 | + if res is None or res.status_code == normal_status_code: | |
17 | + return '' | |
18 | + | |
19 | + logger.info('The res is %s' % res) | |
20 | + err = '网络或服务器繁忙!' | |
21 | + try: | |
22 | + if 'message' in res.json(): | |
23 | + err = res.json()['message'] | |
24 | + except Exception as e: | |
25 | + logger.error(str(e)) | |
26 | + | |
27 | + return err | |
28 | + | |
29 | + | |
30 | +def get_token_by_password(username, password, tenant_id, tenant_type): | |
31 | + """ | |
32 | + 密码方式获取access_token | |
33 | + :param username: 系统管理员用户 | |
34 | + :param password: 系统管理员密码 | |
35 | + :param tenant_id: 客户在workai云平台唯一表示 | |
36 | + :return: 返回access_token, refresh_token 以及error, access_token用于访问接口的令牌, refresh_token | |
37 | + 用于刷新获取新的access_token, error为返回的错误信息 | |
38 | + """ | |
39 | + data = { | |
40 | + 'grant_type': 'password', | |
41 | + 'username': username, | |
42 | + 'password': password, | |
43 | + 'scope': 'global_access:tenant_admin,tenant:%s,tenant_type:%s' % (tenant_id, tenant_type), | |
44 | + } | |
45 | + | |
46 | + path = '%s/uaa/v1/auth/tokens' % WACLOUD_API_ENDPOINT | |
47 | + try: | |
48 | + res = requests.post(path, json=data, timeout=10) | |
49 | + err = handle_error(res, 200) | |
50 | + if err != '': | |
51 | + logger.error('Could not get the token by password for user: %s, tenant: %s' % (username, tenant_id)) | |
52 | + return '', '', err, None | |
53 | + | |
54 | + ret = res.json() | |
55 | + company, username, play_type = '', '', '' | |
56 | + if tenant_type == '0': | |
57 | + play_type = 'HRO' | |
58 | + elif tenant_type == '1': | |
59 | + play_type = 'HR' | |
60 | + | |
61 | + try: | |
62 | + company = ret['tenant']['name'] | |
63 | + username = ret['user']['name'] | |
64 | + except Exception as e: | |
65 | + logger.error(str(e)) | |
66 | + | |
67 | + return ret['access_token'], ret['refresh_token'], '', (company, username, play_type) | |
68 | + except Exception as e: | |
69 | + logger.error(str(e)) | |
70 | + return '', '', '网络或服务器异常,请稍后再试!', None | |
71 | + | |
72 | + | |
73 | +def refresh_token(rtoken, tenant_id): | |
74 | + """ | |
75 | + 刷新token方式获取access_token | |
76 | + :return: 返回access_token, refresh_token 以及error, access_token用于访问接口的令牌, refresh_token | |
77 | + 用于刷新获取新的access_token, error为返回的错误信息 | |
78 | + """ | |
79 | + data = { | |
80 | + 'grant_type': 'refresh_token', | |
81 | + 'refresh_token': rtoken, | |
82 | + 'scope': 'global_access:tenant_admin,tenant:%s' % tenant_id, | |
83 | + } | |
84 | + | |
85 | + path = '%s/uaa/v1/auth/tokens' % WACLOUD_API_ENDPOINT | |
86 | + res = requests.post(path, json=data) | |
87 | + | |
88 | + err = handle_error(res, 200) | |
89 | + if err != '': | |
90 | + logger.error('Could not get the token by refresh token for tenant: %s' % (tenant_id,)) | |
91 | + return '', '', err | |
92 | + | |
93 | + ret = res.json() | |
94 | + return ret['access_token'], ret['refresh_token'], '' | |
95 | + | |
96 | + | |
97 | +def upload_file(ctx, local_path, tenant_type, file_name): | |
98 | + """上传附件""" | |
99 | + object_path = "" | |
100 | + if ctx is None or local_path is None: | |
101 | + return 401, object_path | |
102 | + | |
103 | + headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
104 | + path = '%s/filemeta/v2/inits' % (WACLOUD_API_ENDPOINT,) | |
105 | + if tenant_type == "0": | |
106 | + data = {"object_type": "hro", "access_type": "sdk"} | |
107 | + elif tenant_type == "1": | |
108 | + data = {"object_type": "hr", "access_type": "sdk"} | |
109 | + else: | |
110 | + return 401, '配置文件设置tenant_type的值有误' | |
111 | + | |
112 | + res = None | |
113 | + try: | |
114 | + res = requests.post(path, json=data, headers=headers, timeout=120) | |
115 | + if res.status_code == 401: | |
116 | + access_token, _refresh_token, err = refresh_token(ctx['refresh_token'], ctx['tenant_id']) | |
117 | + if err != '': | |
118 | + return 401, object_path | |
119 | + ctx['access_token'] = access_token | |
120 | + ctx['refresh_token'] = _refresh_token | |
121 | + headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
122 | + res = requests.post(path, json=data, headers=headers, timeout=120) | |
123 | + | |
124 | + err = handle_error(res, 200) | |
125 | + if err != '': | |
126 | + return res.status_code, object_path | |
127 | + except Exception as e: | |
128 | + logger.error(str(e)) | |
129 | + return 401, str(e) | |
130 | + | |
131 | + try: | |
132 | + datas = res.json() | |
133 | + # 使用临时token中的认证信息初始化StsAuth实例。 | |
134 | + # auth = oss2.Auth(datas['access_key_id'], datas['access_key_secret']) | |
135 | + auth = oss2.StsAuth(datas['access_key_id'], datas['access_key_secret'], datas['security_token']) | |
136 | + # 使用StsAuth实例初始化存储空间。 | |
137 | + bucket = oss2.Bucket(auth, datas['domain'], datas['bucket']) | |
138 | + object_path = datas['object_path'] + "/" + file_name | |
139 | + result = bucket.put_object_from_file(object_path, local_path) | |
140 | + return result.status, object_path | |
141 | + except Exception as e: | |
142 | + logger.error(str(e)) | |
143 | + return 403, str(e) | |
144 | + | |
145 | + | |
146 | +def update_object_path(ctx, tenant_type, object_path, types, file_name): | |
147 | + """上传文件路径到saas平台""" | |
148 | + if tenant_type == "0" or tenant_type == "1": | |
149 | + path = '%s/payroll/v1/do-import-data?type=%s' % (WACLOUD_API_ENDPOINT, types) | |
150 | + else: | |
151 | + return False, '平台类型设置有误' | |
152 | + | |
153 | + headers = {'Authorization': 'Bearer %s' % ctx['access_token']} | |
154 | + bucket = '/hro/' | |
155 | + if 'hx' in WACLOUD_API_ENDPOINT: | |
156 | + bucket = '/new-hxhro/' | |
157 | + elif "xsb" in WACLOUD_API_ENDPOINT: | |
158 | + bucket = "/xsbhro/" | |
159 | + elif "engma" in WACLOUD_API_ENDPOINT: | |
160 | + bucket = "/engmahro/" | |
161 | + elif "anbo" in WACLOUD_API_ENDPOINT: | |
162 | + bucket = "/anbohro/" | |
163 | + elif "teda" in WACLOUD_API_ENDPOINT: | |
164 | + bucket = "/tedahro/" | |
165 | + elif "hwapi" in WACLOUD_API_ENDPOINT: | |
166 | + bucket = "/hro/" | |
167 | + elif "jincao" in WACLOUD_API_ENDPOINT: | |
168 | + bucket = "/jchro/" | |
169 | + elif 'jincao' in WACLOUD_API_ENDPOINT: | |
170 | + bucket = '/jchro/' | |
171 | + elif 'bhgn' in WACLOUD_API_ENDPOINT: | |
172 | + bucket = '/bhgnhro/' | |
173 | + elif 'gongyuanhr' in WACLOUD_API_ENDPOINT: | |
174 | + bucket = '/szgyhro/' | |
175 | + | |
176 | + | |
177 | + object_path = bucket + object_path | |
178 | + data = { | |
179 | + "object_path": object_path, | |
180 | + "file_name": file_name | |
181 | + } | |
182 | + | |
183 | + try: | |
184 | + res = requests.post(path, json=data, headers=headers, timeout=120) | |
185 | + if res.status_code == 401: | |
186 | + time.sleep(2) | |
187 | + res = requests.post(path, json=data, headers=headers, timeout=120) | |
188 | + | |
189 | + err = handle_error(res, 200) | |
190 | + if err != '': | |
191 | + logger.error("上传失败:{file}, 原因:{reason}".format(file=object_path, reason=err)) | |
192 | + if 'Internal Server Error' in err: | |
193 | + err = '数据助手sheet附表内容错误' | |
194 | + return False, err | |
195 | + return True, '任务办理成功' | |
196 | + except Exception as e: | |
197 | + logger.error(str(e)) | |
198 | 198 | return False, '请求服务器异常:{}'.format(str(e)) |
\ No newline at end of file | ... | ... |
请
注册
或
登录
后发表评论