Skip to content

Commit b6bcdd7

Browse files
committed
Refactor to move api_budget to root level
1 parent 707a6c6 commit b6bcdd7

File tree

5 files changed

+251
-125
lines changed

5 files changed

+251
-125
lines changed

airbyte_cdk/sources/declarative/declarative_component_schema.yaml

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ properties:
4040
"$ref": "#/definitions/Spec"
4141
concurrency_level:
4242
"$ref": "#/definitions/ConcurrencyLevel"
43+
api_budget:
44+
title: API Budget
45+
description: Defines how many requests can be made to the API in a given time frame. This field accepts either a generic APIBudget or an HTTP-specific configuration (HTTPAPIBudget) to be applied across all streams.
46+
anyOf:
47+
- "$ref": "#/definitions/APIBudget"
48+
- "$ref": "#/definitions/HTTPAPIBudget"
4349
metadata:
4450
type: object
4551
description: For internal Airbyte use only - DO NOT modify manually. Used by consumers of declarative manifests for storing related metadata.
@@ -794,7 +800,7 @@ definitions:
794800
description: This option is used to adjust the upper and lower boundaries of each datetime window to beginning and end of the provided target period (day, week, month)
795801
type: object
796802
required:
797-
- target
803+
- target
798804
properties:
799805
target:
800806
title: Target
@@ -1367,17 +1373,49 @@ definitions:
13671373
additional_properties: true
13681374
APIBudget:
13691375
title: API Budget
1370-
description: Component that defines how many requests can be made to the API in a given time frame.
1376+
description: >
1377+
A generic API budget configuration that defines the policies (rate limiting rules)
1378+
and the maximum number of attempts to acquire a call credit. This budget does not automatically
1379+
update itself based on HTTP response headers.
13711380
type: object
13721381
required:
13731382
- type
1383+
- policies
13741384
properties:
13751385
type:
13761386
type: string
13771387
enum: [APIBudget]
13781388
policies:
13791389
title: Policies
1380-
description: List of policies that define the rate limits for different types of requests.
1390+
description: List of call rate policies that define how many calls are allowed.
1391+
type: array
1392+
items:
1393+
anyOf:
1394+
- "$ref": "#/definitions/FixedWindowCallRatePolicy"
1395+
- "$ref": "#/definitions/MovingWindowCallRatePolicy"
1396+
- "$ref": "#/definitions/UnlimitedCallRatePolicy"
1397+
maximum_attempts_to_acquire:
1398+
title: Maximum Attempts to Acquire
1399+
description: The maximum number of attempts to acquire a call before giving up.
1400+
type: integer
1401+
default: 100000
1402+
additionalProperties: true
1403+
HTTPAPIBudget:
1404+
title: HTTP API Budget
1405+
description: >
1406+
An HTTP-specific API budget that extends APIBudget by updating rate limiting information based
1407+
on HTTP response headers. It extracts available calls and the next reset timestamp from the HTTP responses.
1408+
type: object
1409+
required:
1410+
- type
1411+
- policies
1412+
properties:
1413+
type:
1414+
type: string
1415+
enum: [HTTPAPIBudget]
1416+
policies:
1417+
title: Policies
1418+
description: List of call rate policies that define how many calls are allowed.
13811419
type: array
13821420
items:
13831421
anyOf:
@@ -1386,12 +1424,12 @@ definitions:
13861424
- "$ref": "#/definitions/UnlimitedCallRatePolicy"
13871425
ratelimit_reset_header:
13881426
title: Rate Limit Reset Header
1389-
description: The name of the header that contains the timestamp for when the rate limit will reset.
1427+
description: The HTTP response header name that indicates when the rate limit resets.
13901428
type: string
13911429
default: "ratelimit-reset"
13921430
ratelimit_remaining_header:
13931431
title: Rate Limit Remaining Header
1394-
description: The name of the header that contains the number of remaining requests.
1432+
description: The HTTP response header name that indicates the number of remaining allowed calls.
13951433
type: string
13961434
default: "ratelimit-remaining"
13971435
status_codes_for_ratelimit_hit:
@@ -1505,16 +1543,23 @@ definitions:
15051543
additionalProperties: true
15061544
HttpRequestMatcher:
15071545
title: HTTP Request Matcher
1508-
description: Matches HTTP requests based on method, URL, parameters, and headers.
1546+
description: >
1547+
Matches HTTP requests based on method, base URL, URL path pattern, query parameters, and headers.
1548+
Use `url_base` to specify the scheme and host (without trailing slash) and
1549+
`url_path_pattern` to apply a regex to the request path.
15091550
type: object
15101551
properties:
15111552
method:
15121553
title: Method
15131554
description: The HTTP method to match (e.g., GET, POST).
15141555
type: string
1515-
url:
1516-
title: URL
1517-
description: The URL to match.
1556+
url_base:
1557+
title: URL Base
1558+
description: The base URL (scheme and host, e.g. "https://api.example.com") to match.
1559+
type: string
1560+
url_path_pattern:
1561+
title: URL Path Pattern
1562+
description: A regular expression pattern to match the URL path.
15181563
type: string
15191564
params:
15201565
title: Parameters
@@ -1799,10 +1844,6 @@ definitions:
17991844
- "$ref": "#/definitions/DefaultErrorHandler"
18001845
- "$ref": "#/definitions/CustomErrorHandler"
18011846
- "$ref": "#/definitions/CompositeErrorHandler"
1802-
api_budget:
1803-
title: API Budget
1804-
description: Component that defines how many requests can be made to the API in a given time frame.
1805-
"$ref": "#/definitions/APIBudget"
18061847
http_method:
18071848
title: HTTP Method
18081849
description: The HTTP method used to fetch data from the source (can be GET or POST).

