Stage 03.4 - private auth skeleton
This commit is contained in:
25
app/src/integrations/exchange/auth.py
Normal file
25
app/src/integrations/exchange/auth.py
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
import hmac
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
|
||||
class ExchangeAuth:
|
||||
def __init__(self, api_key: str, api_secret: str):
|
||||
self.api_key = api_key
|
||||
self.api_secret = api_secret.encode()
|
||||
|
||||
def sign(self, query: str) -> str:
|
||||
return hmac.new(self.api_secret, query.encode(), hashlib.sha256).hexdigest()
|
||||
|
||||
def build_headers(self):
|
||||
return {
|
||||
"X-MBX-APIKEY": self.api_key,
|
||||
}
|
||||
|
||||
def build_signed_params(self, params: dict):
|
||||
params["timestamp"] = int(time.time() * 1000)
|
||||
query = "&".join(f"{k}={v}" for k, v in params.items())
|
||||
signature = self.sign(query)
|
||||
params["signature"] = signature
|
||||
return params
|
||||
24
app/src/integrations/exchange/private_client.py
Normal file
24
app/src/integrations/exchange/private_client.py
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
from src.core.config import load_settings
|
||||
from src.integrations.exchange.rest_client import ExchangeRestClient
|
||||
from src.integrations.exchange.auth import ExchangeAuth
|
||||
|
||||
|
||||
class ExchangePrivateClient:
|
||||
def __init__(self):
|
||||
settings = load_settings()
|
||||
self.client = ExchangeRestClient()
|
||||
self.auth = ExchangeAuth(
|
||||
api_key=settings.exchange_api_key,
|
||||
api_secret=settings.exchange_api_secret,
|
||||
)
|
||||
|
||||
def get_account_info(self):
|
||||
params = {}
|
||||
signed = self.auth.build_signed_params(params)
|
||||
|
||||
return self.client.get_json(
|
||||
"/api/v2/account",
|
||||
params=signed,
|
||||
headers=self.auth.build_headers(),
|
||||
)
|
||||
Reference in New Issue
Block a user