airbyte-source-microsoft-dataverse 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.1
2
+ Name: airbyte-source-microsoft-dataverse
3
+ Version: 0.1.2
4
+ Summary: Source implementation for Microsoft Dataverse.
5
+ Author: Airbyte
6
+ Author-email: contact@airbyte.io
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: airbyte-cdk ~=0.2
9
+ Provides-Extra: tests
10
+ Requires-Dist: requests-mock ~=1.9.3 ; extra == 'tests'
11
+ Requires-Dist: pytest ~=6.1 ; extra == 'tests'
12
+ Requires-Dist: pytest-mock ~=3.6.1 ; extra == 'tests'
13
+
14
+ # Microsoft Dataverse Source
15
+
16
+ This is the repository for the Microsoft Dataverse source connector, written in Python.
17
+ For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/sources/microsoft-dataverse).
18
+
19
+
20
+ **To iterate on this connector, make sure to complete this prerequisites section.**
21
+
22
+
23
+ From this connector directory, create a virtual environment:
24
+ ```
25
+ python -m venv .venv
26
+ ```
27
+
28
+ This will generate a virtualenv for this module in `.venv/`. Make sure this venv is active in your
29
+ development environment of choice. To activate it from the terminal, run:
30
+ ```
31
+ source .venv/bin/activate
32
+ pip install -r requirements.txt
33
+ pip install '.[tests]'
34
+ ```
35
+ If you are in an IDE, follow your IDE's instructions to activate the virtualenv.
36
+
37
+ Note that while we are installing dependencies from `requirements.txt`, you should only edit `setup.py` for your dependencies. `requirements.txt` is
38
+ used for editable installs (`pip install -e`) to pull in Python dependencies from the monorepo and will call `setup.py`.
39
+ If this is mumbo jumbo to you, don't worry about it, just put your deps in `setup.py` but install using `pip install -r requirements.txt` and everything
40
+ should work as you expect.
41
+
42
+ **If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/microsoft-dataverse)
43
+ to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_microsoft_dataverse/spec.yaml` file.
44
+ Note that any directory named `secrets` is gitignored across the entire Airbyte repo, so there is no danger of accidentally checking in sensitive information.
45
+ See `integration_tests/sample_config.json` for a sample config file.
46
+
47
+ **If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `source microsoft-dataverse test creds`
48
+ and place them into `secrets/config.json`.
49
+
50
+ ```
51
+ python main.py spec
52
+ python main.py check --config secrets/config.json
53
+ python main.py discover --config secrets/config.json
54
+ python main.py read --config secrets/config.json --catalog integration_tests/configured_catalog.json
55
+ ```
56
+
57
+
58
+
59
+ **Via [`airbyte-ci`](https://github.com/airbytehq/airbyte/blob/master/airbyte-ci/connectors/pipelines/README.md) (recommended):**
60
+ ```bash
61
+ airbyte-ci connectors --name=source-microsoft-dataverse build
62
+ ```
63
+
64
+ An image will be built with the tag `airbyte/source-microsoft-dataverse:dev`.
65
+
66
+ **Via `docker build`:**
67
+ ```bash
68
+ docker build -t airbyte/source-microsoft-dataverse:dev .
69
+ ```
70
+
71
+ Then run any of the connector commands as follows:
72
+ ```
73
+ docker run --rm airbyte/source-microsoft-dataverse:dev spec
74
+ docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-microsoft-dataverse:dev check --config /secrets/config.json
75
+ docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-microsoft-dataverse:dev discover --config /secrets/config.json
76
+ docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/source-microsoft-dataverse:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json
77
+ ```
78
+
79
+ You can run our full test suite locally using [`airbyte-ci`](https://github.com/airbytehq/airbyte/blob/master/airbyte-ci/connectors/pipelines/README.md):
80
+ ```bash
81
+ airbyte-ci connectors --name=source-microsoft-dataverse test
82
+ ```
83
+
84
+ Customize `acceptance-test-config.yml` file to configure tests. See [Connector Acceptance Tests](https://docs.airbyte.com/connector-development/testing-connectors/connector-acceptance-tests-reference) for more information.
85
+ If your connector requires to create or destroy resources for use during acceptance tests create fixtures for it and place them inside integration_tests/acceptance.py.
86
+
87
+ All of your dependencies should go in `setup.py`, NOT `requirements.txt`. The requirements file is only used to connect internal Airbyte dependencies in the monorepo for local development.
88
+ We split dependencies between two groups, dependencies that are:
89
+ * required for your connector to work need to go to `MAIN_REQUIREMENTS` list.
90
+ * required for the testing need to go to `TEST_REQUIREMENTS` list
91
+
92
+ You've checked out the repo, implemented a million dollar feature, and you're ready to share your changes with the world. Now what?
93
+ 1. Make sure your changes are passing our test suite: `airbyte-ci connectors --name=source-microsoft-dataverse test`
94
+ 2. Bump the connector version in `metadata.yaml`: increment the `dockerImageTag` value. Please follow [semantic versioning for connectors](https://docs.airbyte.com/contributing-to-airbyte/resources/pull-requests-handbook/#semantic-versioning-for-connectors).
95
+ 3. Make sure the `metadata.yaml` content is up to date.
96
+ 4. Make the connector documentation and its changelog is up to date (`docs/integrations/sources/microsoft-dataverse.md`).
97
+ 5. Create a Pull Request: use [our PR naming conventions](https://docs.airbyte.com/contributing-to-airbyte/resources/pull-requests-handbook/#pull-request-title-convention).
98
+ 6. Pat yourself on the back for being an awesome contributor.
99
+ 7. Someone from Airbyte will take a look at your PR and iterate with you to merge it into master.
@@ -0,0 +1,23 @@
1
+ integration_tests/__init__.py,sha256=4Hw-PX1-VgESLF16cDdvuYCzGJtHntThLF4qIiULWeo,61
2
+ integration_tests/abnormal_state.json,sha256=F6qYeCLpsXDWSWOK-t622dDz1MP7CPXE2Y-9SkoR-hk,82
3
+ integration_tests/acceptance.py,sha256=8eU9iSDbmHyufPvAouJGhPMgPAFTCP8IKIKHLm7u5TE,435
4
+ integration_tests/configured_catalog.json,sha256=rcfE_cGCXXSimwPslZwhSInofVYvIeIBVi_4wENH27I,347
5
+ integration_tests/invalid_config.json,sha256=SudY8kGqvqyBULe1hYP46cIs8yKkLzTBEzVi_VNLvD8,161
6
+ integration_tests/sample_config.json,sha256=uDZn5UrxyI_viuPhwIsldysAIOk5p4G0VF75hbsbEbs,154
7
+ integration_tests/sample_state.json,sha256=DQ4W-RqAart9346ugzSN0d8hg87VK8Pi5BTaelkB9MI,82
8
+ source_microsoft_dataverse/__init__.py,sha256=xy7FvZqZBBL0_I7mMd5ex5llLWTllG3HRt836ktTD7Q,148
9
+ source_microsoft_dataverse/dataverse.py,sha256=HOQoA571fpy0Bxk4W9l3DttxX34sKkOjTWYeuwZ0uUA,2521
10
+ source_microsoft_dataverse/run.py,sha256=hPGT5kR0LB6ZdKtLmwfLHymdF4EsDaFM2EC4J5ZAr3A,267
11
+ source_microsoft_dataverse/source.py,sha256=7Z6f-FE9P2ycZlgdo5fVNYTlVKjKBiKqll3CMjp8IW4,4435
12
+ source_microsoft_dataverse/spec.yaml,sha256=GmPlzh34HTII0aUW9XUbLxUk3VPEdvQ6E4iIKz4YMiA,1147
13
+ source_microsoft_dataverse/streams.py,sha256=3PhAYr4kM2GjW4jPd-XGIwg0jqiECUyKUK9AJB9JQiw,6168
14
+ unit_tests/__init__.py,sha256=4Hw-PX1-VgESLF16cDdvuYCzGJtHntThLF4qIiULWeo,61
15
+ unit_tests/test_dataverse.py,sha256=yD1tuGjn_WcAFOcx-1Dp7SKTPfVQs_U0ANENd1fVyNE,525
16
+ unit_tests/test_incremental_streams.py,sha256=gCr6fJb_b5U_iaQNRVGL0r5l0LM8GrKsIfj_Uz0pC90,3713
17
+ unit_tests/test_source.py,sha256=yrVHvsNDgj5SPCG1T86j9Xa1Mb0HnIp-gG4xRGzY9wc,5019
18
+ unit_tests/test_streams.py,sha256=jb4YG5e-V4KR2OZywE02KdRE92zQNENH5Q25zOUBsCs,3716
19
+ airbyte_source_microsoft_dataverse-0.1.2.dist-info/METADATA,sha256=O2CwhyE4Q7pF6Gtv5oJz84cO8d9bNWoJKek5FMQijJA,5613
20
+ airbyte_source_microsoft_dataverse-0.1.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
21
+ airbyte_source_microsoft_dataverse-0.1.2.dist-info/entry_points.txt,sha256=uHN7NStTqslpZvvIOhjbOBOlLEmAQUFa6wj1zIxiazE,82
22
+ airbyte_source_microsoft_dataverse-0.1.2.dist-info/top_level.txt,sha256=beuN6fxCEh7L4-h-9GvOc9cvtPyawVZBAhkxIAhs52c,56
23
+ airbyte_source_microsoft_dataverse-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.42.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ source-microsoft-dataverse = source_microsoft_dataverse.run:run
@@ -0,0 +1,3 @@
1
+ integration_tests
2
+ source_microsoft_dataverse
3
+ unit_tests
@@ -0,0 +1,3 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
@@ -0,0 +1,5 @@
1
+ {
2
+ "<entity_name>": {
3
+ "$deltatoken": "12644418993!10/06/2050 20:06:12"
4
+ }
5
+ }
@@ -0,0 +1,16 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+
6
+ import pytest
7
+
8
+ pytest_plugins = ("connector_acceptance_test.plugin",)
9
+
10
+
11
+ @pytest.fixture(scope="session", autouse=True)
12
+ def connector_setup():
13
+ """This fixture is a placeholder for external resources that acceptance test might require."""
14
+ # TODO: setup test dependencies if needed. otherwise remove the TODO comments
15
+ yield
16
+ # TODO: clean up test dependencies
@@ -0,0 +1,15 @@
1
+ {
2
+ "streams": [
3
+ {
4
+ "stream": {
5
+ "name": "<entity_name>",
6
+ "json_schema": {},
7
+ "supported_sync_modes": ["full_refresh", "incremental"]
8
+ },
9
+ "cursor_field": ["modifiedon"],
10
+ "primary_key": [["<entity_primary_key>"]],
11
+ "sync_mode": "incremental",
12
+ "destination_sync_mode": "append"
13
+ }
14
+ ]
15
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "client_id": "<client_id>",
3
+ "tenant_id": "<tenant_id>",
4
+ "client_secret_value": "<invalid_client_secret>",
5
+ "url": "<url>",
6
+ "odata.maxpagesize": 100
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "url": "<url>",
3
+ "client_id": "<client_id>",
4
+ "tenant_id": "<tenant_id>",
5
+ "client_secret_value": "<client_secret>",
6
+ "odata_maxpagesize": 5000
7
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "<entity_name>": {
3
+ "$deltatoken": "12644418993!10/06/2022 20:06:12"
4
+ }
5
+ }
@@ -0,0 +1,8 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+
6
+ from .source import SourceMicrosoftDataverse
7
+
8
+ __all__ = ["SourceMicrosoftDataverse"]
@@ -0,0 +1,80 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ from enum import Enum
6
+ from typing import Any, Mapping, MutableMapping, Optional
7
+
8
+ import requests
9
+ from airbyte_cdk.sources.streams.http.requests_native_auth.oauth import Oauth2Authenticator
10
+
11
+
12
+ class MicrosoftOauth2Authenticator(Oauth2Authenticator):
13
+ def build_refresh_request_body(self) -> Mapping[str, Any]:
14
+ """
15
+ Returns the request body to set on the refresh request
16
+ """
17
+ payload: MutableMapping[str, Any] = {
18
+ "grant_type": "client_credentials",
19
+ "client_id": self.get_client_id(),
20
+ "client_secret": self.get_client_secret(),
21
+ "scope": self.get_scopes(),
22
+ }
23
+
24
+ return payload
25
+
26
+
27
+ class AirbyteType(Enum):
28
+
29
+ String = {"type": ["null", "string"]}
30
+ Boolean = {"type": ["null", "boolean"]}
31
+ Timestamp = {"type": ["null", "string"], "format": "date-time", "airbyte_type": "timestamp_with_timezone"}
32
+ Integer = {"type": ["null", "integer"]}
33
+ Number = {"type": ["null", "number"]}
34
+
35
+
36
+ class DataverseType(Enum):
37
+
38
+ String = AirbyteType.String
39
+ Uniqueidentifier = AirbyteType.String
40
+ DateTime = AirbyteType.Timestamp
41
+ Integer = AirbyteType.Integer
42
+ BigInt = AirbyteType.Integer
43
+ Money = AirbyteType.Number
44
+ Boolean = AirbyteType.Boolean
45
+ Double = AirbyteType.Number
46
+ Decimal = AirbyteType.Number
47
+ Status = AirbyteType.Integer
48
+ State = AirbyteType.Integer
49
+ Picklist = AirbyteType.Integer
50
+ Lookup = AirbyteType.String
51
+ Virtual = None
52
+
53
+
54
+ def get_auth(config: Mapping[str, Any]) -> MicrosoftOauth2Authenticator:
55
+ return MicrosoftOauth2Authenticator(
56
+ token_refresh_endpoint=f'https://login.microsoftonline.com/{config["tenant_id"]}/oauth2/v2.0/token',
57
+ client_id=config["client_id"],
58
+ client_secret=config["client_secret_value"],
59
+ scopes=[f'{config["url"]}/.default'],
60
+ refresh_token="",
61
+ )
62
+
63
+
64
+ def do_request(config: Mapping[str, Any], path: str):
65
+ auth = get_auth(config)
66
+ headers = auth.get_auth_header()
67
+ # Call a protected API with the access token.
68
+ return requests.get(
69
+ config["url"] + "/api/data/v9.2/" + path,
70
+ headers=headers,
71
+ )
72
+
73
+
74
+ def convert_dataverse_type(dataverse_type: str) -> Optional[dict]:
75
+ if dataverse_type in DataverseType.__members__:
76
+ enum_type = DataverseType[dataverse_type]
77
+ if enum_type:
78
+ return enum_type.value if enum_type.value is None else enum_type.value.value
79
+
80
+ return AirbyteType.String.value
@@ -0,0 +1,14 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+
6
+ import sys
7
+
8
+ from airbyte_cdk.entrypoint import launch
9
+ from source_microsoft_dataverse import SourceMicrosoftDataverse
10
+
11
+
12
+ def run():
13
+ source = SourceMicrosoftDataverse()
14
+ launch(source, sys.argv[1:])
@@ -0,0 +1,102 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ import logging
6
+ from typing import Any, Iterator, List, Mapping, MutableMapping, Tuple, Union
7
+
8
+ from airbyte_cdk.models import AirbyteCatalog, AirbyteMessage, AirbyteStateMessage, AirbyteStream, ConfiguredAirbyteCatalog, SyncMode
9
+ from airbyte_cdk.sources import AbstractSource
10
+ from airbyte_cdk.sources.streams import Stream
11
+
12
+ from .dataverse import convert_dataverse_type, do_request, get_auth
13
+ from .streams import IncrementalMicrosoftDataverseStream, MicrosoftDataverseStream
14
+
15
+
16
+ class SourceMicrosoftDataverse(AbstractSource):
17
+ def __init__(self):
18
+ self.catalogs = None
19
+
20
+ def discover(self, logger: logging.Logger, config: Mapping[str, Any]) -> AirbyteCatalog:
21
+ response = do_request(config, "EntityDefinitions?$expand=Attributes")
22
+ response_json = response.json()
23
+ streams = []
24
+ for entity in response_json["value"]:
25
+ schema = {"properties": {}}
26
+ for attribute in entity["Attributes"]:
27
+ dataverse_type = attribute["AttributeType"]
28
+ if dataverse_type == "Lookup":
29
+ attribute["LogicalName"] = "_" + attribute["LogicalName"] + "_value"
30
+ attribute_type = convert_dataverse_type(dataverse_type)
31
+
32
+ if not attribute_type:
33
+ continue
34
+
35
+ schema["properties"][attribute["LogicalName"]] = attribute_type
36
+
37
+ if entity["CanChangeTrackingBeEnabled"]["Value"] and entity["ChangeTrackingEnabled"]:
38
+ schema["properties"].update({"_ab_cdc_updated_at": {"type": "string"}, "_ab_cdc_deleted_at": {"type": ["null", "string"]}})
39
+ stream = AirbyteStream(
40
+ name=entity["LogicalName"], json_schema=schema, supported_sync_modes=[SyncMode.full_refresh, SyncMode.incremental]
41
+ )
42
+ if "modifiedon" in schema["properties"]:
43
+ stream.source_defined_cursor = True
44
+ stream.default_cursor_field = ["modifiedon"]
45
+ else:
46
+ stream = AirbyteStream(name=entity["LogicalName"], json_schema=schema, supported_sync_modes=[SyncMode.full_refresh])
47
+
48
+ stream.source_defined_primary_key = [[entity["PrimaryIdAttribute"]]]
49
+ streams.append(stream)
50
+ return AirbyteCatalog(streams=streams)
51
+
52
+ def check_connection(self, logger, config) -> Tuple[bool, any]:
53
+ """
54
+ :param config: the user-input config object conforming to the connector's spec.yaml
55
+ :param logger: logger object
56
+ :return Tuple[bool, any]: (True, None) if the input config can be used to connect to the API successfully, (False, error) otherwise.
57
+ """
58
+ try:
59
+ response = do_request(config, "")
60
+ # Raises an exception for error codes (4xx or 5xx)
61
+ response.raise_for_status()
62
+ return True, None
63
+ except Exception as e:
64
+ return False, e
65
+
66
+ def read(
67
+ self,
68
+ logger: logging.Logger,
69
+ config: Mapping[str, Any],
70
+ catalog: ConfiguredAirbyteCatalog,
71
+ state: Union[List[AirbyteStateMessage], MutableMapping[str, Any]] = None,
72
+ ) -> Iterator[AirbyteMessage]:
73
+ self.catalogs = catalog
74
+ return super().read(logger, config, catalog, state)
75
+
76
+ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
77
+ """
78
+ :param config: A Mapping of the user input configuration as defined in the connector spec.
79
+ """
80
+ auth = get_auth(config)
81
+
82
+ streams = []
83
+ for catalog in self.catalogs.streams:
84
+ response = do_request(config, f"EntityDefinitions(LogicalName='{catalog.stream.name}')")
85
+ response_json = response.json()
86
+
87
+ args = {
88
+ "url": config["url"],
89
+ "stream_name": catalog.stream.name,
90
+ "stream_path": response_json["EntitySetName"],
91
+ "primary_key": catalog.primary_key,
92
+ "schema": catalog.stream.json_schema,
93
+ "odata_maxpagesize": config["odata_maxpagesize"],
94
+ "authenticator": auth,
95
+ }
96
+
97
+ if catalog.sync_mode == SyncMode.incremental:
98
+ streams.append(IncrementalMicrosoftDataverseStream(**args, config_cursor_field=catalog.cursor_field))
99
+ else:
100
+ streams.append(MicrosoftDataverseStream(**args))
101
+
102
+ return streams
@@ -0,0 +1,47 @@
1
+ documentationUrl: https://docs.airbyte.io/integrations/sources/microsoft-dataverse
2
+ connectionSpecification:
3
+ $schema: http://json-schema.org/draft-07/schema#
4
+ title: Microsoft Dataverse Spec
5
+ type: object
6
+ required:
7
+ - url
8
+ - tenant_id
9
+ - client_id
10
+ - client_secret_value
11
+ additionalProperties: true
12
+ properties:
13
+ url:
14
+ type: string
15
+ description: URL to Microsoft Dataverse API
16
+ title: URL
17
+ examples:
18
+ - https://<org-id>.crm.dynamics.com
19
+ order: 0
20
+
21
+ tenant_id:
22
+ type: string
23
+ description: Tenant Id of your Microsoft Dataverse Instance
24
+ title: Tenant Id
25
+ airbyte_secret: true
26
+ order: 1
27
+
28
+ client_id:
29
+ type: string
30
+ description: App Registration Client Id
31
+ title: Client Id
32
+ airbyte_secret: true
33
+ order: 2
34
+
35
+ client_secret_value:
36
+ type: string
37
+ description: App Registration Client Secret
38
+ title: Client Secret
39
+ airbyte_secret: true
40
+ order: 3
41
+
42
+ odata_maxpagesize:
43
+ type: integer
44
+ description: Max number of results per page. Default=5000
45
+ title: Max page size
46
+ default: 5000
47
+ order: 4
@@ -0,0 +1,152 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ from abc import ABC
6
+ from datetime import datetime
7
+ from typing import Any, Iterable, Mapping, MutableMapping, Optional
8
+ from urllib import parse
9
+
10
+ import requests
11
+ from airbyte_cdk.sources.streams import IncrementalMixin
12
+ from airbyte_cdk.sources.streams.http import HttpStream
13
+
14
+
15
+ # Basic full refresh stream
16
+ class MicrosoftDataverseStream(HttpStream, ABC):
17
+
18
+ # Base url will be set by init(), using information provided by the user through config input
19
+ url_base = ""
20
+ primary_key = ""
21
+
22
+ def __init__(self, url, stream_name, stream_path, schema, primary_key, odata_maxpagesize, **kwargs):
23
+ super().__init__(**kwargs)
24
+ self.url_base = url + "/api/data/v9.2/"
25
+ self.stream_name = stream_name
26
+ self.stream_path = stream_path
27
+ self.primary_key = primary_key
28
+ self.schema = schema
29
+ self.odata_maxpagesize = odata_maxpagesize
30
+
31
+ @property
32
+ def name(self) -> str:
33
+ """Source name"""
34
+ return self.stream_name
35
+
36
+ def get_json_schema(self) -> Mapping[str, Any]:
37
+ return self.schema
38
+
39
+ def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
40
+ """
41
+ :param response: the most recent response from the API
42
+ :return If there is another page in the result, a mapping (e.g: dict) containing information needed to query the next page in the response.
43
+ If there are no more pages in the result, return None.
44
+ """
45
+
46
+ response_json = response.json()
47
+
48
+ if "@odata.nextLink" in response_json:
49
+ next_link = response_json["@odata.nextLink"]
50
+ next_link_params = dict(parse.parse_qsl(parse.urlsplit(next_link).query))
51
+ return next_link_params
52
+ else:
53
+ return None
54
+
55
+ def request_params(
56
+ self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, any] = None, next_page_token: Mapping[str, Any] = None
57
+ ) -> MutableMapping[str, Any]:
58
+ """
59
+ :return a dict containing the parameters to be used in the request
60
+ """
61
+ request_params = super().request_params(stream_state)
62
+ # If there is not a nextLink(contains "next_page_token") in the response, means it is the last page.
63
+ # In this case, the deltatoken is passed instead.
64
+ if next_page_token is None:
65
+ request_params.update(stream_state)
66
+ return request_params
67
+ elif next_page_token is not None:
68
+ request_params.update(next_page_token)
69
+ return request_params
70
+
71
+ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
72
+ """
73
+ :return an iterable containing each record in the response
74
+ """
75
+ for result in response.json()["value"]:
76
+ yield result
77
+
78
+ def request_headers(
79
+ self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None
80
+ ) -> Mapping[str, Any]:
81
+ return {
82
+ "Cache-Control": "no-cache",
83
+ "OData-Version": "4.0",
84
+ "Content-Type": "application/json",
85
+ "Prefer": "odata.maxpagesize=" + str(self.odata_maxpagesize),
86
+ }
87
+
88
+ def path(
89
+ self,
90
+ *,
91
+ stream_state: Mapping[str, Any] = None,
92
+ stream_slice: Mapping[str, Any] = None,
93
+ next_page_token: Mapping[str, Any] = None,
94
+ ) -> str:
95
+ return self.stream_path
96
+
97
+
98
+ # Basic incremental stream
99
+ class IncrementalMicrosoftDataverseStream(MicrosoftDataverseStream, IncrementalMixin, ABC):
100
+
101
+ delta_token_field = "$deltatoken"
102
+ state_checkpoint_interval = None # For now we just use the change tracking as state, and it is only emitted on last page
103
+
104
+ def __init__(self, url, stream_name, stream_path, schema, primary_key, odata_maxpagesize, config_cursor_field, **kwargs):
105
+ super().__init__(url, stream_name, stream_path, schema, primary_key, odata_maxpagesize, **kwargs)
106
+ self._cursor_value = None
107
+ self.config_cursor_field = config_cursor_field
108
+
109
+ @property
110
+ def state(self) -> Mapping[str, Any]:
111
+ return {self.delta_token_field: str(self._cursor_value)}
112
+
113
+ @property
114
+ def cursor_field(self) -> str:
115
+ return self.config_cursor_field
116
+
117
+ # Sets the state got by state getter. "value" is the return of state getter -> dict
118
+ @state.setter
119
+ def state(self, value: Mapping[str, Any]):
120
+ self._cursor_value = value[self.delta_token_field]
121
+
122
+ def request_headers(
123
+ self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None
124
+ ) -> Mapping[str, Any]:
125
+ """
126
+ Override to return any non-auth headers. Authentication headers will overwrite any overlapping headers returned from this method.
127
+ """
128
+ request_headers = super().request_headers(stream_state=stream_state)
129
+ request_headers.update(
130
+ {"Prefer": "odata.track-changes," + request_headers["Prefer"]}
131
+ ) # odata.track-changes -> Header that enables change tracking
132
+ return request_headers
133
+
134
+ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
135
+ response_json = response.json()
136
+ if "@odata.deltaLink" in response_json:
137
+ delta_link = response_json["@odata.deltaLink"]
138
+ delta_link_params = dict(parse.parse_qsl(parse.urlsplit(delta_link).query))
139
+ self._cursor_value = delta_link_params[self.delta_token_field]
140
+ for result in response_json["value"]:
141
+ if "@odata.context" in result and result["reason"] == "deleted":
142
+ result.update({self.primary_key[0][0]: result["id"]})
143
+ result.pop("@odata.context", None)
144
+ result.pop("id", None)
145
+ result.pop("reason", None)
146
+ now = datetime.now().isoformat()
147
+ result.update({self.cursor_field[0]: now})
148
+ result.update({"_ab_cdc_deleted_at": now})
149
+ else:
150
+ result.update({"_ab_cdc_updated_at": result[self.cursor_field[0]]})
151
+
152
+ yield result
unit_tests/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
@@ -0,0 +1,15 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ import pytest
6
+ from source_microsoft_dataverse.dataverse import AirbyteType, convert_dataverse_type
7
+
8
+
9
+ @pytest.mark.parametrize(
10
+ "dataverse_type,expected_result",
11
+ [("String", AirbyteType.String.value), ("Integer", AirbyteType.Integer.value), ("Virtual", None), ("Random", AirbyteType.String.value)],
12
+ )
13
+ def test_convert_dataverse_type(dataverse_type, expected_result):
14
+ result = convert_dataverse_type(dataverse_type)
15
+ assert result == expected_result
@@ -0,0 +1,101 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ from unittest.mock import MagicMock
6
+
7
+ from pytest import fixture
8
+ from source_microsoft_dataverse.source import IncrementalMicrosoftDataverseStream
9
+
10
+
11
+ @fixture
12
+ def incremental_config():
13
+ return {
14
+ "url": "http://test-url",
15
+ "stream_name": "test_stream",
16
+ "stream_path": "test_path",
17
+ "primary_key": [["test_primary_key"]],
18
+ "schema": {},
19
+ "odata_maxpagesize": 100,
20
+ "config_cursor_field": ["test_cursor_field"],
21
+ "authenticator": MagicMock(),
22
+ }
23
+
24
+
25
+ @fixture
26
+ def incremental_response(incremental_config):
27
+ return {
28
+ "@odata.deltaLink": f"{incremental_config['url']}?$deltatoken=12644418993%2110%2F06%2F2022%2020%3A06%3A12",
29
+ "value": [
30
+ {"test_primary_key": "pk", "test_cursor_field": "test-date"},
31
+ {"id": "pk2", "@odata.context": "context", "reason": "deleted"},
32
+ ],
33
+ }
34
+
35
+
36
+ def test_primary_key(incremental_config):
37
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
38
+ expected_primary_key = [["test_primary_key"]]
39
+ assert stream.primary_key == expected_primary_key
40
+
41
+
42
+ def test_stream_name(incremental_config):
43
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
44
+ expected_stream_name = "test_stream"
45
+ assert stream.name == expected_stream_name
46
+
47
+
48
+ def test_stream_path(incremental_config):
49
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
50
+ expected_stream_path = "test_path"
51
+ assert stream.path() == expected_stream_path
52
+
53
+
54
+ def test_cursor_field(incremental_config):
55
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
56
+ expected_cursor_field = ["test_cursor_field"]
57
+ assert stream.cursor_field == expected_cursor_field
58
+
59
+
60
+ def test_supports_incremental(incremental_config, mocker):
61
+ mocker.patch.object(IncrementalMicrosoftDataverseStream, "cursor_field", "dummy_field")
62
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
63
+ assert stream.supports_incremental
64
+
65
+
66
+ def test_source_defined_cursor(incremental_config):
67
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
68
+ assert stream.source_defined_cursor
69
+
70
+
71
+ def test_stream_checkpoint_interval(incremental_config):
72
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
73
+ expected_checkpoint_interval = None
74
+ assert stream.state_checkpoint_interval == expected_checkpoint_interval
75
+
76
+
77
+ def test_parse_request(incremental_config, incremental_response, mocker):
78
+ response_mock, datetime_mock = MagicMock(), MagicMock()
79
+ response_mock.json.return_value = incremental_response
80
+ datetime_mock.now.return_value.isoformat.return_value = "test-time"
81
+ mocker.patch("source_microsoft_dataverse.streams.datetime", datetime_mock)
82
+
83
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
84
+
85
+ iterable = stream.parse_response(response_mock)
86
+ iterable_list = list(iterable)
87
+ assert len(iterable_list) == 2
88
+ assert stream.state[stream.delta_token_field] == "12644418993!10/06/2022 20:06:12"
89
+ assert iterable_list[0]["_ab_cdc_updated_at"] == "test-date"
90
+ assert iterable_list[1]["_ab_cdc_deleted_at"] == "test-time"
91
+ assert iterable_list[1][incremental_config["primary_key"][0][0]] == "pk2"
92
+ assert "id" not in iterable_list[1]
93
+ assert "reason" not in iterable_list[1]
94
+ assert "@odata.context" not in iterable_list[1]
95
+
96
+
97
+ def test_request_headers(incremental_config):
98
+ stream = IncrementalMicrosoftDataverseStream(**incremental_config)
99
+ headers = stream.request_headers(stream_state={})
100
+ assert "Prefer" in headers
101
+ assert headers["Prefer"] == "odata.track-changes,odata.maxpagesize=100"
@@ -0,0 +1,148 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ import json
6
+ from unittest import mock
7
+ from unittest.mock import MagicMock
8
+
9
+ from airbyte_cdk.models import SyncMode
10
+ from source_microsoft_dataverse.dataverse import AirbyteType
11
+ from source_microsoft_dataverse.source import SourceMicrosoftDataverse
12
+ from source_microsoft_dataverse.streams import IncrementalMicrosoftDataverseStream, MicrosoftDataverseStream
13
+
14
+
15
+ @mock.patch("source_microsoft_dataverse.source.do_request")
16
+ def test_check_connection(mock_request):
17
+ mock_request.return_value.raise_for_status = lambda: ()
18
+ source = SourceMicrosoftDataverse()
19
+ logger_mock, config_mock = MagicMock(), MagicMock()
20
+ assert source.check_connection(logger_mock, config_mock) == (True, None)
21
+
22
+
23
+ @mock.patch("source_microsoft_dataverse.source.get_auth")
24
+ @mock.patch("source_microsoft_dataverse.source.do_request")
25
+ def test_streams_incremental(mock_get_auth, mock_request):
26
+ streams = MagicMock()
27
+ streams.sync_mode = SyncMode.incremental
28
+ streams.stream.name = "test"
29
+
30
+ catalog = MagicMock()
31
+
32
+ catalog.streams = [streams]
33
+
34
+ config_mock = MagicMock()
35
+ source = SourceMicrosoftDataverse()
36
+ source.catalogs = catalog
37
+
38
+ streams = source.streams(config_mock)
39
+
40
+ expected_streams_number = 1
41
+ assert len(streams) == expected_streams_number
42
+ assert isinstance(streams[0], IncrementalMicrosoftDataverseStream)
43
+ assert streams[0].name == "test"
44
+
45
+
46
+ @mock.patch("source_microsoft_dataverse.source.get_auth")
47
+ @mock.patch("source_microsoft_dataverse.source.do_request")
48
+ def test_streams_full_refresh(mock_get_auth, mock_request):
49
+ streams = MagicMock()
50
+ streams.sync_mode = SyncMode.full_refresh
51
+ streams.stream.name = "test"
52
+
53
+ catalog = MagicMock()
54
+
55
+ catalog.streams = [streams]
56
+
57
+ config_mock = MagicMock()
58
+ source = SourceMicrosoftDataverse()
59
+ source.catalogs = catalog
60
+
61
+ streams = source.streams(config_mock)
62
+
63
+ expected_streams_number = 1
64
+ assert len(streams) == expected_streams_number
65
+ assert isinstance(streams[0], MicrosoftDataverseStream)
66
+ assert streams[0].name == "test"
67
+
68
+
69
+ @mock.patch("source_microsoft_dataverse.source.do_request")
70
+ def test_discover_incremental(mock_request):
71
+ result_json = json.loads(
72
+ """
73
+ {
74
+ "value": [
75
+ {
76
+ "LogicalName": "stream",
77
+ "PrimaryIdAttribute": "primary",
78
+ "ChangeTrackingEnabled": true,
79
+ "CanChangeTrackingBeEnabled": {
80
+ "Value": true
81
+ },
82
+ "Attributes": [
83
+ {
84
+ "LogicalName": "test",
85
+ "AttributeType": "String"
86
+ },
87
+ {
88
+ "LogicalName": "modifiedon",
89
+ "AttributeType": "DateTime"
90
+ }
91
+ ]
92
+ }
93
+ ]
94
+ }
95
+ """
96
+ )
97
+
98
+ mock_request.return_value.status.return_value = 200
99
+ mock_request.return_value.json.return_value = result_json
100
+
101
+ source = SourceMicrosoftDataverse()
102
+ logger_mock, config_mock = MagicMock(), MagicMock()
103
+
104
+ catalog = source.discover(logger_mock, config_mock)
105
+
106
+ assert not {"modifiedon"} ^ set(catalog.streams[0].default_cursor_field)
107
+ assert not {SyncMode.full_refresh, SyncMode.incremental} ^ set(catalog.streams[0].supported_sync_modes)
108
+ assert not {"primary"} ^ set(catalog.streams[0].source_defined_primary_key[0])
109
+ assert catalog.streams[0].json_schema["properties"]["test"] == AirbyteType.String.value
110
+
111
+
112
+ @mock.patch("source_microsoft_dataverse.source.do_request")
113
+ def test_discover_full_refresh(mock_request):
114
+ result_json = json.loads(
115
+ """
116
+ {
117
+ "value": [
118
+ {
119
+ "LogicalName": "stream",
120
+ "PrimaryIdAttribute": "primary",
121
+ "ChangeTrackingEnabled": false,
122
+ "CanChangeTrackingBeEnabled": {
123
+ "Value": false
124
+ },
125
+ "Attributes": [
126
+ {
127
+ "LogicalName": "test",
128
+ "AttributeType": "String"
129
+ }
130
+ ]
131
+ }
132
+ ]
133
+ }
134
+ """
135
+ )
136
+
137
+ mock_request.return_value.status.return_value = 200
138
+ mock_request.return_value.json.return_value = result_json
139
+
140
+ source = SourceMicrosoftDataverse()
141
+ logger_mock, config_mock = MagicMock(), MagicMock()
142
+
143
+ catalog = source.discover(logger_mock, config_mock)
144
+
145
+ assert catalog.streams[0].default_cursor_field is None or len(catalog.streams[0].default_cursor_field) == 0
146
+ assert not {SyncMode.full_refresh} ^ set(catalog.streams[0].supported_sync_modes)
147
+ assert not {"primary"} ^ set(catalog.streams[0].source_defined_primary_key[0])
148
+ assert catalog.streams[0].json_schema["properties"]["test"] == AirbyteType.String.value
@@ -0,0 +1,107 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ from http import HTTPStatus
6
+ from unittest.mock import MagicMock
7
+
8
+ import pytest
9
+ from pytest import fixture
10
+ from source_microsoft_dataverse.source import MicrosoftDataverseStream
11
+
12
+
13
+ @fixture
14
+ def incremental_config():
15
+ return {
16
+ "url": "http://test-url",
17
+ "stream_name": "test_stream",
18
+ "stream_path": "test_path",
19
+ "primary_key": [["test_primary_key"]],
20
+ "schema": {},
21
+ "odata_maxpagesize": 100,
22
+ "authenticator": MagicMock(),
23
+ }
24
+
25
+
26
+ @pytest.mark.parametrize(
27
+ ("inputs", "expected_params"),
28
+ [
29
+ ({"stream_slice": None, "stream_state": {}, "next_page_token": None}, {}),
30
+ ({"stream_slice": None, "stream_state": {}, "next_page_token": {"$skiptoken": "skiptoken"}}, {"$skiptoken": "skiptoken"}),
31
+ ({"stream_slice": None, "stream_state": {"$deltatoken": "delta"}, "next_page_token": None}, {"$deltatoken": "delta"}),
32
+ ],
33
+ )
34
+ def test_request_params(inputs, expected_params, incremental_config):
35
+ stream = MicrosoftDataverseStream(**incremental_config)
36
+ assert stream.request_params(**inputs) == expected_params
37
+
38
+
39
+ @pytest.mark.parametrize(
40
+ ("response_json", "next_page_token"),
41
+ [
42
+ (
43
+ {
44
+ "@odata.nextLink": "https://url?$skiptoken=oEBwdSP6uehIAxQOWq_3Ksh_TLol6KIm3stvdc6hGhZRi1hQ7Spe__dpvm3U4zReE4CYXC2zOtaKdi7KHlUtC2CbRiBIUwOxPKLa"
45
+ },
46
+ {"$skiptoken": "oEBwdSP6uehIAxQOWq_3Ksh_TLol6KIm3stvdc6hGhZRi1hQ7Spe__dpvm3U4zReE4CYXC2zOtaKdi7KHlUtC2CbRiBIUwOxPKLa"},
47
+ ),
48
+ ({"value": []}, None),
49
+ ],
50
+ )
51
+ def test_next_page_token(response_json, next_page_token, incremental_config):
52
+ stream = MicrosoftDataverseStream(**incremental_config)
53
+ response = MagicMock()
54
+ response.json.return_value = response_json
55
+ inputs = {"response": response}
56
+ expected_token = next_page_token
57
+ assert stream.next_page_token(**inputs) == expected_token
58
+
59
+
60
+ def test_parse_response(incremental_config):
61
+ stream = MicrosoftDataverseStream(**incremental_config)
62
+ response = MagicMock()
63
+ response.json.return_value = {"value": [{"test-key": "test-value"}]}
64
+ inputs = {"response": response}
65
+ expected_parsed_object = {"test-key": "test-value"}
66
+ assert next(stream.parse_response(**inputs)) == expected_parsed_object
67
+
68
+
69
+ def test_request_headers(incremental_config):
70
+ stream = MicrosoftDataverseStream(**incremental_config)
71
+ inputs = {"stream_slice": None, "stream_state": None, "next_page_token": None}
72
+ expected_headers = {
73
+ "Cache-Control": "no-cache",
74
+ "OData-Version": "4.0",
75
+ "Content-Type": "application/json",
76
+ "Prefer": "odata.maxpagesize=100",
77
+ }
78
+ assert stream.request_headers(**inputs) == expected_headers
79
+
80
+
81
+ def test_http_method(incremental_config):
82
+ stream = MicrosoftDataverseStream(**incremental_config)
83
+ expected_method = "GET"
84
+ assert stream.http_method == expected_method
85
+
86
+
87
+ @pytest.mark.parametrize(
88
+ ("http_status", "should_retry"),
89
+ [
90
+ (HTTPStatus.OK, False),
91
+ (HTTPStatus.BAD_REQUEST, False),
92
+ (HTTPStatus.TOO_MANY_REQUESTS, True),
93
+ (HTTPStatus.INTERNAL_SERVER_ERROR, True),
94
+ ],
95
+ )
96
+ def test_should_retry(incremental_config, http_status, should_retry):
97
+ response_mock = MagicMock()
98
+ response_mock.status_code = http_status
99
+ stream = MicrosoftDataverseStream(**incremental_config)
100
+ assert stream.should_retry(response_mock) == should_retry
101
+
102
+
103
+ def test_backoff_time(incremental_config):
104
+ response_mock = MagicMock()
105
+ stream = MicrosoftDataverseStream(**incremental_config)
106
+ expected_backoff_time = None
107
+ assert stream.backoff_time(response_mock) == expected_backoff_time