Skip to content

Commit

Permalink
adjust the encoding stuff in kraken.spot
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Mar 8, 2024
1 parent 7798535 commit 0281ae0
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 70 deletions.
31 changes: 14 additions & 17 deletions kraken/base_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import hmac
import json
import time
import urllib.parse
from functools import wraps
from typing import Any, Callable, Final, Optional, Type, TypeVar
from urllib.parse import urlencode, urljoin
Expand Down Expand Up @@ -182,9 +181,8 @@ class KrakenSpotBaseAPI:
:type sandbox: bool, optional
"""

URL: Final[str] = "https://api.kraken.com"
API_V: Final[str] = "/0"
TIMEOUT: Final[int] = 10
URL: str = "https://api.kraken.com"
TIMEOUT: int = 10

def __init__(
self: KrakenSpotBaseAPI,
Expand All @@ -198,9 +196,7 @@ def __init__(
if sandbox:
raise ValueError("Sandbox not available for Kraken Spot trading.")
if url:
self.url = url
else:
self.url = urljoin(self.URL, self.API_V)
self.URL = url

self.__key: str = key
self.__secret: str = secret
Expand Down Expand Up @@ -254,6 +250,9 @@ def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
:rtype: dict[str, Any] | list[str] | list[dict[str, Any]] |
requests.Response
"""
METHOD: str = method.upper()
URL: str = urljoin(self.URL, uri)

if not defined(params):
params = {}
if defined(extra_params):
Expand All @@ -263,12 +262,11 @@ def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
else extra_params
)

METHOD: str = method.upper()
if METHOD in {"GET", "DELETE"} and params:
data_json: str = "&".join(
[f"{key}={params[key]}" for key in sorted(params)],
)
uri += f"?{data_json}".replace(" ", "%20")
query_params: str = (
urlencode(params, doseq=True)
if METHOD in {"GET", "DELETE"} and params
else ""
)

TIMEOUT: int = self.TIMEOUT if timeout != 10 else timeout
HEADERS: dict = {}
Expand All @@ -286,26 +284,25 @@ def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
sign_data = json.dumps(params)
else:
content_type = "application/x-www-form-urlencoded; charset=utf-8"
sign_data = urllib.parse.urlencode(params)
sign_data = urlencode(params, doseq=True)

HEADERS.update(
{
"Content-Type": content_type,
"API-Key": self.__key,
"API-Sign": self._get_kraken_signature(
url_path=f"{self.API_V}{uri}",
url_path=f"{uri}{query_params}",
data=sign_data,
nonce=params["nonce"],
),
},
)

