Skip to content

Instantly share code, notes, and snippets.

@Canx
Last active May 17, 2024 12:25
Show Gist options
  • Save Canx/561504f7e17f23af65d803b3b2b7e8ec to your computer and use it in GitHub Desktop.
Save Canx/561504f7e17f23af65d803b3b2b7e8ec to your computer and use it in GitHub Desktop.
import time
from polygon import RESTClient
def apply_sleep_to_polygon_api_calls(sleep=60):
from polygon.rest import BaseClient
# Save a reference to the original method
original_get = BaseClient._get
# Define a new version of the _get method
def new_get(self, path, params=None, result_key=None, deserializer=None, raw=False, options=None):
# Add delay
time.sleep(sleep)
# Call the original method
return original_get(self, path, params, result_key, deserializer, raw, options)
# Replace the original method with the new version
BaseClient._get = new_get
# Call this function before using RESTClient
PAID_POLYGON = False
if no PAID_POLYGON:
apply_sleep_to_polygon_api_calls()
# Now you can use RESTClient and the _get method will have the delay
client = RESTClient(api_key="YOUR_API_KEY")
response = client.list_options_contracts(ticker="AAPL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment