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

Adding MSGraphOperator in Microsoft Azure provider #38111

Merged
merged 172 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
172 commits
Select commit Hold shift + click to select a range
f82717c
refactor: Initial commit contains the new MSGraphOperator
davidblain-infrabel Mar 13, 2024
86643d4
refactor: Extracted common method into Base class for patching airflo…
davidblain-infrabel Mar 13, 2024
6b92195
refactor: Refactored some typing issues related to msgraph
davidblain-infrabel Mar 13, 2024
40c0e0e
Merge branch 'main' into feature/msgraph-operator
dabla Mar 13, 2024
1f4f704
refactor: Added some docstrings and fixed additional typing issues
davidblain-infrabel Mar 13, 2024
dc9df4a
Merge branch 'main' into feature/msgraph-operator
dabla Mar 14, 2024
a32d7df
refactor: Fixed more static checks
davidblain-infrabel Mar 14, 2024
04d6b85
refactor: Added license on top of test serializer and fixed import
davidblain-infrabel Mar 14, 2024
b77e152
Revert "refactor: Added license on top of test serializer and fixed i…
davidblain-infrabel Mar 14, 2024
90c498b
refactor: Added license on top of serializer files and fixed addition…
davidblain-infrabel Mar 14, 2024
07034db
refactor: Added new line at end of json test files
davidblain-infrabel Mar 14, 2024
a03378f
refactor: Try fixing docstrings on operator and serializer
davidblain-infrabel Mar 14, 2024
f1e3414
refactor: Replaced NoneType with None
davidblain-infrabel Mar 14, 2024
3e01b89
refactor: Made type unions Python 3.8 compatible
davidblain-infrabel Mar 14, 2024
d7198db
refactor: Reformatted some files to comply with static checks formatting
davidblain-infrabel Mar 14, 2024
bcaa2ff
refactor: Reformatted base to comply with static checks formatting
davidblain-infrabel Mar 14, 2024
b319be4
refactor: Added msgraph-core dependency to provider.yaml
davidblain-infrabel Mar 14, 2024
9a41911
Merge branch 'main' into feature/msgraph-operator
dabla Mar 14, 2024
7dc3e7e
refactor: Added msgraph integration info to provider.yaml
davidblain-infrabel Mar 14, 2024
24c3f44
refactor: Added init in resources
davidblain-infrabel Mar 14, 2024
4cef94e
fix: Fixed typing of response_handler
davidblain-infrabel Mar 14, 2024
88aa7dc
refactor: Added assertions on conn_id, tenant_id, client_id and clien…
davidblain-infrabel Mar 14, 2024
86b3af1
Merge branch 'main' into feature/msgraph-operator
dabla Mar 14, 2024
c528f50
refactor: Fixed some static checks
davidblain-infrabel Mar 14, 2024
258abd6
Revert "refactor: Added assertions on conn_id, tenant_id, client_id a…
davidblain-infrabel Mar 14, 2024
2304baf
refactor: Changed imports in hook as we don't use mockito anymore we …
davidblain-infrabel Mar 14, 2024
22906cf
refactor: Renamed test methods
davidblain-infrabel Mar 14, 2024
8078c62
refactor: Replace List type with list
davidblain-infrabel Mar 14, 2024
967216e
refactor: Moved docstring as one line
davidblain-infrabel Mar 14, 2024
c8c4671
refactor: Fixed typing for tests and added test for response_handler
davidblain-infrabel Mar 14, 2024
cf7f1d1
Merge branch 'main' into feature/msgraph-operator
dabla Mar 14, 2024
1d6ceea
refactor: Refactored tests
davidblain-infrabel Mar 14, 2024
2d1cc51
fix: Fixed MS Graph logo filename
davidblain-infrabel Mar 15, 2024
730e9ec
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
740c96e
refactor: Fixed additional static checks remarks
davidblain-infrabel Mar 15, 2024
77dfaee
refactor: Added white line in type checking block
davidblain-infrabel Mar 15, 2024
6ffe8a9
refactor: Added msgraph-core dependency to provider_dependencies.json
davidblain-infrabel Mar 15, 2024
71fb85a
refactor: Updated docstring on response handler
davidblain-infrabel Mar 15, 2024
bfd97ce
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
5dd317c
refactor: Moved ResponseHandler and Serializer to triggers module
davidblain-infrabel Mar 15, 2024
04553ca
docs: Added documentation on how to use the MSGraphAsyncOperator
davidblain-infrabel Mar 15, 2024
c0e3d8a
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
6501906
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
d835e34
docs: Fixed END tag in examples
davidblain-infrabel Mar 15, 2024
6a14ebe
refactor: Removed docstring from CallableResponseHandler
davidblain-infrabel Mar 15, 2024
6cbdd94
refactor: Ignore UP031 Use format specifiers instead of percent forma…
davidblain-infrabel Mar 15, 2024
001f492
Revert "refactor: Removed docstring from CallableResponseHandler"
davidblain-infrabel Mar 15, 2024
61a6ec8
refactor: Simplified docstring on CallableResponseHandler
davidblain-infrabel Mar 15, 2024
b2db2e1
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
d44c0eb
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
5beceef
refactor: Updated provider.yaml to add reference of msgraph to how-to…
davidblain-infrabel Mar 15, 2024
9bbd504
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
20262c5
Merge branch 'main' into feature/msgraph-operator
dabla Mar 15, 2024
b2dc00a
Merge branch 'main' into feature/msgraph-operator
dabla Mar 17, 2024
f641847
Merge branch 'main' into feature/msgraph-operator
dabla Mar 18, 2024
19684bd
Merge branch 'main' into feature/msgraph-operator
dabla Mar 18, 2024
256c09d
Merge branch 'main' into feature/msgraph-operator
dabla Mar 18, 2024
7170983
refactor: Updated docstrings on operator and trigger
davidblain-infrabel Mar 18, 2024
d35ee9f
Merge branch 'main' into feature/msgraph-operator
dabla Mar 18, 2024
738622d
Merge branch 'main' into feature/msgraph-operator
dabla Mar 18, 2024
aa09267
refactor: Fixed additional static checks
davidblain-infrabel Mar 18, 2024
8fec9a6
refactor: Ignore UP031 Use format specifiers instead of percent forma…
davidblain-infrabel Mar 19, 2024
9eb9892
Merge branch 'main' into feature/msgraph-operator
dabla Mar 20, 2024
52a2152
refactor: Added param to docstring ResponseHandler
davidblain-infrabel Mar 21, 2024
00b582b
Merge branch 'main' into feature/msgraph-operator
dabla Mar 21, 2024
7728878
Merge branch 'main' into feature/msgraph-operator
dabla Mar 21, 2024
8e535b2
refactor: Updated pyproject.toml as main
davidblain-infrabel Mar 25, 2024
d1d079c
Merge branch 'main' into feature/msgraph-operator
dabla Mar 25, 2024
4b4cb42
Merge branch 'main' into feature/msgraph-operator
dabla Mar 25, 2024
4e19195
refactor: Reformatted docstrings in trigger
davidblain-infrabel Mar 25, 2024
7054dba
Merge branch 'main' into feature/msgraph-operator
dabla Mar 27, 2024
ae7fc03
Merge branch 'main' into feature/msgraph-operator
dabla Mar 27, 2024
ce43306
refactor: Removed unused serialization module
davidblain-infrabel Mar 27, 2024
8a55cd6
fix: Fixed execution of consecutive tasks in execute_operator method
davidblain-infrabel Apr 3, 2024
40a8cce
refactor: Added customizable pagination_function parameter to Operato…
davidblain-infrabel Apr 3, 2024
fe5d41f
Merge branch 'main' into feature/msgraph-operator
dabla Apr 3, 2024
3b09732
refactor: Reformatted operator and trigger
davidblain-infrabel Apr 3, 2024
b28253a
refactor: Added check if query_parameters is not None
davidblain-infrabel Apr 3, 2024
445f5cd
refactor: Removed typing of top and odata_count
davidblain-infrabel Apr 3, 2024
dcd322c
refactor: Ignore type for tenant_id (this is an issue in the ClientSe…
davidblain-infrabel Apr 3, 2024
be9485c
Merge branch 'main' into feature/msgraph-operator
dabla Apr 3, 2024
8fca162
refactor: Changed docstring on MSGraphTrigger
davidblain-infrabel Apr 3, 2024
f3fc9ca
refactor: Changed docstring on MSGraphTrigger
davidblain-infrabel Apr 3, 2024
0fd9f68
Merge branch 'main' into feature/msgraph-operator
dabla Apr 3, 2024
568dffc
refactor: Added docstring to handle_response_async method
davidblain-infrabel Apr 3, 2024
5c25836
Merge branch 'main' into feature/msgraph-operator
dabla Apr 3, 2024
a664867
refactor: Fixed docstring to imperative for handle_response_async method
davidblain-infrabel Apr 3, 2024
ed100cb
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
2f0018c
refactor: Try quoting Sharepoint so it doesn't get spell checked
davidblain-infrabel Apr 4, 2024
fcedbc9
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
6cd7f5e
refactor: Try double quoting Sharepoint so it doesn't get spell checked
davidblain-infrabel Apr 4, 2024
0551bd0
refactor: Always get a new event loop and close it after test is done
davidblain-infrabel Apr 4, 2024
b7b6968
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
a493217
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
45ea05d
refactor: Reordered imports from contextlib
davidblain-infrabel Apr 4, 2024
6d71597
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
a2ba077
refactor: Added Sharepoint to spelling_wordlist.txt
davidblain-infrabel Apr 4, 2024
afaed29
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
ae8a195
refactor: Removed connection-type for KiotaRequestAdapterHook
davidblain-infrabel Apr 4, 2024
deb4fb1
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
a367a79
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
4010b38
refactor: Refactored encoded_query_parameters
davidblain-infrabel Apr 4, 2024
09e478c
refactor: Suppress ImportError
davidblain-infrabel Apr 4, 2024
180b52e
refactor: Added return type to paginate method
davidblain-infrabel Apr 4, 2024
3d3b65b
refactor: Updated paging_function type in MSGraphAsyncOperator
davidblain-infrabel Apr 4, 2024
68814d0
refactor: Pass the method name from method reference instead of hard …
davidblain-infrabel Apr 4, 2024
9f12dca
refactor: Changed return type of paginate method
davidblain-infrabel Apr 4, 2024
b99c104
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
0bc54d3
Merge branch 'main' into feature/msgraph-operator
dabla Apr 4, 2024
3bb64a6
Merge branch 'main' into feature/msgraph-operator
dabla Apr 6, 2024
f9f61ac
Merge branch 'main' into feature/msgraph-operator
dabla Apr 8, 2024
32e9d10
Merge branch 'main' into feature/msgraph-operator
dabla Apr 9, 2024
01f1419
Merge branch 'main' into feature/msgraph-operator
dabla Apr 9, 2024
2600e94
Merge branch 'main' into feature/msgraph-operator
dabla Apr 9, 2024
79c7158
Merge branch 'main' into feature/msgraph-operator
dabla Apr 10, 2024
aa50a99
refactor: Added MSGraphSensor which easily allows us to poll PowerBI …
davidblain-infrabel Apr 10, 2024
c012614
refactor: Moved BytesIO and Context to type checking block for MSGrap…
davidblain-infrabel Apr 10, 2024
ca6f92c
refactor: Added noqa check on pull_execute_complete method of MSGraph…
davidblain-infrabel Apr 10, 2024
981b192
Merge branch 'main' into feature/msgraph-operator
dabla Apr 10, 2024
bf08a93
fix: Fixed test_serialize of TestMSGraphTrigger
davidblain-infrabel Apr 10, 2024
48c4348
refactor: Added docstring to MSGraphSensor and updated the docstring …
davidblain-infrabel Apr 11, 2024
867873e
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
edf1ecd
refactor: Reformatted docstring of MSGraphSensor
davidblain-infrabel Apr 11, 2024
0d955f5
refactor: Added white line at end of status.json file to keep static …
davidblain-infrabel Apr 11, 2024
c42133c
refactor: Removed timeout parameter from constructor MSGraphSensor as…
davidblain-infrabel Apr 11, 2024
229bb60
fix: Added missing return for async_poke in MSGraphSensor
davidblain-infrabel Apr 11, 2024
8c4482a
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
f704593
Revert "refactor: Added noqa check on pull_execute_complete method of…
davidblain-infrabel Apr 11, 2024
e1e2ed4
refactor: Reorganised imports on MSGraphSensor
davidblain-infrabel Apr 11, 2024
b520b95
refactor: Reformatted TestMSGraphSensor
davidblain-infrabel Apr 11, 2024
5b15e5e
refactor: Added MSGraph sensor integration name in provider.yaml
davidblain-infrabel Apr 11, 2024
fdb6f30
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
c939111
refactor: Updated apache-airflow version to at least 2.7.0 in provide…
davidblain-infrabel Apr 11, 2024
192a572
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
5a1bdfd
refactor: Exclude microsoft-azure from compatibility check with airfl…
davidblain-infrabel Apr 11, 2024
21232c8
refactor: Also updated the apache-airflow dependency version from 2.6…
davidblain-infrabel Apr 11, 2024
9f46826
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
732a13b
refactor: Reformatted global_constants.py
davidblain-infrabel Apr 11, 2024
6980e1e
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
8250560
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
7c8064a
refactor: Add logging statements for proxies and authority related stuff
davidblain-infrabel Apr 11, 2024
35915ef
fix: Fixed exclusion of microsoft.azure dependency in global_constant…
davidblain-infrabel Apr 11, 2024
7f75caa
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
2767828
refactor: Some Azure related imports should be ignored when running A…
davidblain-infrabel Apr 11, 2024
a26ffd4
refactor: Import of ADLSListOperator should be ignored when running A…
davidblain-infrabel Apr 11, 2024
e3dbba1
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
1259b32
refactor: Moved optional provider imports that should be ignored when…
davidblain-infrabel Apr 11, 2024
42ceac6
Merge branch 'main' into feature/msgraph-operator
dabla Apr 11, 2024
7317725
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
201f371
refactor: Fixed the event loop closed issue when executing long runni…
davidblain-infrabel Apr 12, 2024
aac5db7
refactor: Extracted reusable mock_context method
davidblain-infrabel Apr 12, 2024
14e9327
refactor: Moved import of Session into type checking block
davidblain-infrabel Apr 12, 2024
aed317a
refactor: Updated the TestMSGraphSensor
davidblain-infrabel Apr 12, 2024
d6f7b45
refactor: Reformatted the mock_context method
davidblain-infrabel Apr 12, 2024
f34aac2
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
693975e
refactor: Try implementing cached connections on MSGraphTrigger
davidblain-infrabel Apr 12, 2024
0f99861
docs: Added example for the MSGraphSensor and additional examples on …
davidblain-infrabel Apr 12, 2024
d9f1d7a
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
e78c1bc
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
78494c7
Revert "refactor: Try implementing cached connections on MSGraphTrigger"
davidblain-infrabel Apr 12, 2024
e571db6
fix: Fixed serialization of event payload as xcom_value for the MSGra…
davidblain-infrabel Apr 12, 2024
c7a06db
refactor: TestMSGraphAsyncOperator should be allowed to run as a db test
davidblain-infrabel Apr 12, 2024
3e508b0
Revert "refactor: TestMSGraphAsyncOperator should be allowed to run a…
davidblain-infrabel Apr 12, 2024
9d5151d
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
9b4b445
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
e318581
refactor: TestMSGraphAsyncOperator should be allowed to run as a db test
davidblain-infrabel Apr 12, 2024
ea3957e
refactor: Also added result_processor to MSGraphSensor
davidblain-infrabel Apr 12, 2024
03d5a04
Merge branch 'main' into feature/msgraph-operator
dabla Apr 12, 2024
1479af7
refactor: Fixed template_fields in operator, trigger and sensor
davidblain-infrabel Apr 12, 2024
cbcee93
Merge branch 'main' into feature/msgraph-operator
dabla Apr 13, 2024
70fa271
Merge branch 'main' into feature/msgraph-operator
dabla Apr 13, 2024
e2457ea
Merge branch 'main' into feature/msgraph-operator
dabla Apr 13, 2024
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
8 changes: 7 additions & 1 deletion airflow/providers/amazon/aws/transfers/azure_blob_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@

from airflow.models import BaseOperator
from airflow.providers.amazon.aws.hooks.s3 import S3Hook
from airflow.providers.microsoft.azure.hooks.wasb import WasbHook

try:
from airflow.providers.microsoft.azure.hooks.wasb import WasbHook
except ModuleNotFoundError as e:
from airflow.exceptions import AirflowOptionalProviderFeatureException

raise AirflowOptionalProviderFeatureException(e)

if TYPE_CHECKING:
from airflow.utils.context import Context
Expand Down
10 changes: 8 additions & 2 deletions airflow/providers/google/cloud/transfers/adls_to_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@
from typing import TYPE_CHECKING, Sequence

from airflow.providers.google.cloud.hooks.gcs import GCSHook, _parse_gcs_url
from airflow.providers.microsoft.azure.hooks.data_lake import AzureDataLakeHook
from airflow.providers.microsoft.azure.operators.adls import ADLSListOperator

try:
from airflow.providers.microsoft.azure.hooks.data_lake import AzureDataLakeHook
from airflow.providers.microsoft.azure.operators.adls import ADLSListOperator
except ModuleNotFoundError as e:
from airflow.exceptions import AirflowOptionalProviderFeatureException

raise AirflowOptionalProviderFeatureException(e)

if TYPE_CHECKING:
from airflow.utils.context import Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@

from airflow.models import BaseOperator
from airflow.providers.google.cloud.hooks.gcs import GCSHook
from airflow.providers.microsoft.azure.hooks.wasb import WasbHook

try:
from airflow.providers.microsoft.azure.hooks.wasb import WasbHook
except ModuleNotFoundError as e:
from airflow.exceptions import AirflowOptionalProviderFeatureException

raise AirflowOptionalProviderFeatureException(e)

if TYPE_CHECKING:
from airflow.utils.context import Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.models import BaseOperator
from airflow.providers.google.cloud.hooks.gcs import GCSHook, _parse_gcs_url, gcs_object_is_directory
from airflow.providers.microsoft.azure.hooks.fileshare import AzureFileShareHook

try:
from airflow.providers.microsoft.azure.hooks.fileshare import AzureFileShareHook
except ModuleNotFoundError as e:
from airflow.exceptions import AirflowOptionalProviderFeatureException

raise AirflowOptionalProviderFeatureException(e)

if TYPE_CHECKING:
from airflow.utils.context import Context
Expand Down
208 changes: 208 additions & 0 deletions airflow/providers/microsoft/azure/hooks/msgraph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
from urllib.parse import urljoin, urlparse

import httpx
from azure.identity import ClientSecretCredential
from httpx import Timeout
from kiota_authentication_azure.azure_identity_authentication_provider import (
AzureIdentityAuthenticationProvider,
)
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
from msgraph_core import GraphClientFactory
from msgraph_core._enums import APIVersion, NationalClouds

from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook

if TYPE_CHECKING:
from kiota_abstractions.request_adapter import RequestAdapter

from airflow.models import Connection


class KiotaRequestAdapterHook(BaseHook):
"""
A Microsoft Graph API interaction hook, a Wrapper around KiotaRequestAdapter.

https://github.com/microsoftgraph/msgraph-sdk-python-core

:param conn_id: The HTTP Connection ID to run the trigger against.
:param timeout: The HTTP timeout being used by the KiotaRequestAdapter (default is None).
When no timeout is specified or set to None then no HTTP timeout is applied on each request.
:param proxies: A Dict defining the HTTP proxies to be used (default is None).
:param api_version: The API version of the Microsoft Graph API to be used (default is v1).
You can pass an enum named APIVersion which has 2 possible members v1 and beta,
or you can pass a string as "v1.0" or "beta".
"""

cached_request_adapters: dict[str, tuple[APIVersion, RequestAdapter]] = {}
default_conn_name: str = "msgraph_default"

def __init__(
self,
conn_id: str = default_conn_name,
timeout: float | None = None,
proxies: dict | None = None,
api_version: APIVersion | str | None = None,
):
super().__init__()
self.conn_id = conn_id
self.timeout = timeout
self.proxies = proxies
self._api_version = self.resolve_api_version_from_value(api_version)

@property
def api_version(self) -> APIVersion:
self.get_conn() # Make sure config has been loaded through get_conn to have correct api version!
return self._api_version

@staticmethod
def resolve_api_version_from_value(
api_version: APIVersion | str, default: APIVersion | None = None
) -> APIVersion:
if isinstance(api_version, APIVersion):
return api_version
return next(
filter(lambda version: version.value == api_version, APIVersion),
default,
)

def get_api_version(self, config: dict) -> APIVersion:
if self._api_version is None:
return self.resolve_api_version_from_value(
api_version=config.get("api_version"), default=APIVersion.v1
)
return self._api_version

@staticmethod
def get_host(connection: Connection) -> str:
if connection.schema and connection.host:
return f"{connection.schema}://{connection.host}"
return NationalClouds.Global.value

@staticmethod
def format_no_proxy_url(url: str) -> str:
if "://" not in url:
url = f"all://{url}"
return url

@classmethod
def to_httpx_proxies(cls, proxies: dict) -> dict:
proxies = proxies.copy()
if proxies.get("http"):
proxies["http://"] = proxies.pop("http")
if proxies.get("https"):
proxies["https://"] = proxies.pop("https")
if proxies.get("no"):
for url in proxies.pop("no", "").split(","):
proxies[cls.format_no_proxy_url(url.strip())] = None
return proxies

@classmethod
def to_msal_proxies(cls, authority: str | None, proxies: dict):
if authority:
no_proxies = proxies.get("no")
if no_proxies:
for url in no_proxies.split(","):
domain_name = urlparse(url).path.replace("*", "")
if authority.endswith(domain_name):
return None
return proxies

def get_conn(self) -> RequestAdapter:
if not self.conn_id:
raise AirflowException("Failed to create the KiotaRequestAdapterHook. No conn_id provided!")

api_version, request_adapter = self.cached_request_adapters.get(self.conn_id, (None, None))

if not request_adapter:
connection = self.get_connection(conn_id=self.conn_id)
client_id = connection.login
client_secret = connection.password
config = connection.extra_dejson if connection.extra else {}
tenant_id = config.get("tenant_id")
api_version = self.get_api_version(config)
host = self.get_host(connection)
base_url = config.get("base_url", urljoin(host, api_version.value))
authority = config.get("authority")
proxies = self.proxies or config.get("proxies", {})
msal_proxies = self.to_msal_proxies(authority=authority, proxies=proxies)
httpx_proxies = self.to_httpx_proxies(proxies=proxies)
scopes = config.get("scopes", ["https://graph.microsoft.com/.default"])
verify = config.get("verify", True)
trust_env = config.get("trust_env", False)
disable_instance_discovery = config.get("disable_instance_discovery", False)
allowed_hosts = (config.get("allowed_hosts", authority) or "").split(",")

self.log.info(
"Creating Microsoft Graph SDK client %s for conn_id: %s",
api_version.value,
self.conn_id,
)
self.log.info("Host: %s", host)
self.log.info("Base URL: %s", base_url)
self.log.info("Tenant id: %s", tenant_id)
self.log.info("Client id: %s", client_id)
self.log.info("Client secret: %s", client_secret)
self.log.info("API version: %s", api_version.value)
self.log.info("Scope: %s", scopes)
self.log.info("Verify: %s", verify)
self.log.info("Timeout: %s", self.timeout)
self.log.info("Trust env: %s", trust_env)
self.log.info("Authority: %s", authority)
self.log.info("Disable instance discovery: %s", disable_instance_discovery)
self.log.info("Allowed hosts: %s", allowed_hosts)
self.log.info("Proxies: %s", proxies)
self.log.info("MSAL Proxies: %s", msal_proxies)
self.log.info("HTTPX Proxies: %s", httpx_proxies)
credentials = ClientSecretCredential(
tenant_id=tenant_id, # type: ignore
client_id=connection.login,
client_secret=connection.password,
authority=authority,
proxies=msal_proxies,
disable_instance_discovery=disable_instance_discovery,
connection_verify=verify,
)
http_client = GraphClientFactory.create_with_default_middleware(
api_version=api_version,
client=httpx.AsyncClient(
proxies=httpx_proxies,
timeout=Timeout(timeout=self.timeout),
verify=verify,
trust_env=trust_env,
),
host=host,
)
auth_provider = AzureIdentityAuthenticationProvider(
credentials=credentials,
scopes=scopes,
allowed_hosts=allowed_hosts,
)
request_adapter = HttpxRequestAdapter(
authentication_provider=auth_provider,
http_client=http_client,
base_url=base_url,
)
self.cached_request_adapters[self.conn_id] = (api_version, request_adapter)
self._api_version = api_version
return request_adapter
Loading
Loading