Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add logic to slow down api calls to avoid rate limiting #208

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airbyte-integrations/connectors/source-recurly/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from setuptools import find_packages, setup

MAIN_REQUIREMENTS = ["airbyte-cdk~=0.1", "recurly==4.10.0", "requests"]
MAIN_REQUIREMENTS = ["airbyte-cdk==0.67", "recurly==4.10.0", "requests"]

TEST_REQUIREMENTS = [
"pytest~=6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

import re
import time
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, Union

from airbyte_cdk.models import SyncMode
Expand All @@ -17,6 +18,7 @@

BEGIN_TIME_PARAM = "begin_time"
END_TIME_PARAM = "end_time"
RATE_LIMIT_SLEEP_IN_SECS = 0.16
debanjan97 marked this conversation as resolved.
Show resolved Hide resolved

CAMEL_CASE_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")

Expand Down Expand Up @@ -128,11 +130,15 @@ def read_records(
if self.end_time:
params.update({END_TIME_PARAM: self.end_time})

items = getattr(self._client, self.client_method_name)(params=params).items()
pages = getattr(self._client, self.client_method_name)(params=params).pages()

# Call the Recurly client methods
for item in items:
yield self._item_to_dict(item)
for page in pages:
for item in page:
yield self._item_to_dict(item)
# slow down the API calls to avoid rate limiting
# https://recurly.com/developers/api-v2/v2.2/#section/Rate-Limits
time.sleep(RATE_LIMIT_SLEEP_IN_SECS)

def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]):
"""
Expand Down Expand Up @@ -205,9 +211,12 @@ def read_records(
# and log a warn
try:
for account in accounts:
items = getattr(self._client, self.client_method_name)(params=params, account_id=account.id).items()
for item in items:
yield self._item_to_dict(item)
pages = getattr(self._client, self.client_method_name)(params=params, account_id=account.id).pages()
for page in pages:
for item in page:
yield self._item_to_dict(item)
# slow down the API calls to avoid rate limiting
time.sleep(RATE_LIMIT_SLEEP_IN_SECS)
except MissingFeatureError as error:
super().logger.warning(f"Missing feature error {error}")

Expand Down Expand Up @@ -330,8 +339,10 @@ def read_records(

for coupon in coupons:
try:
items = self._client.list_unique_coupon_codes(params=params, coupon_id=coupon.id).items()
for item in items:
pages = self._client.list_unique_coupon_codes(params=params, coupon_id=coupon.id).pages()
for item in pages:
yield self._item_to_dict(item)
# slow down the API calls to avoid rate limiting
time.sleep(RATE_LIMIT_SLEEP_IN_SECS)
except (NotFoundError, ValidationError):
pass
Loading