Skip to content

Commit

Permalink
Merge branch 'master' into 176-add-start-end-and-cursor-parameters-to…
Browse files Browse the repository at this point in the history
…-krakenspotwithdraw_status
  • Loading branch information
btschwertfeger committed Dec 6, 2023
2 parents b61505c + 94cb0bd commit ebe16ad
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
FUTURES_SECRET_KEY: ${{ secrets.FUTURES_SECRET_KEY }}
FUTURES_SANDBOX_KEY: ${{ secrets.FUTURES_SANDBOX_KEY }}
FUTURES_SANDBOX_SECRET: ${{ secrets.FUTURES_SANDBOX_SECRET }}
run: pytest -vv --cov --cov-report=xml:coverage.xml tests
run: pytest -vv --cov --cov-report=xml:coverage.xml -m "not flaky" tests

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/_test_futures_private.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
FUTURES_SECRET_KEY: ${{ secrets.FUTURES_SECRET_KEY }}
FUTURES_SANDBOX_KEY: ${{ secrets.FUTURES_SANDBOX_KEY }}
FUTURES_SANDBOX_SECRET: ${{ secrets.FUTURES_SANDBOX_SECRET }}
run: pytest -vv -m "futures_auth and not futures_websocket" tests
run: pytest -vv -m "futures_auth and not futures_websocket and not flaky" tests

## Unit tests of the Futures websocket client
##
Expand All @@ -69,4 +69,4 @@ jobs:
FUTURES_SECRET_KEY: ${{ secrets.FUTURES_SECRET_KEY }}
FUTURES_SANDBOX_KEY: ${{ secrets.FUTURES_SANDBOX_KEY }}
FUTURES_SANDBOX_SECRET: ${{ secrets.FUTURES_SANDBOX_SECRET }}
run: pytest -vv -m futures_websocket tests
run: pytest -vv -m "futures_websocket and not flaky" tests
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# GitHub: https://github.com/btschwertfeger
#

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: check-json
Expand Down Expand Up @@ -51,7 +52,7 @@ repos:
args:
- --profile=black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.1
rev: v0.1.7
hooks:
- id: ruff
args:
Expand All @@ -67,7 +68,7 @@ repos:
- --show-source
- --statistics
- repo: https://github.com/pycqa/pylint
rev: v2.17.5
rev: v3.0.1
hooks:
- id: pylint
name: pylint
Expand All @@ -77,7 +78,7 @@ repos:
- --rcfile=pyproject.toml
- -d=R0801 # ignore duplicate code
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.7.1
hooks:
- id: mypy
name: mypy
Expand Down
81 changes: 46 additions & 35 deletions kraken/base_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ class KrakenSpotBaseAPI:
This class the the base for all Spot clients, handles un-/signed
requests and returns exception handled results.
If you are facing timeout errors on derived clients, you can make use of the
``TIMEOUT`` attribute to deviate from the default ``10`` seconds.
:param key: Spot API public key (default: ``""``)
:type key: str, optional
:param secret: Spot API secret key (default: ``""``)
Expand All @@ -181,6 +184,7 @@ class KrakenSpotBaseAPI:

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

