Skip to content

Commit

Permalink
Address Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SumanMaharana committed Sep 16, 2024
1 parent cad211a commit 2d8c09e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
54 changes: 31 additions & 23 deletions ingestion/src/metadata/ingestion/source/dashboard/sigma/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@
WorkBookPageResponse,
WorkBookResponseDetails,
)
from metadata.utils.constants import AUTHORIZATION_HEADER, UTF_8
from metadata.utils.helpers import clean_uri
from metadata.utils.logger import utils_logger

logger = utils_logger()

HEADERS = {
"accept": "application/json",
"Content-type": "application/x-www-form-urlencoded",
}

TOKEN_PAYLOAD = {"grant_type": "client_credentials"}


class SigmaApiClient:
"""
Expand All @@ -51,21 +59,16 @@ def __init__(self, config: SigmaConnection):
token_api_key = str(
b64encode(
f"{self.config.clientId}:{self.config.clientSecret.get_secret_value()}".encode(
"utf-8"
UTF_8
)
).decode("utf-8")
).decode(UTF_8)
)

headers = {
"accept": "application/json",
"Content-type": "application/x-www-form-urlencoded",
}

token_config = ClientConfig(
base_url=clean_uri(config.hostPort),
api_version=config.apiVersion,
auth_header="Authorization",
extra_headers=headers,
auth_header=AUTHORIZATION_HEADER,
extra_headers=HEADERS,
auth_token=lambda: (token_api_key, 0),
auth_token_mode="Basic",
)
Expand All @@ -76,7 +79,7 @@ def __init__(self, config: SigmaConnection):
base_url=clean_uri(config.hostPort),
api_version=config.apiVersion,
auth_token=self.get_auth_token,
auth_header="Authorization",
auth_header=AUTHORIZATION_HEADER,
)

self.client = REST(client_config)
Expand All @@ -85,15 +88,16 @@ def get_auth_token(self) -> Tuple[str, int]:
"""
generate auth token
"""
payload = {"grant_type": "client_credentials"}
result = AuthToken(**self.token_client.post("/auth/token", data=payload))
result = AuthToken.model_validate(
self.token_client.post("/auth/token", data=TOKEN_PAYLOAD)
)
return result.access_token, 0

def get_dashboards(self) -> Optional[List[Workbook]]:
"""
method to fetch dashboards from api
"""
result = WorkBookResponseDetails(**self.client.get("/workbooks"))
result = WorkBookResponseDetails.model_validate(self.client.get("/workbooks"))
if result:
return result.entries

Expand All @@ -102,7 +106,9 @@ def get_dashboard_detail(self, workbook_id: str) -> Optional[WorkbookDetails]:
method to fetch dashboard details from api
"""
try:
result = WorkbookDetails(**self.client.get(f"/workbooks/{workbook_id}"))
result = WorkbookDetails.model_validate(
self.client.get(f"/workbooks/{workbook_id}")
)
if result:
return result
except Exception as exc: # pylint: disable=broad-except
Expand All @@ -117,7 +123,9 @@ def get_owner_detail(self, owner_id: str) -> Optional[OwnerDetails]:
method to fetch dashboard owner details from api
"""
try:
result = OwnerDetails(**self.client.get(f"/members/{owner_id}"))
result = OwnerDetails.model_validate(
self.client.get(f"/members/{owner_id}")
)
if result:
return result
except Exception as exc: # pylint: disable=broad-except
Expand All @@ -131,12 +139,12 @@ def get_chart_details(self, workbook_id: str) -> Optional[List[Elements]]:
"""
try:
elements_list = []
pages = WorkBookPageResponse(
**self.client.get(f"/workbooks/{workbook_id}/pages")
pages = WorkBookPageResponse.model_validate(
self.client.get(f"/workbooks/{workbook_id}/pages")
)
for page in pages.entries:
elements = ElementsResponse(
**self.client.get(
elements = ElementsResponse.model_validate(
self.client.get(
f"/workbooks/{workbook_id}/pages/{page.pageId}/elements"
)
)
Expand All @@ -157,15 +165,15 @@ def get_lineage_details(
"""
try:
source_nodes = []
edges_response = EdgeSourceResponse(
**self.client.get(
edges_response = EdgeSourceResponse.model_validate(
self.client.get(
f"/workbooks/{workbook_id}/lineage/elements/{element_id}"
)
)
for node in edges_response.edges:
if node.node_id:
node_details = NodeDetails(
**self.client.get(f"/files/{node.node_id}")
node_details = NodeDetails.model_validate(
self.client.get(f"/files/{node.node_id}")
)
source_nodes.append(node_details)
return source_nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def yield_dashboard(
dashboard_request = CreateDashboardRequest(
name=EntityName(str(dashboard_details.workbookId)),
displayName=dashboard_details.name,
description=Markdown(dashboard_details.description),
description=Markdown(dashboard_details.description)
if dashboard_details.description
else None,
charts=[
FullyQualifiedEntityName(
fqn.build(
Expand Down Expand Up @@ -145,7 +147,9 @@ def yield_dashboard_chart(
self.context.get().dashboard_service
),
sourceUrl=SourceUrl(dashboard_details.url),
description=Markdown(dashboard_details.description),
description=Markdown(dashboard_details.description)
if dashboard_details.description
else None,
)
)
except Exception as exc:
Expand Down
20 changes: 10 additions & 10 deletions ingestion/src/metadata/ingestion/source/dashboard/sigma/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ class AuthToken(BaseModel):

class Workbook(BaseModel):
workbookId: str
name: Optional[str] = ""
ownerId: Optional[str] = ""
name: Optional[str] = None
ownerId: Optional[str] = None


class WorkbookDetails(BaseModel):
workbookId: str
name: Optional[str] = ""
name: Optional[str] = None
createdAt: str
url: str
path: Optional[str] = ""
ownerId: Optional[str] = ""
path: Optional[str] = None
ownerId: Optional[str] = None
isArchived: bool
description: Optional[str] = ""
description: Optional[str] = None


class WorkBookResponseDetails(BaseModel):
Expand All @@ -58,16 +58,16 @@ class WorkBookPageResponse(BaseModel):

class Elements(BaseModel):
elementId: str
name: Optional[str] = ""
vizualizationType: Optional[str] = ""
name: Optional[str] = None
vizualizationType: Optional[str] = None


class ElementsResponse(BaseModel):
entries: Optional[List[Elements]] = []


class EdgeSource(BaseModel):
source: Optional[str] = ""
source: str

@property
def node_id(self):
Expand All @@ -84,7 +84,7 @@ class EdgeSourceResponse(BaseModel):
class NodeDetails(BaseModel):
id: str
name: Optional[str]
node_type: Optional[str] = Field("", alias="type")
node_type: str = Field(alias="type")
path: Optional[str] = ""

@property
Expand Down

0 comments on commit 2d8c09e

Please sign in to comment.