Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApiError(Exception):
- def __init__(self, request, response):
- super().__init__(f"{repr(request)} -> {repr(response)}")
- self.request = request
- self.response = response
- class Api:
- API_URL = "api.exmo.com"
- API_VERSION = "v1"
- REQUESTS_PER_MINUTE = 180
- def __init__(self, key, secret, timeout):
- self._key = key
- self._secret = secret.encode("utf-8")
- self._timeout = timeout
- self._last_call = None
- def _sha512(self, data):
- h = hmac.new(key=self._secret, digestmod=hashlib.sha512)
- h.update(data.encode("utf-8"))
- return h.hexdigest()
- def __getattr__(self, name):
- return functools.partial(self._call, name)
- def _call(self, name, **params):
- now = time.monotonic()
- if self._last_call:
- delta = 60 / self.REQUESTS_PER_MINUTE + self._last_call - now
- time.sleep(max(0, delta))
- self._last_call = now
- params["nonce"] = int(round(time.time() * 1000))
- encoded_params = urllib.parse.urlencode(params)
- headers = {
- "Content-type": "application/x-www-form-urlencoded",
- "Key": self._key,
- "Sign": self._sha512(encoded_params),
- }
- conn = http.client.HTTPSConnection(self.API_URL, timeout=self._timeout)
- conn.request("POST", f"/{self.API_VERSION}/{name}/", encoded_params, headers)
- response = Dict(json.loads(conn.getresponse().read().decode("utf-8")))
- conn.close()
- if response.result is False:
- raise ApiError(params, response)
- return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement