@@ -95,26 +95,30 @@ def merge_params_data_with_common_args(self, method, params, data, enable_app_se
95
95
data = json .dumps (data )
96
96
return params , data
97
97
98
- def _pre_process_headers (self , headers ):
98
+ def _pre_process_headers (self , method , headers , params , data ):
99
99
if self .use_test_env :
100
100
headers ["x-use-test-env" ] = "1"
101
101
if self .language :
102
102
headers ["blueking-language" ] = self .language
103
103
104
104
if "X-Bkapi-Authorization" not in headers :
105
- headers ["X-Bkapi-Authorization" ] = json .dumps (
106
- {
107
- "bk_app_code" : self .app_code ,
108
- "bk_app_secret" : self .app_secret ,
109
- ** {k : v for k , v in self .common_args .items () if v }, # filter item with empty value
110
- }
111
- )
105
+ auth_header = {
106
+ "bk_app_code" : self .app_code ,
107
+ "bk_app_secret" : self .app_secret ,
108
+ ** self .common_args ,
109
+ }
110
+ for auth_key in ["bk_username" , "bk_token" , "access_token" ]:
111
+ if method == "GET" and auth_key in (params or {}):
112
+ auth_header [auth_key ] = params [auth_key ]
113
+ if method == "POST" and auth_key in (data or {}):
114
+ auth_header [auth_key ] = data [auth_key ]
115
+ headers ["X-Bkapi-Authorization" ] = json .dumps (auth_header )
112
116
113
117
def request (self , method , url , params = None , data = None , ** kwargs ):
114
118
"""Send request"""
115
119
# determine whether access test environment of third-party system
116
120
headers = kwargs .pop ("headers" , {})
117
- self ._pre_process_headers (headers )
121
+ self ._pre_process_headers (method , headers , params , data )
118
122
params , data = self .merge_params_data_with_common_args (method , params , data , enable_app_secret = True )
119
123
logger .debug ("Calling %s %s with params=%s, data=%s, headers=%s" , method , url , params , data , headers )
120
124
return requests .request (method , url , params = params , data = data , verify = False , headers = headers , ** kwargs )
0 commit comments