URL: str = f"{self.url}{uri}"
if METHOD in {"GET", "DELETE"}:
return self.__check_response_data(
response=self.__session.request(
method=METHOD,
url=URL,
url=f"{URL}?{query_params}",
headers=HEADERS,
timeout=TIMEOUT,
),
Expand Down
20 changes: 10 additions & 10 deletions kraken/spot/funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_deposit_methods(
"""
return self._request(
method="POST",
uri="/private/DepositMethods",
uri="/0/private/DepositMethods",
params={"asset": asset}, # type: ignore[return-value]
extra_params=extra_params,
)
Expand Down Expand Up @@ -150,7 +150,7 @@ def get_deposit_address(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/DepositAddresses",
uri="/0/private/DepositAddresses",
params={"asset": asset, "method": method, "new": new},
extra_params=extra_params,
)
Expand Down Expand Up @@ -243,7 +243,7 @@ def get_recent_deposits_status(

return self._request( # type: ignore[return-value]
method="POST",
uri="/private/DepositStatus",
uri="/0/private/DepositStatus",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -296,7 +296,7 @@ def get_withdrawal_info(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WithdrawInfo",
uri="/0/private/WithdrawInfo",
params={"asset": asset, "key": str(key), "amount": str(amount)},
extra_params=extra_params,
)
Expand Down Expand Up @@ -349,7 +349,7 @@ def withdraw_funds(

return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Withdraw",
uri="/0/private/Withdraw",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -419,7 +419,7 @@ def get_recent_withdraw_status(
params["cursor"] = cursor
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WithdrawStatus",
uri="/0/private/WithdrawStatus",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -457,7 +457,7 @@ def cancel_withdraw(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WithdrawCancel",
uri="/0/private/WithdrawCancel",
params={"asset": asset, "refid": str(refid)},
extra_params=extra_params,
)
Expand Down Expand Up @@ -505,7 +505,7 @@ def wallet_transfer(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WalletTransfer",
uri="/0/private/WalletTransfer",
params={"asset": asset, "from": from_, "to": to_, "amount": str(amount)},
extra_params=extra_params,
)
Expand Down Expand Up @@ -542,7 +542,7 @@ def withdraw_methods(
params["network"] = network
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WithdrawMethods",
uri="/0/private/WithdrawMethods",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -589,7 +589,7 @@ def withdraw_addresses(
params["verified"] = verified
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/WithdrawMethods",
uri="/0/private/WithdrawMethods",
params=params,
extra_params=extra_params,
)
Expand Down
16 changes: 8 additions & 8 deletions kraken/spot/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def get_assets(
params["aclass"] = aclass
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/Assets",
uri="/0/public/Assets",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -213,7 +213,7 @@ def get_asset_pairs(
params["info"] = info
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/AssetPairs",
uri="/0/public/AssetPairs",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -263,7 +263,7 @@ def get_ticker(
params["pair"] = pair
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/Ticker",
uri="/0/public/Ticker",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -318,7 +318,7 @@ def get_ohlc(
params["since"] = since
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/OHLC",
uri="/0/public/OHLC",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -366,7 +366,7 @@ def get_order_book(
"""
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/Depth",
uri="/0/public/Depth",
params={"pair": pair, "count": count},
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -417,7 +417,7 @@ def get_recent_trades(
params["count"] = count
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/Trades",
uri="/0/public/Trades",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -462,7 +462,7 @@ def get_recent_spreads(
params["since"] = since
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/Spread",
uri="/0/public/Spread",
params=params,
auth=False,
extra_params=extra_params,
Expand Down Expand Up @@ -491,7 +491,7 @@ def get_system_status(
"""
return self._request( # type: ignore[return-value]
method="GET",
uri="/public/SystemStatus",
uri="/0/public/SystemStatus",
auth=False,
extra_params=extra_params,
)
Expand Down
10 changes: 5 additions & 5 deletions kraken/spot/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def stake_asset(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Stake",
uri="/0/private/Stake",
params={"asset": asset, "amount": amount, "method": method},
auth=True,
extra_params=extra_params,
Expand Down Expand Up @@ -154,7 +154,7 @@ def unstake_asset(

return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Unstake",
uri="/0/private/Unstake",
params=params,
auth=True,
extra_params=extra_params,
Expand Down Expand Up @@ -219,7 +219,7 @@ def list_stakeable_assets(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Staking/Assets",
uri="/0/private/Staking/Assets",
auth=True,
extra_params=extra_params,
)
Expand Down Expand Up @@ -262,7 +262,7 @@ def get_pending_staking_transactions(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Staking/Pending",
uri="/0/private/Staking/Pending",
auth=True,
extra_params=extra_params,
)
Expand Down Expand Up @@ -308,7 +308,7 @@ def list_staking_transactions(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/Staking/Transactions",
uri="/0/private/Staking/Transactions",
auth=True,
extra_params=extra_params,
)
Expand Down
14 changes: 7 additions & 7 deletions kraken/spot/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa

return self._request( # type: ignore[return-value]
method="POST",
uri="/private/AddOrder",
uri="/0/private/AddOrder",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -438,7 +438,7 @@ def create_order_batch(
params["deadline"] = deadline
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/AddOrderBatch",
uri="/0/private/AddOrderBatch",
params=params,
do_json=True,
extra_params=extra_params,
Expand Down Expand Up @@ -545,7 +545,7 @@ def edit_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
params["deadline"] = deadline
return self._request( # type: ignore[return-value]
"POST",
uri="/private/EditOrder",
uri="/0/private/EditOrder",
params=params,
extra_params=extra_params,
)
Expand Down Expand Up @@ -582,7 +582,7 @@ def cancel_order(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/CancelOrder",
uri="/0/private/CancelOrder",
params={"txid": txid},
extra_params=extra_params,
)
Expand Down Expand Up @@ -614,7 +614,7 @@ def cancel_all_orders(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/CancelAll",
uri="/0/private/CancelAll",
extra_params=extra_params,
)

Expand Down Expand Up @@ -651,7 +651,7 @@ def cancel_all_orders_after_x(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/CancelAllOrdersAfter",
uri="/0/private/CancelAllOrdersAfter",
params={"timeout": timeout},
extra_params=extra_params,
)
Expand Down Expand Up @@ -688,7 +688,7 @@ def cancel_order_batch(
"""
return self._request( # type: ignore[return-value]
method="POST",
uri="/private/CancelOrderBatch",
uri="/0/private/CancelOrderBatch",
params={"orders": orders},
do_json=True,
extra_params=extra_params,
Expand Down
Loading

0 comments on commit 0281ae0

Please sign in to comment.