|
11 | 11 | from common.error import SingleMessageError |
12 | 12 |
|
13 | 13 |
|
| 14 | +def get_es_auth(conf: DatasourceConf): |
| 15 | + username = f"{conf.username}" |
| 16 | + password = f"{conf.password}" |
| 17 | + |
| 18 | + credentials = f"{username}:{password}" |
| 19 | + encoded_credentials = b64encode(credentials.encode()).decode() |
| 20 | + |
| 21 | + return { |
| 22 | + "Content-Type": "application/json", |
| 23 | + "Authorization": f"Basic {encoded_credentials}" |
| 24 | + } |
| 25 | + |
| 26 | + |
14 | 27 | def get_es_connect(conf: DatasourceConf): |
15 | 28 | es_client = Elasticsearch( |
16 | 29 | [conf.host], # ES address |
17 | 30 | basic_auth=(conf.username, conf.password), |
18 | 31 | verify_certs=False, |
19 | | - compatibility_mode=True |
| 32 | + compatibility_mode=True, |
| 33 | + headers=get_es_auth(conf) |
20 | 34 | ) |
21 | 35 | return es_client |
22 | 36 |
|
@@ -95,18 +109,8 @@ def get_es_data_by_http(conf: DatasourceConf, sql: str): |
95 | 109 | url = url[:-1] |
96 | 110 |
|
97 | 111 | host = f'{url}/_sql?format=json' |
98 | | - username = f"{conf.username}" |
99 | | - password = f"{conf.password}" |
100 | | - |
101 | | - credentials = f"{username}:{password}" |
102 | | - encoded_credentials = b64encode(credentials.encode()).decode() |
103 | | - |
104 | | - headers = { |
105 | | - "Content-Type": "application/json", |
106 | | - "Authorization": f"Basic {encoded_credentials}" |
107 | | - } |
108 | 112 |
|
109 | | - response = requests.post(host, data=json.dumps({"query": sql}), headers=headers, verify=False) |
| 113 | + response = requests.post(host, data=json.dumps({"query": sql}), headers=get_es_auth(conf), verify=False) |
110 | 114 |
|
111 | 115 | # print(response.json()) |
112 | 116 | res = response.json() |
|
0 commit comments