feat: 支持x-login-as三参数认证、auth_url跟随base_url
- 认证请求增加x-login-as header支持连接账户 - auth_url根据base_url动态构建,不再硬编码生产环境 - 默认Base URL改为demo环境 - 设置页面新增Account ID字段,带tooltip说明
This commit is contained in:
@@ -2,16 +2,20 @@
|
||||
import logging
|
||||
import httpx
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional
|
||||
from airwallex.client import AirwallexClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProxiedAirwallexClient(AirwallexClient):
|
||||
"""AirwallexClient that routes requests through an HTTP or SOCKS5 proxy."""
|
||||
"""AirwallexClient that routes requests through an HTTP or SOCKS5 proxy.
|
||||
Also supports x-login-as header for connected accounts.
|
||||
"""
|
||||
|
||||
def __init__(self, proxy_url: str | None = None, **kwargs):
|
||||
def __init__(self, proxy_url: str | None = None, login_as: str | None = None, **kwargs):
|
||||
self._proxy_url = proxy_url
|
||||
self._login_as = login_as
|
||||
super().__init__(**kwargs)
|
||||
# Replace the default httpx client with a proxied one
|
||||
if proxy_url:
|
||||
@@ -27,26 +31,30 @@ class ProxiedAirwallexClient(AirwallexClient):
|
||||
logger.info("Airwallex client initialized without proxy")
|
||||
|
||||
def authenticate(self) -> None:
|
||||
"""Override authenticate to use proxy for auth requests too."""
|
||||
"""Override authenticate to use proxy and x-login-as for auth requests."""
|
||||
if self._token and self._token_expiry and datetime.now(timezone.utc) < self._token_expiry:
|
||||
return
|
||||
|
||||
logger.info("Authenticating with Airwallex API at %s (proxy: %s)",
|
||||
self.auth_url, bool(self._proxy_url))
|
||||
logger.info("Authenticating with Airwallex API at %s (proxy: %s, login_as: %s)",
|
||||
self.auth_url, bool(self._proxy_url), self._login_as or "none")
|
||||
|
||||
auth_kwargs: dict = {"timeout": self.request_timeout}
|
||||
if self._proxy_url:
|
||||
auth_kwargs["proxy"] = self._proxy_url
|
||||
|
||||
auth_headers = {
|
||||
"Content-Type": "application/json",
|
||||
"x-client-id": self.client_id,
|
||||
"x-api-key": self.api_key,
|
||||
}
|
||||
if self._login_as:
|
||||
auth_headers["x-login-as"] = self._login_as
|
||||
|
||||
auth_client = httpx.Client(**auth_kwargs)
|
||||
try:
|
||||
response = auth_client.post(
|
||||
self.auth_url,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"x-client-id": self.client_id,
|
||||
"x-api-key": self.api_key,
|
||||
},
|
||||
headers=auth_headers,
|
||||
content="{}",
|
||||
)
|
||||
|
||||
@@ -61,3 +69,11 @@ class ProxiedAirwallexClient(AirwallexClient):
|
||||
logger.info("Authentication successful, token expires in 28 minutes")
|
||||
finally:
|
||||
auth_client.close()
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
"""Add x-login-as to all API request headers."""
|
||||
h = super().headers
|
||||
if self._login_as:
|
||||
h["x-login-as"] = self._login_as
|
||||
return h
|
||||
|
||||
Reference in New Issue
Block a user