Skip to content

Commit 23ebb6a

Browse files
Add proxy support for futures and spot clients (async and sync) (#257)
Co-authored-by: Benjamin T. Schwertfeger <contact@b-schwertfeger.de>
1 parent f7c5cf9 commit 23ebb6a

15 files changed

Lines changed: 163 additions & 18 deletions

File tree

kraken/base_api/__init__.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class SpotClient:
187187
:type secret: str, optional
188188
:param url: URL to access the Kraken API (default: https://api.kraken.com)
189189
:type url: str, optional
190+
:param proxy: proxy URL, may contain authentication information
191+
:type proxy: str, optional
190192
"""
191193

192194
URL: str = "https://api.kraken.com"
@@ -201,6 +203,7 @@ def __init__(
201203
key: str = "",
202204
secret: str = "",
203205
url: str = "",
206+
proxy: str | None = None,
204207
*,
205208
use_custom_exceptions: bool = True,
206209
) -> None:
@@ -212,6 +215,13 @@ def __init__(
212215
self._use_custom_exceptions: bool = use_custom_exceptions
213216
self._err_handler: ErrorHandler = ErrorHandler()
214217
self.__session: requests.Session = requests.Session()
218+
if proxy is not None:
219+
self.__session.proxies.update(
220+
{
221+
"http": proxy,
222+
"https": proxy,
223+
},
224+
)
215225
self.__session.headers.update(self.HEADERS)
216226

217227
def _prepare_request(
@@ -469,13 +479,16 @@ class SpotAsyncClient(SpotClient):
469479
:type secret: str, optional
470480
:param url: URL to access the Kraken API (default: https://api.kraken.com)
471481
:type url: str, optional
482+
:param proxy: proxy URL, may contain authentication information
483+
:type proxy: str, optional
472484
"""
473485

474486
def __init__(
475487
self: SpotAsyncClient,
476488
key: str = "",
477489
secret: str = "",
478490
url: str = "",
491+
proxy: str | None = None,
479492
*,
480493
use_custom_exceptions: bool = True,
481494
) -> None:
@@ -486,6 +499,7 @@ def __init__(
486499
use_custom_exceptions=use_custom_exceptions,
487500
)
488501
self.__session = aiohttp.ClientSession(headers=self.HEADERS)
502+
self.proxy = proxy
489503

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

545559
if method in {"GET", "DELETE"}:
546560
return await self.__check_response_data( # type: ignore[return-value]
547-
response=await self.__session.request( # type: ignore[misc]
561+
response=await self.__session.request( # type: ignore[misc,call-arg]
548562
method=method,
549563
url=f"{url}?{query_params}" if query_params else url,
550564
headers=headers,
551565
timeout=timeout,
566+
proxy=self.proxy,
552567
),
553568
return_raw=return_raw,
554569
)
555570

556571
if do_json:
557572
return await self.__check_response_data( # type: ignore[return-value]
558-
response=await self.__session.request( # type: ignore[misc]
573+
response=await self.__session.request( # type: ignore[misc,call-arg]
559574
method=method,
560575
url=url,
561576
headers=headers,
562577
json=params,
563578
timeout=timeout,
579+
proxy=self.proxy,
564580
),
565581
return_raw=return_raw,
566582
)
567583

568584
return await self.__check_response_data( # type: ignore[return-value]
569-
response=await self.__session.request( # type: ignore[misc]
585+
response=await self.__session.request( # type: ignore[misc,call-arg]
570586
method=method,
571587
url=url,
572588
headers=headers,
573589
data=params,
574590
timeout=timeout,
591+
proxy=self.proxy,
575592
),
576593
return_raw=return_raw,
577594
)
@@ -646,6 +663,8 @@ class FuturesClient:
646663
:type url: str, optional
647664
:param sandbox: If set to ``True`` the URL will be https://demo-futures.kraken.com (default: ``False``)
648665
:type sandbox: bool, optional
666+
:param proxy: proxy URL, may contain authentication information
667+
:type proxy: str, optional
649668
"""
650669

651670
URL: str = "https://futures.kraken.com"
@@ -661,6 +680,7 @@ def __init__(
661680
key: str = "",
662681
secret: str = "",
663682
url: str = "",
683+
proxy: str | None = None,
664684
*,
665685
sandbox: bool = False,
666686
use_custom_exceptions: bool = True,
@@ -686,6 +706,13 @@ def __init__(
686706
" (https://github.com/btschwertfeger/python-kraken-sdk)",
687707
},
688708
)
709+
if proxy is not None:
710+
self.__session.proxies.update(
711+
{
712+
"http": proxy,
713+
"https": proxy,
714+
},
715+
)
689716

690717
def _prepare_request(
691718
self: FuturesClient,
@@ -931,6 +958,8 @@ class FuturesAsyncClient(FuturesClient):
931958
:param url: The URL to access the Futures Kraken API (default:
932959
https://futures.kraken.com)
933960
:type url: str, optional
961+
:param proxy: proxy URL, may contain authentication information
962+
:type proxy: str, optional
934963
:param sandbox: If set to ``True`` the URL will be
935964
https://demo-futures.kraken.com (default: ``False``)
936965
:type sandbox: bool, optional
@@ -941,6 +970,7 @@ def __init__(
941970
key: str = "",
942971
secret: str = "",
943972
url: str = "",
973+
proxy: str | None = None,
944974
*,
945975
sandbox: bool = False,
946976
use_custom_exceptions: bool = True,
@@ -953,6 +983,7 @@ def __init__(
953983
use_custom_exceptions=use_custom_exceptions,
954984
)
955985
self.__session = aiohttp.ClientSession(headers=self.HEADERS)
986+
self.proxy = proxy
956987

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

9771008
if method in {"GET", "DELETE"}:
9781009
return await self.__check_response_data(
979-
response=await self.__session.request( # type: ignore[misc]
1010+
response=await self.__session.request( # type: ignore[misc,call-arg]
9801011
method=method,
9811012
url=url,
9821013
params=query_string,
9831014
headers=headers,
9841015
timeout=timeout,
1016+
proxy=self.proxy,
9851017
),
9861018
return_raw=return_raw,
9871019
)
9881020

9891021
if method == "PUT":
9901022
return await self.__check_response_data(
991-
response=await self.__session.request( # type: ignore[misc]
1023+
response=await self.__session.request( # type: ignore[misc,call-arg]
9921024
method=method,
9931025
url=url,
9941026
params=encoded_payload,
9951027
headers=headers,
9961028
timeout=timeout,
1029+
proxy=self.proxy,
9971030
),
9981031
return_raw=return_raw,
9991032
)
10001033

10011034
return await self.__check_response_data(
1002-
response=await self.__session.request( # type: ignore[misc]
1035+
response=await self.__session.request( # type: ignore[misc,call-arg]
10031036
method=method,
10041037
url=url,
10051038
data=encoded_payload,
10061039
headers=headers,
10071040
timeout=timeout,
1041+
proxy=self.proxy,
10081042
),
10091043
return_raw=return_raw,
10101044
)

kraken/futures/funding.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Funding(FuturesClient):
2727
:param url: Alternative URL to access the Futures Kraken API (default:
2828
https://futures.kraken.com)
2929
:type url: str, optional
30+
:param proxy: proxy URL, may contain authentication information
31+
:type proxy: str, optional
3032
:param sandbox: If set to ``True`` the URL will be
3133
https://demo-futures.kraken.com (default: ``False``)
3234
:type sandbox: bool, optional
@@ -53,10 +55,11 @@ def __init__(
5355
key: str = "",
5456
secret: str = "",
5557
url: str = "",
58+
proxy: str | None = None,
5659
*,
5760
sandbox: bool = False,
5861
) -> None:
59-
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
62+
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)
6063

6164
def __enter__(self: Self) -> Self:
6265
super().__enter__()

kraken/futures/market.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Market(FuturesClient):
2929
:param url: Alternative URL to access the Futures Kraken API (default:
3030
https://futures.kraken.com)
3131
:type url: str, optional
32+
:param proxy: proxy URL, may contain authentication information
33+
:type proxy: str, optional
3234
:param sandbox: If set to ``True`` the URL will be
3335
https://demo-futures.kraken.com (default: ``False``)
3436
:type sandbox: bool, optional
@@ -55,10 +57,11 @@ def __init__(
5557
key: str = "",
5658
secret: str = "",
5759
url: str = "",
60+
proxy: str | None = None,
5861
*,
5962
sandbox: bool = False,
6063
) -> None:
61-
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
64+
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)
6265

6366
def __enter__(self: Self) -> Self:
6467
super().__enter__()

kraken/futures/trade.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Trade(FuturesClient):
2828
:param url: Alternative URL to access the Futures Kraken API (default:
2929
https://futures.kraken.com)
3030
:type url: str, optional
31+
:param proxy: proxy URL, may contain authentication information
32+
:type proxy: str, optional
3133
:param sandbox: If set to ``True`` the URL will be
3234
https://demo-futures.kraken.com (default: ``False``)
3335
:type sandbox: bool, optional
@@ -54,10 +56,11 @@ def __init__(
5456
key: str = "",
5557
secret: str = "",
5658
url: str = "",
59+
proxy: str | None = None,
5760
*,
5861
sandbox: bool = False,
5962
) -> None:
60-
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
63+
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)
6164

6265
def __enter__(self: Self) -> Self:
6366
super().__enter__()

kraken/futures/user.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class User(FuturesClient):
3131
:param url: Alternative URL to access the Futures Kraken API (default:
3232
https://futures.kraken.com)
3333
:type url: str, optional
34+
:param proxy: proxy URL, may contain authentication information
35+
:type proxy: str, optional
3436
:param sandbox: If set to ``True`` the URL will be
3537
https://demo-futures.kraken.com (default: ``False``)
3638
:type sandbox: bool, optional
@@ -57,10 +59,11 @@ def __init__(
5759
key: str = "",
5860
secret: str = "",
5961
url: str = "",
62+
proxy: str | None = None,
6063
*,
6164
sandbox: bool = False,
6265
) -> None:
63-
super().__init__(key=key, secret=secret, url=url, sandbox=sandbox)
66+
super().__init__(key=key, secret=secret, url=url, proxy=proxy, sandbox=sandbox)
6467

6568
def __enter__(self: Self) -> Self:
6669
super().__enter__()

kraken/nft/market.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Market(NFTClient):
2727
:type key: str, optional
2828
:param secret: Spot API secret key (default: ``""``)
2929
:type secret: str, optional
30+
:param proxy: proxy URL, may contain authentication information
31+
:type proxy: str, optional
3032
3133
.. code-block:: python
3234
:linenos:
@@ -50,8 +52,9 @@ def __init__(
5052
key: str = "",
5153
secret: str = "",
5254
url: str = "",
55+
proxy: str | None = None,
5356
) -> None:
54-
super().__init__(key=key, secret=secret, url=url)
57+
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
5558

5659
def __enter__(self: Self) -> Self:
5760
super().__enter__()

kraken/nft/trade.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ def __init__(
5050
key: str = "",
5151
secret: str = "",
5252
url: str = "",
53+
proxy: str | None = None,
5354
) -> None:
54-
super().__init__(key=key, secret=secret, url=url)
55+
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
5556

5657
def __enter__(self: Self) -> Self:
5758
super().__enter__()

kraken/spot/earn.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Earn(SpotClient):
2929
:param url: Alternative URL to access the Kraken API (default:
3030
https://api.kraken.com)
3131
:type url: str, optional
32+
:param proxy: proxy URL, may contain authentication information
33+
:type proxy: str, optional
3234
3335
.. code-block:: python
3436
:linenos:
@@ -52,8 +54,9 @@ def __init__(
5254
key: str = "",
5355
secret: str = "",
5456
url: str = "",
57+
proxy: str | None = None,
5558
) -> None:
56-
super().__init__(key=key, secret=secret, url=url)
59+
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
5760

5861
def __enter__(self: Self) -> Self:
5962
super().__enter__()

kraken/spot/funding.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Funding(SpotClient):
2828
:param url: Alternative URL to access the Kraken API (default:
2929
https://api.kraken.com)
3030
:type url: str, optional
31+
:param proxy: proxy URL, may contain authentication information
32+
:type proxy: str, optional
3133
3234
.. code-block:: python
3335
:linenos:
@@ -51,8 +53,9 @@ def __init__(
5153
key: str = "",
5254
secret: str = "",
5355
url: str = "",
56+
proxy: str | None = None,
5457
) -> None:
55-
super().__init__(key=key, secret=secret, url=url)
58+
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
5659

5760
def __enter__(self: Self) -> Self:
5861
super().__enter__()

kraken/spot/market.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Market(SpotClient):
2929
:param url: Alternative URL to access the Kraken API (default:
3030
https://api.kraken.com)
3131
:type url: str, optional
32+
:param proxy: proxy URL, may contain authentication information
33+
:type proxy: str, optional
3234
3335
.. code-block:: python
3436
:linenos:
@@ -52,8 +54,9 @@ def __init__(
5254
key: str = "",
5355
secret: str = "",
5456
url: str = "",
57+
proxy: str | None = None,
5558
) -> None:
56-
super().__init__(key=key, secret=secret, url=url)
59+
super().__init__(key=key, secret=secret, url=url, proxy=proxy)
5760

5861
def __enter__(self: Self) -> Self:
5962
super().__enter__()

0 commit comments

Comments
 (0)