2525
2626import logging
2727import sys
28- from re import sub as re_sub
2928from typing import TYPE_CHECKING , Any
29+ from urllib .parse import urlparse
3030
3131from click import echo
3232from cloup import (
4848LOG : logging .Logger = logging .getLogger (__name__ )
4949
5050
51- def print_version (ctx : Context , param : Any , value : Any ) -> None : # noqa: ANN401, ARG001
52- """Prints the version of the package"""
51+ def _print_version (
52+ ctx : Context ,
53+ param : Any , # noqa: ANN401, ARG001
54+ value : Any , # noqa: ANN401
55+ ) -> None :
56+ """Prints the version of the package."""
5357 if not value or ctx .resilient_parsing :
5458 return
5559 from importlib .metadata import version # noqa: PLC0415
@@ -58,6 +62,30 @@ def print_version(ctx: Context, param: Any, value: Any) -> None: # noqa: ANN401
5862 ctx .exit ()
5963
6064
65+ def _get_base_url (url : str ) -> str :
66+ """Extracts the base URL from a full URL."""
67+
68+ parsed_url = urlparse (url )
69+ if parsed_url .scheme and parsed_url .netloc :
70+ return f"{ parsed_url .scheme } ://{ parsed_url .netloc } "
71+ return ""
72+
73+
74+ def _get_uri_path (url : str ) -> str :
75+ """
76+ Extracts the URI path from a full URL or returns the URL if it's already a
77+ path.
78+ """
79+
80+ parsed_url = urlparse (url )
81+ if parsed_url .scheme and parsed_url .netloc :
82+ path = parsed_url .path
83+ if parsed_url .query :
84+ path += f"?{ parsed_url .query } "
85+ return path
86+ return url
87+
88+
6189@group (
6290 context_settings = {
6391 "auto_envvar_prefix" : "KRAKEN" ,
@@ -76,7 +104,7 @@ def print_version(ctx: Context, param: Any, value: Any) -> None: # noqa: ANN401
76104@option (
77105 "--version" ,
78106 is_flag = True ,
79- callback = print_version ,
107+ callback = _print_version ,
80108 expose_value = False ,
81109 is_eager = True ,
82110)
@@ -142,26 +170,27 @@ def spot(ctx: Context, url: str, **kwargs: dict) -> None: # noqa: ARG001
142170 """Access the Kraken Spot REST API"""
143171 from kraken .base_api import SpotClient # noqa: PLC0415
144172
145- LOG .debug ("Initialize the Kraken client" )
146173 client = SpotClient (
147174 key = kwargs ["api_key" ], # type: ignore[arg-type]
148175 secret = kwargs ["secret_key" ], # type: ignore[arg-type]
176+ url = _get_base_url (url ),
149177 )
150178
179+ uri = _get_uri_path (url )
151180 try :
152181 response = (
153182 client .request ( # pylint: disable=protected-access,no-value-for-parameter
154183 method = kwargs ["x" ], # type: ignore[arg-type]
155- uri = ( uri := re_sub ( r"https://.*.com" , "" , url )) ,
184+ uri = uri ,
156185 params = orloads (kwargs .get ("data" ) or "{}" ),
157186 timeout = kwargs ["timeout" ], # type: ignore[arg-type]
158187 auth = "private" in uri .lower (),
159188 )
160189 )
161190 except JSONDecodeError as exc :
162- LOG .error (f "Could not parse the passed data. { exc } " ) # noqa: G004
191+ LOG .error ("Could not parse the passed data. %s" , exc )
163192 except Exception as exc : # noqa: BLE001
164- LOG .error (f "Exception occurred: { exc } " ) # noqa: G004
193+ LOG .error ("Exception occurred: %s" , exc )
165194 sys .exit (1 )
166195 else :
167196 echo (response )
@@ -218,27 +247,28 @@ def futures(ctx: Context, url: str, **kwargs: dict) -> None: # noqa: ARG001
218247 """Access the Kraken Futures REST API"""
219248 from kraken .base_api import FuturesClient # noqa: PLC0415
220249
221- LOG .debug ("Initialize the Kraken client" )
222250 client = FuturesClient (
223251 key = kwargs ["api_key" ], # type: ignore[arg-type]
224252 secret = kwargs ["secret_key" ], # type: ignore[arg-type]
253+ url = _get_base_url (url ),
225254 )
226255
256+ uri = _get_uri_path (url )
227257 try :
228258 response = (
229259 client .request ( # pylint: disable=protected-access,no-value-for-parameter
230260 method = kwargs ["x" ], # type: ignore[arg-type]
231- uri = ( uri := re_sub ( r"https://.*.com" , "" , url )) ,
261+ uri = uri ,
232262 post_params = orloads (kwargs .get ("data" ) or "{}" ),
233263 query_params = orloads (kwargs .get ("query" ) or "{}" ),
234264 timeout = kwargs ["timeout" ], # type: ignore[arg-type]
235265 auth = "derivatives" in uri .lower (),
236266 )
237267 )
238268 except JSONDecodeError as exc :
239- LOG .error (f "Could not parse the passed data. { exc } " ) # noqa: G004
269+ LOG .error ("Could not parse the passed data. %s" , exc )
240270 except Exception as exc : # noqa: BLE001
241- LOG .error (f "Exception occurred: { exc } " ) # noqa: G004
271+ LOG .error ("Exception occurred: %s" , exc )
242272 sys .exit (1 )
243273 else :
244274 echo (response )
0 commit comments