|
14 | 14 | from __future__ import annotations
|
15 | 15 |
|
16 | 16 | import json
|
17 |
| -from typing import TYPE_CHECKING, Any |
| 17 | +from typing import TYPE_CHECKING, Any, Literal |
18 | 18 |
|
19 | 19 | import airbyte_api
|
| 20 | +import requests |
20 | 21 | from airbyte_api import api, models
|
21 | 22 |
|
22 | 23 | from airbyte.exceptions import (
|
|
26 | 27 | AirbyteMultipleResourcesError,
|
27 | 28 | PyAirbyteInputError,
|
28 | 29 | )
|
| 30 | +from airbyte.secrets.base import SecretString |
| 31 | + |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from collections.abc import Callable |
29 | 35 |
|
30 | 36 |
|
31 | 37 | if TYPE_CHECKING:
|
|
35 | 41 | DestinationConfiguration,
|
36 | 42 | )
|
37 | 43 |
|
38 |
| - from airbyte.secrets.base import SecretString |
39 |
| - |
40 | 44 |
|
41 | 45 | JOB_WAIT_INTERVAL_SECS = 2.0
|
42 | 46 | JOB_WAIT_TIMEOUT_SECS_DEFAULT = 60 * 60 # 1 hour
|
43 | 47 | CLOUD_API_ROOT = "https://api.airbyte.com/v1"
|
| 48 | +"""The Airbyte Cloud API root URL. |
| 49 | +
|
| 50 | +This is the root URL for the Airbyte Cloud API. It is used to interact with the Airbyte Cloud API |
| 51 | +and is the default API root for the `CloudWorkspace` class. |
| 52 | +- https://reference.airbyte.com/reference/getting-started |
| 53 | +""" |
| 54 | +CLOUD_CONFIG_API_ROOT = "https://cloud.airbyte.com/api/v1" |
| 55 | +"""Internal-Use API Root, aka Airbyte "Config API". |
| 56 | +
|
| 57 | +Documentation: |
| 58 | +- https://docs.airbyte.com/api-documentation#configuration-api-deprecated |
| 59 | +- https://github.com/airbytehq/airbyte-platform-internal/blob/master/oss/airbyte-api/server-api/src/main/openapi/config.yaml |
| 60 | +""" |
44 | 61 |
|
45 | 62 |
|
46 | 63 | def status_ok(status_code: int) -> bool:
|
47 | 64 | """Check if a status code is OK."""
|
48 | 65 | return status_code >= 200 and status_code < 300 # noqa: PLR2004 # allow inline magic numbers
|
49 | 66 |
|
50 | 67 |
|
| 68 | +def get_config_api_root(api_root: str) -> str: |
| 69 | + """Get the configuration API root from the main API root.""" |
| 70 | + if api_root == CLOUD_API_ROOT: |
| 71 | + return CLOUD_CONFIG_API_ROOT |
| 72 | + |
| 73 | + raise NotImplementedError("Configuration API root not implemented for this API root.") |
| 74 | + |
| 75 | + |
51 | 76 | def get_airbyte_server_instance(
|
52 | 77 | *,
|
53 | 78 | api_root: str,
|
@@ -78,7 +103,7 @@ def get_workspace(
|
78 | 103 | client_id: SecretString,
|
79 | 104 | client_secret: SecretString,
|
80 | 105 | ) -> models.WorkspaceResponse:
|
81 |
| - """Get a connection.""" |
| 106 | + """Get a workspace object.""" |
82 | 107 | airbyte_instance = get_airbyte_server_instance(
|
83 | 108 | api_root=api_root,
|
84 | 109 | client_id=client_id,
|
@@ -113,7 +138,7 @@ def list_connections(
|
113 | 138 | name: str | None = None,
|
114 | 139 | name_filter: Callable[[str], bool] | None = None,
|
115 | 140 | ) -> list[models.ConnectionResponse]:
|
116 |
| - """Get a connection.""" |
| 141 | + """List connections.""" |
117 | 142 | if name and name_filter:
|
118 | 143 | raise PyAirbyteInputError(message="You can provide name or name_filter, but not both.")
|
119 | 144 |
|
@@ -155,7 +180,7 @@ def list_workspaces(
|
155 | 180 | name: str | None = None,
|
156 | 181 | name_filter: Callable[[str], bool] | None = None,
|
157 | 182 | ) -> list[models.WorkspaceResponse]:
|
158 |
| - """Get a connection.""" |
| 183 | + """List workspaces.""" |
159 | 184 | if name and name_filter:
|
160 | 185 | raise PyAirbyteInputError(message="You can provide name or name_filter, but not both.")
|
161 | 186 |
|
@@ -196,7 +221,7 @@ def list_sources(
|
196 | 221 | name: str | None = None,
|
197 | 222 | name_filter: Callable[[str], bool] | None = None,
|
198 | 223 | ) -> list[models.SourceResponse]:
|
199 |
| - """Get a connection.""" |
| 224 | + """List sources.""" |
200 | 225 | if name and name_filter:
|
201 | 226 | raise PyAirbyteInputError(message="You can provide name or name_filter, but not both.")
|
202 | 227 |
|
@@ -234,7 +259,7 @@ def list_destinations(
|
234 | 259 | name: str | None = None,
|
235 | 260 | name_filter: Callable[[str], bool] | None = None,
|
236 | 261 | ) -> list[models.DestinationResponse]:
|
237 |
| - """Get a connection.""" |
| 262 | + """List destinations.""" |
238 | 263 | if name and name_filter:
|
239 | 264 | raise PyAirbyteInputError(message="You can provide name or name_filter, but not both.")
|
240 | 265 |
|
@@ -720,16 +745,116 @@ def delete_connection(
|
720 | 745 | )
|
721 | 746 |
|
722 | 747 |
|
723 |
| -# Not yet implemented |
| 748 | +# Functions for leveraging the Airbyte Config API (may not be supported or stable) |
| 749 | + |
| 750 | + |
| 751 | +def get_bearer_token( |
| 752 | + *, |
| 753 | + client_id: SecretString, |
| 754 | + client_secret: SecretString, |
| 755 | + api_root: str = CLOUD_API_ROOT, |
| 756 | +) -> SecretString: |
| 757 | + """Get a bearer token. |
| 758 | +
|
| 759 | + https://reference.airbyte.com/reference/createaccesstoken |
| 760 | +
|
| 761 | + """ |
| 762 | + response = requests.post( |
| 763 | + url=api_root + "/applications/token", |
| 764 | + headers={ |
| 765 | + "content-type": "application/json", |
| 766 | + "accept": "application/json", |
| 767 | + }, |
| 768 | + json={ |
| 769 | + "client_id": client_id, |
| 770 | + "client_secret": client_secret, |
| 771 | + }, |
| 772 | + ) |
| 773 | + if not status_ok(response.status_code): |
| 774 | + response.raise_for_status() |
| 775 | + |
| 776 | + return SecretString(response.json()["access_token"]) |
| 777 | + |
| 778 | + |
| 779 | +def _make_config_api_request( |
| 780 | + *, |
| 781 | + api_root: str, |
| 782 | + path: str, |
| 783 | + json: dict[str, Any], |
| 784 | + client_id: SecretString, |
| 785 | + client_secret: SecretString, |
| 786 | +) -> dict[str, Any]: |
| 787 | + config_api_root = get_config_api_root(api_root) |
| 788 | + bearer_token = get_bearer_token( |
| 789 | + client_id=client_id, |
| 790 | + client_secret=client_secret, |
| 791 | + api_root=api_root, |
| 792 | + ) |
| 793 | + headers: dict[str, Any] = { |
| 794 | + "Content-Type": "application/json", |
| 795 | + "Authorization": f"Bearer {bearer_token}", |
| 796 | + "User-Agent": "PyAirbyte Client", |
| 797 | + } |
| 798 | + response = requests.request( |
| 799 | + method="POST", |
| 800 | + url=config_api_root + path, |
| 801 | + headers=headers, |
| 802 | + json=json, |
| 803 | + ) |
| 804 | + if not status_ok(response.status_code): |
| 805 | + try: |
| 806 | + response.raise_for_status() |
| 807 | + except requests.HTTPError as ex: |
| 808 | + raise AirbyteError( |
| 809 | + context={ |
| 810 | + "url": response.request.url, |
| 811 | + "body": response.request.body, |
| 812 | + "response": response.__dict__, |
| 813 | + }, |
| 814 | + ) from ex |
| 815 | + |
| 816 | + return response.json() |
| 817 | + |
| 818 | + |
| 819 | +def check_connector( |
| 820 | + *, |
| 821 | + actor_id: str, |
| 822 | + connector_type: Literal["source", "destination"], |
| 823 | + client_id: SecretString, |
| 824 | + client_secret: SecretString, |
| 825 | + workspace_id: str | None = None, |
| 826 | + api_root: str = CLOUD_API_ROOT, |
| 827 | +) -> tuple[bool, str | None]: |
| 828 | + """Check a source. |
| 829 | +
|
| 830 | + Raises an exception if the check fails. Uses one of these endpoints: |
| 831 | +
|
| 832 | + - /v1/sources/check_connection: https://github.com/airbytehq/airbyte-platform-internal/blob/10bb92e1745a282e785eedfcbed1ba72654c4e4e/oss/airbyte-api/server-api/src/main/openapi/config.yaml#L1409 |
| 833 | + - /v1/destinations/check_connection: https://github.com/airbytehq/airbyte-platform-internal/blob/10bb92e1745a282e785eedfcbed1ba72654c4e4e/oss/airbyte-api/server-api/src/main/openapi/config.yaml#L1995 |
| 834 | + """ |
| 835 | + _ = workspace_id # Not used (yet) |
| 836 | + |
| 837 | + json_result = _make_config_api_request( |
| 838 | + path=f"/{connector_type}s/check_connection", |
| 839 | + json={ |
| 840 | + f"{connector_type}Id": actor_id, |
| 841 | + }, |
| 842 | + api_root=api_root, |
| 843 | + client_id=client_id, |
| 844 | + client_secret=client_secret, |
| 845 | + ) |
| 846 | + result, message = json_result.get("status"), json_result.get("message") |
| 847 | + |
| 848 | + if result == "succeeded": |
| 849 | + return True, None |
724 | 850 |
|
| 851 | + if result == "failed": |
| 852 | + return False, message |
725 | 853 |
|
726 |
| -# def check_source( |
727 |
| -# source_id: str, |
728 |
| -# *, |
729 |
| -# api_root: str, |
730 |
| -# api_key: str, |
731 |
| -# workspace_id: str | None = None, |
732 |
| -# ) -> api.SourceCheckResponse: |
733 |
| -# """Check a source.""" |
734 |
| -# _ = source_id, workspace_id, api_root, api_key |
735 |
| -# raise NotImplementedError |
| 854 | + raise AirbyteError( |
| 855 | + context={ |
| 856 | + "actor_id": actor_id, |
| 857 | + "connector_type": connector_type, |
| 858 | + "response": json_result, |
| 859 | + }, |
| 860 | + ) |
0 commit comments