def __init__(
self: KrakenSpotBaseAPI,
Expand All @@ -206,7 +210,7 @@ def __init__(
self.__session: requests.Session = requests.Session()
self.__session.headers.update({"User-Agent": "python-kraken-sdk"})

def _request( # noqa: PLR0913
def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
self: KrakenSpotBaseAPI,
method: str,
uri: str,
Expand Down Expand Up @@ -259,14 +263,16 @@ def _request( # noqa: PLR0913
else extra_params
)

method = method.upper()
if method in {"GET", "DELETE"} and 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")

headers: dict = {}
TIMEOUT: int = self.TIMEOUT if timeout != 10 else timeout
HEADERS: dict = {}

if auth:
if not self.__key or not self.__secret:
raise ValueError("Missing credentials.")
Expand All @@ -282,7 +288,7 @@ def _request( # noqa: PLR0913
content_type = "application/x-www-form-urlencoded; charset=utf-8"
sign_data = urllib.parse.urlencode(params)

headers.update(
HEADERS.update(
{
"Content-Type": content_type,
"API-Key": self.__key,
Expand All @@ -294,37 +300,37 @@ def _request( # noqa: PLR0913
},
)

url: str = f"{self.url}{uri}"
if method in {"GET", "DELETE"}:
URL: str = f"{self.url}{uri}"
if METHOD in {"GET", "DELETE"}:
return self.__check_response_data(
response=self.__session.request(
method=method,
url=url,
headers=headers,
timeout=timeout,
method=METHOD,
url=URL,
headers=HEADERS,
timeout=TIMEOUT,
),
return_raw=return_raw,
)

if do_json:
return self.__check_response_data(
response=self.__session.request(
method=method,
url=url,
headers=headers,
method=METHOD,
url=URL,
headers=HEADERS,
json=params,
timeout=timeout,
timeout=TIMEOUT,
),
return_raw=return_raw,
)

return self.__check_response_data(
response=self.__session.request(
method=method,
url=url,
headers=headers,
method=METHOD,
url=URL,
headers=HEADERS,
data=params,
timeout=timeout,
timeout=TIMEOUT,
),
return_raw=return_raw,
)
Expand Down Expand Up @@ -416,6 +422,9 @@ class KrakenFuturesBaseAPI:
The base class for all Futures clients handles un-/signed requests
and returns exception handled results.
If you are facing timeout errors on derived clients, you can make use of the
``TIMEOUT`` attribute to deviate from the default ``10`` seconds.
If the sandbox environment is chosen, the keys must be generated from here:
https://demo-futures.kraken.com/settings/api
Expand All @@ -431,6 +440,7 @@ class KrakenFuturesBaseAPI:

URL: str = "https://futures.kraken.com"
SANDBOX_URL: str = "https://demo-futures.kraken.com"
TIMEOUT: int = 10

def __init__(
self: KrakenFuturesBaseAPI,
Expand Down Expand Up @@ -458,7 +468,7 @@ def __init__(
self.__session: requests.Session = requests.Session()
self.__session.headers.update({"User-Agent": "python-kraken-sdk"})

def _request( # noqa: PLR0913
def _request( # noqa: PLR0913 # pylint: disable=too-many-arguments
self: KrakenFuturesBaseAPI,
method: str,
uri: str,
Expand All @@ -468,7 +478,7 @@ def _request( # noqa: PLR0913
*,
auth: bool = True,
return_raw: bool = False,
extra_params: Optional[dict] = None,
extra_params: Optional[str | dict] = None,
) -> dict[str, Any] | list[dict[str, Any]] | list[str] | requests.Response:
"""
Handles the requested requests, by sending the request, handling the
Expand Down Expand Up @@ -501,7 +511,7 @@ def _request( # noqa: PLR0913
:return: The response
:rtype: dict[str, Any] | list[dict[str, Any]] | list[str] | requests.Response
"""
method = method.upper()
METHOD: str = method.upper()

post_string: str = ""
listed_params: list[str]
Expand Down Expand Up @@ -531,12 +541,13 @@ def _request( # noqa: PLR0913
else:
query_params = {}

headers: dict = {}
TIMEOUT: int = self.TIMEOUT if timeout == 10 else timeout
HEADERS: dict = {}
if auth:
if not self.__key or not self.__secret:
raise ValueError("Missing credentials")
nonce: str = str(int(time.time() * 100_000_000))
headers.update(
HEADERS.update(
{
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Nonce": nonce,
Expand All @@ -549,38 +560,38 @@ def _request( # noqa: PLR0913
},
)

if method in {"GET", "DELETE"}:
if METHOD in {"GET", "DELETE"}:
return self.__check_response_data(
response=self.__session.request(
method=method,
method=METHOD,
url=f"{self.url}{uri}"
if not query_string
else f"{self.url}{uri}?{query_string}",
headers=headers,
timeout=timeout,
headers=HEADERS,
timeout=TIMEOUT,
),
return_raw=return_raw,
)

if method == "PUT":
if METHOD == "PUT":
return self.__check_response_data(
response=self.__session.request(
method=method,
method=METHOD,
url=f"{self.url}{uri}",
params=str.encode(post_string),
headers=headers,
timeout=timeout,
headers=HEADERS,
timeout=TIMEOUT,
),
return_raw=return_raw,
)

return self.__check_response_data(
response=self.__session.request(
method=method,
method=METHOD,
url=f"{self.url}{uri}?{post_string}",
data=str.encode(post_string),
headers=headers,
timeout=timeout,
headers=HEADERS,
timeout=TIMEOUT,
),
return_raw=return_raw,
)
Expand Down
4 changes: 2 additions & 2 deletions kraken/futures/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,15 @@ def get_orders_status(
extra_params=extra_params,
)

def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913
def create_order( # pylint: disable=too-many-arguments # noqa: PLR0913, PLR0917
self: Trade,
orderType: str,
size: str | float,
symbol: str,
side: str,
cliOrdId: Optional[str] = None,
limitPrice: Optional[str | float] = None,
reduceOnly: Optional[bool] = None,
reduceOnly: Optional[bool] = None, # noqa: FBT001
stopPrice: Optional[str | float] = None,
triggerSignal: Optional[str] = None,
trailingStopDeviationUnit: Optional[str] = None,
Expand Down
5 changes: 4 additions & 1 deletion kraken/futures/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def get_notifications(
extra_params=extra_params,
)

def get_account_log( # noqa: PLR0913
def get_account_log( # noqa: PLR0913 # pylint: disable=too-many-arguments
self: User,
before: Optional[str | int] = None,
count: Optional[str | int] = None,
Expand Down Expand Up @@ -484,6 +484,9 @@ def get_execution_events(
- https://docs.futures.kraken.com/#http-api-history-account-history-get-execution-events
(If you are facing some timeout error, just set the clients attribute
``TIMEOUT`` temporarily to the desired amount in seconds.)
:param before: Filter by time
:type before: int, optional
:param continuation_token: Token that can be used to continue requesting
Expand Down
2 changes: 1 addition & 1 deletion kraken/spot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# GitHub: https://github.com/btschwertfeger
# pylint: disable=unused-import
# pylint: disable=unused-import,cyclic-import

"""Module that provides the Spot REST clients and utility functions."""

Expand Down
Loading

0 comments on commit ebe16ad

Please sign in to comment.