Setup & Helpers
Python environment setup and shared REST and WebSocket helpers
Requirements
pip install requests websockets cryptographyAuthenticated REST client
import hmac, hashlib, base64, time, json, requests
SERVICE_MAP = {
"/api/events": "https://event-service.outpoll.com",
"/api/categories": "https://event-service.outpoll.com",
"/api/tags": "https://event-service.outpoll.com",
"/api/coins": "https://wallet-mutator-view.outpoll.com",
"/api/user-balances": "https://wallet-mutator-view.outpoll.com",
"/api/history": "https://history-service.outpoll.com",
"/orders": "https://order-service.outpoll.com",
"/auth": "https://auth-service.outpoll.com",
}
class OutpollClient:
def __init__(self, api_key: str, api_secret: str):
self.api_key = api_key
self.api_secret = api_secret
def _host(self, path: str) -> str:
for prefix, host in SERVICE_MAP.items():
if path.startswith(prefix):
return host
return "https://event-service.outpoll.com"
def _sign(self, method: str, path: str, body: str = "") -> dict:
timestamp = str(int(time.time()))
message = timestamp + method + path + body
padded = self.api_secret + "=" * (4 - len(self.api_secret) % 4) if len(self.api_secret) % 4 else self.api_secret
secret_bytes = base64.urlsafe_b64decode(padded)
signature = base64.urlsafe_b64encode(
hmac.new(secret_bytes, message.encode(), hashlib.sha256).digest()
).decode().rstrip("=")
return {
"OUTPOLL-API-KEY": self.api_key,
"OUTPOLL-API-SIGNATURE": signature,
"OUTPOLL-API-TIMESTAMP": timestamp,
"Content-Type": "application/json",
}
def get(self, path: str, params: dict = None):
headers = self._sign("GET", path)
return requests.get(self._host(path) + path, headers=headers, params=params)
def post(self, path: str, data: dict = None):
body = json.dumps(data) if data else ""
headers = self._sign("POST", path, body)
return requests.post(self._host(path) + path, headers=headers, data=body)
def delete(self, path: str):
headers = self._sign("DELETE", path)
return requests.delete(self._host(path) + path, headers=headers)WebSocket auth helper
Related pages
Last updated
Was this helpful?

