Skip to content

Commit

Permalink
Add proxy support for futures and spot clients (async and sync) (#257)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin T. Schwertfeger <contact@b-schwertfeger.de>
  • Loading branch information
Graeme22 and btschwertfeger authored Jul 31, 2024
1 parent f7c5cf9 commit 23ebb6a
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 18 deletions.
46 changes: 40 additions & 6 deletions kraken/base_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class SpotClient:
:type secret: str, optional
:param url: URL to access the Kraken API (default: https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
"""

URL: str = "https://api.kraken.com"
Expand All @@ -201,6 +203,7 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
use_custom_exceptions: bool = True,
) -> None:
Expand All @@ -212,6 +215,13 @@ def __init__(
self._use_custom_exceptions: bool = use_custom_exceptions
self._err_handler: ErrorHandler = ErrorHandler()
self.__session: requests.Session = requests.Session()
if proxy is not None:
self.__session.proxies.update(
{
"http": proxy,
"https": proxy,
},
)
self.__session.headers.update(self.HEADERS)

def _prepare_request(
Expand Down Expand Up @@ -469,13 +479,16 @@ class SpotAsyncClient(SpotClient):
:type secret: str, optional
:param url: URL to access the Kraken API (default: https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
"""

def __init__(
self: SpotAsyncClient,
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
use_custom_exceptions: bool = True,
) -> None:
Expand All @@ -486,6 +499,7 @@ def __init__(
use_custom_exceptions=use_custom_exceptions,
)
self.__session = aiohttp.ClientSession(headers=self.HEADERS)
self.proxy = proxy

async def request( # type: ignore[override] # pylint: disable=invalid-overridden-method,too-many-arguments # noqa: PLR0913
self: SpotAsyncClient,
Expand Down Expand Up @@ -544,34 +558,37 @@ async def request( # type: ignore[override] # pylint: disable=invalid-overridde

if method in {"GET", "DELETE"}:
return await self.__check_response_data( # type: ignore[return-value]
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=f"{url}?{query_params}" if query_params else url,
headers=headers,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)

if do_json:
return await self.__check_response_data( # type: ignore[return-value]
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=url,
headers=headers,
json=params,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)

return await self.__check_response_data( # type: ignore[return-value]
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=url,
headers=headers,
data=params,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)
Expand Down Expand Up @@ -646,6 +663,8 @@ class FuturesClient:
:type url: str, optional
:param sandbox: If set to ``True`` the URL will be https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
"""

URL: str = "https://futures.kraken.com"
Expand All @@ -661,6 +680,7 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
use_custom_exceptions: bool = True,
Expand All @@ -686,6 +706,13 @@ def __init__(
" (https://github.com/btschwertfeger/python-kraken-sdk)",
},
)
if proxy is not None:
self.__session.proxies.update(
{
"http": proxy,
"https": proxy,
},
)

def _prepare_request(
self: FuturesClient,
Expand Down Expand Up @@ -931,6 +958,8 @@ class FuturesAsyncClient(FuturesClient):
:param url: The URL to access the Futures Kraken API (default:
https://futures.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
:param sandbox: If set to ``True`` the URL will be
https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
Expand All @@ -941,6 +970,7 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
use_custom_exceptions: bool = True,
Expand All @@ -953,6 +983,7 @@ def __init__(
use_custom_exceptions=use_custom_exceptions,
)
self.__session = aiohttp.ClientSession(headers=self.HEADERS)
self.proxy = proxy

async def request( # type: ignore[override] # pylint: disable=arguments-differ,invalid-overridden-method
self: FuturesAsyncClient,
Expand All @@ -976,35 +1007,38 @@ async def request( # type: ignore[override] # pylint: disable=arguments-differ,

if method in {"GET", "DELETE"}:
return await self.__check_response_data(
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=url,
params=query_string,
headers=headers,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)

if method == "PUT":
return await self.__check_response_data(
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=url,
params=encoded_payload,
headers=headers,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)

return await self.__check_response_data(
response=await self.__session.request( # type: ignore[misc]
response=await self.__session.request( # type: ignore[misc,call-arg]
method=method,
url=url,
data=encoded_payload,
headers=headers,
timeout=timeout,
proxy=self.proxy,
),
return_raw=return_raw,
)
Expand Down
5 changes: 4 additions & 1 deletion kraken/futures/funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Funding(FuturesClient):
:param url: Alternative URL to access the Futures Kraken API (default:
https://futures.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
:param sandbox: If set to ``True`` the URL will be
https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
Expand All @@ -53,10 +55,11 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
) -> None:
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/futures/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Market(FuturesClient):
:param url: Alternative URL to access the Futures Kraken API (default:
https://futures.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
:param sandbox: If set to ``True`` the URL will be
https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
Expand All @@ -55,10 +57,11 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
) -> None:
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/futures/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Trade(FuturesClient):
:param url: Alternative URL to access the Futures Kraken API (default:
https://futures.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
:param sandbox: If set to ``True`` the URL will be
https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
Expand All @@ -54,10 +56,11 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
) -> None:
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/futures/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class User(FuturesClient):
:param url: Alternative URL to access the Futures Kraken API (default:
https://futures.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
:param sandbox: If set to ``True`` the URL will be
https://demo-futures.kraken.com (default: ``False``)
:type sandbox: bool, optional
Expand All @@ -57,10 +59,11 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
*,
sandbox: bool = False,
) -> None:
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/nft/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Market(NFTClient):
:type key: str, optional
:param secret: Spot API secret key (default: ``""``)
:type secret: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
.. code-block:: python
:linenos:
Expand All @@ -50,8 +52,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
3 changes: 2 additions & 1 deletion kraken/nft/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/spot/earn.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Earn(SpotClient):
:param url: Alternative URL to access the Kraken API (default:
https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
.. code-block:: python
:linenos:
Expand All @@ -52,8 +54,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/spot/funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Funding(SpotClient):
:param url: Alternative URL to access the Kraken API (default:
https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
.. code-block:: python
:linenos:
Expand All @@ -51,8 +53,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/spot/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Market(SpotClient):
:param url: Alternative URL to access the Kraken API (default:
https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
.. code-block:: python
:linenos:
Expand All @@ -52,8 +54,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)

def __enter__(self: Self) -> Self:
super().__enter__()
Expand Down
5 changes: 4 additions & 1 deletion kraken/spot/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Trade(SpotClient):
:type secret: str, optional
:param url: The URL to access the Kraken API (default: https://api.kraken.com)
:type url: str, optional
:param proxy: proxy URL, may contain authentication information
:type proxy: str, optional
.. code-block:: python
:linenos:
Expand All @@ -54,8 +56,9 @@ def __init__(
key: str = "",
secret: str = "",
url: str = "",
proxy: str | None = None,
) -> None:
super().__init__(key=key, secret=secret, url=url)
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
self.__market: Market = Market()

def __enter__(self: Self) -> Self:
Expand Down
Loading

0 comments on commit 23ebb6a

Please sign in to comment.