coinbase-advanced-py icon indicating copy to clipboard operation
coinbase-advanced-py copied to clipboard

Rest API fails to properly utilize the base_url when specified.

Open Cohbra11 opened this issue 10 months ago • 3 comments

This code was taken directly from the tutorial: https://docs.cdp.coinbase.com/advanced-trade/docs/sdk-rest-client-trade

The code works when base_url=base_url is omitted from the RESTClient() class.

Ultimatetly I need to test with the sandbox endpoint https://api-sandbox.coinbase.com

# This is a summary of all the code for this tutorial
import os
from coinbase.rest import RESTClient
from json import dumps
import math

api_key = "organizations/*******************/apiKeys/****************************"
api_secret = "-----BEGIN EC PRIVATE KEY-----\n*************************\n***********************8==\n-----END EC PRIVATE KEY-----\n"
base_url = 'https://api.coinbase.com'

client = RESTClient(api_key=api_key, api_secret=api_secret, **_base_url=base_url_**)

product = client.get_product("BTC-USD")
btc_usd_price = float(product["price"])
adjusted_btc_usd_price = str(math.floor(btc_usd_price - (btc_usd_price * 0.05)))

order = client.limit_order_gtc_buy(
    client_order_id="00000002",
    product_id="BTC-USD",
    base_size="0.0002",
    limit_price=adjusted_btc_usd_price
)

if order['success']:
    order_id = order['success_response']['order_id']
    client.cancel_orders(order_ids=[order_id])
else:
    error_response = order['error_response']
    print(error_response)

Traceback (most recent call last):
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connection.py", line 199, in _new_conn
    sock = connection.create_connection(
        (self._dns_host, self.port),
    ...<2 lines>...
        socket_options=self.socket_options,
    )
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cohbr\AppData\Local\Programs\Python\Python313\Lib\socket.py", line 975, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen
    response = self._make_request(
        conn,
    ...<10 lines>...
        **response_kw,
    )
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connectionpool.py", line 490, in _make_request
    raise new_e
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connectionpool.py", line 466, in _make_request
    self._validate_conn(conn)
    ~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connectionpool.py", line 1095, in _validate_conn
    conn.connect()
    ~~~~~~~~~~~~^^
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connection.py", line 693, in connect
    self.sock = sock = self._new_conn()
                       ~~~~~~~~~~~~~~^^
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connection.py", line 206, in _new_conn
    raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x000001BEF8405400>: Failed to resolve 'https' ([Errno 11001] getaddrinfo failed)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\.......................\.venv\Lib\site-packages\requests\adapters.py", line 667, in send
    resp = conn.urlopen(
        method=request.method,
    ...<9 lines>...
        chunked=chunked,
    )
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\connectionpool.py", line 843, in urlopen
    retries = retries.increment(
        method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
    )
  File "C:\Users\.......................\.venv\Lib\site-packages\urllib3\util\retry.py", line 519, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: /api.coinbase.com/api/v3/brokerage/products/BTC-USD?get_tradability_status=False (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001BEF8405400>: Failed to resolve 'https' ([Errno 11001] getaddrinfo failed)"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\.......................\src\coinbase\coinbase_test.py", line 13, in <module>
    product = client.get_product("BTC-USD")
  File "C:\Users\.......................\.venv\Lib\site-packages\coinbase\rest\products.py", line 83, in get_product
    return GetProductResponse(self.get(endpoint, params=params, **kwargs))
                              ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\.......................\.venv\Lib\site-packages\coinbase\rest\rest_base.py", line 101, in get
    return self.prepare_and_send_request(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        "GET", url_path, params, data=None, public=public
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\.......................\.venv\Lib\site-packages\coinbase\rest\rest_base.py", line 207, in prepare_and_send_request
    return self.send_request(http_method, url_path, params, headers, data=data)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\.......................\.venv\Lib\site-packages\coinbase\rest\rest_base.py", line 220, in send_request
    response = self.session.request(
        http_method,
    ...<4 lines>...
        timeout=self.timeout,
    )
  File "C:\Users\.......................\.venv\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\.......................\.venv\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\.......................\.venv\Lib\site-packages\requests\adapters.py", line 700, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: /api.coinbase.com/api/v3/brokerage/products/BTC-USD?get_tradability_status=False (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001BEF8405400>: Failed to resolve 'https' ([Errno 11001] getaddrinfo failed)"))

Cohbra11 avatar Feb 10 '25 04:02 Cohbra11

Thank you for reporting! If this is an SDK specific issue, we will look into it and get back to you soon. If this is an API related request, report it in our Advanced API Discord instead (use this invite link if it's your first time accessing the Discord).

github-actions[bot] avatar Feb 10 '25 04:02 github-actions[bot]

I'm having the same issue, what is the point in having the base_url parameter if it doesn't get used? Anyways, I hope this can be updated, or this PR can get merged in. https://github.com/coinbase/coinbase-advanced-py/pull/99

JonnyBoy333 avatar Aug 20 '25 13:08 JonnyBoy333

FYI - https://github.com/coinbase/coinbase-advanced-py/issues/104#issuecomment-3502385523

timborden avatar Nov 07 '25 12:11 timborden