Skip to content

Commit

Permalink
[feat] allow disabling of InsecureRequestWarnings from this package
Browse files Browse the repository at this point in the history
  • Loading branch information
aarjaneiro committed Apr 4, 2024
1 parent e8d2188 commit 242e4dc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,12 @@ discount = vc.get_discount(symbol="BTC/CAD")
Note that due to CloudFlare protection, this version of the client attempts to access the API through its
IP address and not the domain name. Should the IP address change, the client will need to be updated.
Moreover, you will receive `InsecureRequestWarning` warnings when using the client until this issue is resolved.
To suppress these warnings, you can use the following code:
To suppress these warnings, you can use the following to disable such warnings from being emitted by this package:

```python
import urllib3
from vcx_py import STOP_URLLIB_INSECURE_WARN

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
```

or you can use the environment variable `PYTHONWARNINGS` to suppress the warnings:

```bash
export PYTHONWARNINGS="ignore:Unverified HTTPS request"
STOP_URLLIB_INSECURE_WARN.set()
```

## Paused Trading
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='vcx_py',
version='1.1.4',
version='1.2.0',
packages=['vcx_py'],
license='MIT',
author='Aaron Janeiro Stone',
Expand Down
5 changes: 3 additions & 2 deletions vcx_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
# SOFTWARE.

from .client import VirgoCXClient
from .constants import KLineType, OrderStatus, OrderType, OrderDirection, Enums
from .constants import KLineType, OrderStatus, OrderType, OrderDirection, Enums, STOP_URLLIB_INSECURE_WARN
from .utils import VirgoCXException

__all__ = ["VirgoCXClient", "VirgoCXException", "KLineType", "OrderStatus", "OrderType", "OrderDirection", "Enums"]
__all__ = ["VirgoCXClient", "VirgoCXException", "KLineType", "OrderStatus", "OrderType", "OrderDirection", "Enums",
"STOP_URLLIB_INSECURE_WARN"]
11 changes: 10 additions & 1 deletion vcx_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .constants import ROOT_ADDRESS, VERIFICATION, KLineType, OrderStatus, \
OrderType, OrderDirection
from .utils import VirgoCXWarning, VirgoCXException, vcx_sign, result_formatter
from .utils import VirgoCXWarning, VirgoCXException, vcx_sign, result_formatter, maybe_suppress_insecure


class VirgoCXClient:
Expand Down Expand Up @@ -44,6 +44,7 @@ def signer(dct: dict):
"min_total": v["minTotal"]} for v in
self.tickers()}

@maybe_suppress_insecure
@result_formatter()
def kline(self, symbol: str, period: KLineType):
"""
Expand All @@ -57,6 +58,7 @@ def kline(self, symbol: str, period: KLineType):
return rq.get(f"{self.ENDPOINT}/market/history/kline", params={"symbol": symbol, "period": period},
verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def ticker(self, symbol: str):
"""
Expand All @@ -66,13 +68,15 @@ def ticker(self, symbol: str):
"""
return rq.get(f"{self.ENDPOINT}/market/detail/merged", params={"symbol": symbol}, verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def tickers(self):
"""
Returns the ticker data for all symbols.
"""
return rq.get(f"{self.ENDPOINT}/market/tickers", verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def account_info(self):
"""
Expand All @@ -82,6 +86,7 @@ def account_info(self):
payload["sign"] = self.signer(payload)
return rq.get(f"{self.ENDPOINT}/member/accounts", params=payload, verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def query_orders(self, symbol: str, status: Optional[OrderStatus] = None):
"""
Expand All @@ -98,6 +103,7 @@ def query_orders(self, symbol: str, status: Optional[OrderStatus] = None):
payload["sign"] = self.signer(payload)
return rq.get(f"{self.ENDPOINT}/member/queryOrder", params=payload, verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter(False)
def query_trades(self, symbol: str):
"""
Expand All @@ -109,6 +115,7 @@ def query_trades(self, symbol: str):
payload["sign"] = self.signer(payload)
return rq.get(f"{self.ENDPOINT}/member/queryTrade", params=payload, verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def place_order(self, symbol: str, category: OrderType, direction: OrderDirection,
price: Optional[float] = None, qty: Optional[float] = None,
Expand Down Expand Up @@ -213,6 +220,7 @@ def __extract_market_price__(self, direction, symbol):
market_price = float(market_price["Bid"])
return market_price

@maybe_suppress_insecure
@result_formatter()
def cancel_order(self, order_id: str):
"""
Expand All @@ -224,6 +232,7 @@ def cancel_order(self, order_id: str):
payload["sign"] = self.signer(payload)
return rq.post(f"{self.ENDPOINT}/member/cancelOrder", data=payload, verify=VERIFICATION)

@maybe_suppress_insecure
@result_formatter()
def get_discount(self, symbol: Optional[str] = None):
"""
Expand Down
30 changes: 28 additions & 2 deletions vcx_py/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,40 @@

from enum import IntEnum


class _Flag:
"""A simple flag class."""

def __init__(self, value):
self._value = value

def set(self):
self._value = True

def unset(self):
self._value = False

def __bool__(self):
return self._value


# Because CloudFlare is may block when accessed via the domain name, the IP address is used instead for now.
ROOT_ADDRESS = "https://3.98.238.66/api"
"""The root address of the VirgoCX API."""

# Once the CloudFlare issue is resolved, the domain name will be used instead of the IP and the following
# can be set to True.
VERIFICATION = False
"""Whether or not to verify the SSL certificate of the VirgoCX API."""
VERIFICATION = _Flag(False)
"""
Whether to verify the SSL certificate of the VirgoCX API.
Use the `.set()` method to activate this feature.
"""

STOP_URLLIB_INSECURE_WARN = _Flag(False)
"""
Whether to suppress the urllib3 InsecureRequestWarning warning within this package.
Use the `.set()` method to activate this feature.
"""


class KLineType(IntEnum):
Expand Down
22 changes: 21 additions & 1 deletion vcx_py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,28 @@

from hashlib import md5
from typing import Union
import warnings

from vcx_py.constants import TYPICAL_KEY_TO_ENUM, ATYPICAL_KEY_TO_ENUM
import urllib3 as url # noqa (already comes with requests)

from vcx_py.constants import TYPICAL_KEY_TO_ENUM, ATYPICAL_KEY_TO_ENUM, STOP_URLLIB_INSECURE_WARN


def maybe_suppress_insecure(fn: callable) -> callable:
"""
Potentially suppresses urllib3 InsecureRequestWarning warnings.
This only suppresses the warning within the context of this package.
"""

def inner(*args, **kwargs):
if STOP_URLLIB_INSECURE_WARN:
with warnings.catch_warnings():
warnings.simplefilter("ignore", url.exceptions.InsecureRequestWarning)
return fn(*args, **kwargs)
return fn(*args, **kwargs)

return inner


class VirgoCXWarning(Warning):
Expand Down

0 comments on commit 242e4dc

Please sign in to comment.