airbyte_cdk/sources/declarative/manifest_declarative_source.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
137137
self._source_config, config
138138
)
139139

140+
api_budget_model = self._source_config.get("api_budget")
141+
if api_budget_model:
142+
self._constructor.set_api_budget(api_budget_model, config)
143+
140144
source_streams = [
141145
self._constructor.create_component(
142146
DeclarativeStreamModel,

airbyte_cdk/sources/declarative/models/declarative_component_schema.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,16 @@ class Config:
664664
method: Optional[str] = Field(
665665
None, description="The HTTP method to match (e.g., GET, POST).", title="Method"
666666
)
667-
url: Optional[str] = Field(None, description="The URL to match.", title="URL")
667+
url_base: Optional[str] = Field(
668+
None,
669+
description='The base URL (scheme and host, e.g. "https://api.example.com") to match.',
670+
title="URL Base",
671+
)
672+
url_path_pattern: Optional[str] = Field(
673+
None,
674+
description="A regular expression pattern to match the URL path.",
675+
title="URL Path Pattern",
676+
)
668677
params: Optional[Dict[str, Any]] = Field(
669678
None, description="The query parameters to match.", title="Parameters"
670679
)
@@ -1799,27 +1808,48 @@ class Config:
17991808
extra = Extra.allow
18001809

18011810
type: Literal["APIBudget"]
1802-
policies: Optional[
1803-
List[
1804-
Union[
1805-
FixedWindowCallRatePolicy,
1806-
MovingWindowCallRatePolicy,
1807-
UnlimitedCallRatePolicy,
1808-
]
1811+
policies: List[
1812+
Union[
1813+
FixedWindowCallRatePolicy,
1814+
MovingWindowCallRatePolicy,
1815+
UnlimitedCallRatePolicy,
18091816
]
18101817
] = Field(
1811-
None,
1812-
description="List of policies that define the rate limits for different types of requests.",
1818+
...,
1819+
description="List of call rate policies that define how many calls are allowed.",
1820+
title="Policies",
1821+
)
1822+
maximum_attempts_to_acquire: Optional[int] = Field(
1823+
100000,
1824+
description="The maximum number of attempts to acquire a call before giving up.",
1825+
title="Maximum Attempts to Acquire",
1826+
)
1827+
1828+
1829+
class HTTPAPIBudget(BaseModel):
1830+
class Config:
1831+
extra = Extra.allow
1832+
1833+
type: Literal["HTTPAPIBudget"]
1834+
policies: List[
1835+
Union[
1836+
FixedWindowCallRatePolicy,
1837+
MovingWindowCallRatePolicy,
1838+
UnlimitedCallRatePolicy,
1839+
]
1840+
] = Field(
1841+
...,
1842+
description="List of call rate policies that define how many calls are allowed.",
18131843
title="Policies",
18141844
)
18151845
ratelimit_reset_header: Optional[str] = Field(
18161846
"ratelimit-reset",
1817-
description="The name of the header that contains the timestamp for when the rate limit will reset.",
1847+
description="The HTTP response header name that indicates when the rate limit resets.",
18181848
title="Rate Limit Reset Header",
18191849
)
18201850
ratelimit_remaining_header: Optional[str] = Field(
18211851
"ratelimit-remaining",
1822-
description="The name of the header that contains the number of remaining requests.",
1852+
description="The HTTP response header name that indicates the number of remaining allowed calls.",
18231853
title="Rate Limit Remaining Header",
18241854
)
18251855
status_codes_for_ratelimit_hit: Optional[List[int]] = Field(
@@ -1867,6 +1897,11 @@ class Config:
18671897
definitions: Optional[Dict[str, Any]] = None
18681898
spec: Optional[Spec] = None
18691899
concurrency_level: Optional[ConcurrencyLevel] = None
1900+
api_budget: Optional[Union[APIBudget, HTTPAPIBudget]] = Field(
1901+
None,
1902+
description="Defines how many requests can be made to the API in a given time frame. This field accepts either a generic APIBudget or an HTTP-specific configuration (HTTPAPIBudget) to be applied across all streams.",
1903+
title="API Budget",
1904+
)
18701905
metadata: Optional[Dict[str, Any]] = Field(
18711906
None,
18721907
description="For internal Airbyte use only - DO NOT modify manually. Used by consumers of declarative manifests for storing related metadata.",
@@ -1893,6 +1928,11 @@ class Config:
18931928
definitions: Optional[Dict[str, Any]] = None
18941929
spec: Optional[Spec] = None
18951930
concurrency_level: Optional[ConcurrencyLevel] = None
1931+
api_budget: Optional[Union[APIBudget, HTTPAPIBudget]] = Field(
1932+
None,
1933+
description="Defines how many requests can be made to the API in a given time frame. This field accepts either a generic APIBudget or an HTTP-specific configuration (HTTPAPIBudget) to be applied across all streams.",
1934+
title="API Budget",
1935+
)
18961936
metadata: Optional[Dict[str, Any]] = Field(
18971937
None,
18981938
description="For internal Airbyte use only - DO NOT modify manually. Used by consumers of declarative manifests for storing related metadata.",
@@ -2104,11 +2144,6 @@ class HttpRequester(BaseModel):
21042144
description="Error handler component that defines how to handle errors.",
21052145
title="Error Handler",
21062146
)
2107-
api_budget: Optional[APIBudget] = Field(
2108-
None,
2109-
description="Component that defines how many requests can be made to the API in a given time frame.",
2110-
title="API Budget",
2111-
)
21122147
http_method: Optional[HttpMethod] = Field(
21132148
HttpMethod.GET,
21142149
description="The HTTP method used to fetch data from the source (can be GET or POST).",

0 commit comments

Comments
 (0)