-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get globals data from integrations and flows (#50)
* Get globals data from integrations and flows * Populate globals from services on integrate feature * Return all infos from IntegratedFeatureSerializer after integrate feature
- Loading branch information
Showing
14 changed files
with
256 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# base_service_view.py | ||
from rest_framework import views | ||
|
||
from rest_framework.permissions import IsAuthenticated | ||
|
||
from retail.clients.flows.client import FlowsClient | ||
from retail.clients.integrations.client import IntegrationsClient | ||
from retail.services.flows.service import FlowsService | ||
from retail.services.integrations.service import IntegrationsService | ||
|
||
|
||
class BaseServiceView(views.APIView): | ||
""" | ||
BaseServiceView is a base class that provides common service and client | ||
injection logic for views. Other views should inherit from this class to | ||
reuse the integration and flows service logic. | ||
""" | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
integrations_service_class = IntegrationsService | ||
integrations_client_class = IntegrationsClient | ||
flows_service_class = FlowsService | ||
flows_client_class = FlowsClient | ||
|
||
_integrations_service = None | ||
_flows_service = None | ||
|
||
@property | ||
def integrations_service(self): | ||
if not self._integrations_service: | ||
self._integrations_service = self.integrations_service_class( | ||
self.integrations_client_class() | ||
) | ||
return self._integrations_service | ||
|
||
@property | ||
def flows_service(self): | ||
if not self._flows_service: | ||
self._flows_service = self.flows_service_class(self.flows_client_class()) | ||
return self._flows_service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
PopulateGlobalsValuesUsecase is responsible for filling in specific global keys | ||
in the globals_values dictionary using data fetched from external services. | ||
Attributes: | ||
integrations_service: Service used to fetch data related to VTEX integrations. | ||
flows_service: Service used to fetch user API tokens from the flows system. | ||
""" | ||
|
||
|
||
class PopulateGlobalsValuesUsecase: | ||
def __init__(self, integrations_service, flows_service): | ||
self.integrations_service = integrations_service | ||
self.flows_service = flows_service | ||
|
||
def execute(self, globals_values: dict, user_email: str, project_uuid: str) -> dict: | ||
""" | ||
Fill in the keys of globals_values using the appropriate services. | ||
Only the following keys are manipulated: | ||
- x_vtex_api_appkey | ||
- x_vtex_api_apptoken | ||
- url_api_vtex | ||
- api_token (from flows, mapped to 'token') | ||
""" | ||
filled_globals_values = globals_values.copy() | ||
|
||
# Handle the keys related to the integrations service | ||
integration_data = self.integrations_service.get_vtex_integration_detail( | ||
project_uuid | ||
) | ||
if integration_data: | ||
if "url_api_vtex" in globals_values: | ||
filled_globals_values["url_api_vtex"] = integration_data.get( | ||
"domain", globals_values["url_api_vtex"] | ||
) | ||
if "x_vtex_api_appkey" in globals_values: | ||
filled_globals_values["x_vtex_api_appkey"] = integration_data.get( | ||
"app_key", globals_values["x_vtex_api_appkey"] | ||
) | ||
if "x_vtex_api_apptoken" in globals_values: | ||
filled_globals_values["x_vtex_api_apptoken"] = integration_data.get( | ||
"app_token", globals_values["x_vtex_api_apptoken"] | ||
) | ||
|
||
# Handle the key related to the flows service (api_token) | ||
flow_data = self.flows_service.get_user_api_token(user_email, project_uuid) | ||
if flow_data: | ||
if "api_token" in globals_values: | ||
filled_globals_values["api_token"] = flow_data.get( | ||
"token", globals_values["api_token"] | ||
) | ||
|
||
return filled_globals_values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
RemoveGlobalsKeysUsecase is responsible for removing specific global keys | ||
from the feature's globals list based on data fetched from external services. | ||
Attributes: | ||
integrations_service: Service used to fetch data related to VTEX integrations. | ||
flows_service: Service used to fetch user API tokens from the flows system. | ||
""" | ||
|
||
|
||
class RemoveGlobalsKeysUsecase: | ||
def __init__(self, integrations_service, flows_service): | ||
self.integrations_service = integrations_service | ||
self.flows_service = flows_service | ||
|
||
def execute( | ||
self, features: list[dict], user_email: str, project_uuid: str | ||
) -> list[dict]: | ||
# Fetch data from the services, handling cases where data might be None | ||
integrations_data = self.integrations_service.get_vtex_integration_detail( | ||
project_uuid | ||
) | ||
flows_data = self.flows_service.get_user_api_token(user_email, project_uuid) | ||
|
||
for feature in features: | ||
globals_to_remove = [] | ||
|
||
# Check and mark globals for removal based on integrations data if available | ||
if integrations_data: | ||
if "x_vtex_api_appkey" in feature["globals"] and integrations_data.get( | ||
"app_key" | ||
): | ||
globals_to_remove.append("x_vtex_api_appkey") | ||
if "x_vtex_api_apptoken" in feature[ | ||
"globals" | ||
] and integrations_data.get("app_token"): | ||
globals_to_remove.append("x_vtex_api_apptoken") | ||
if "url_api_vtex" in feature["globals"] and integrations_data.get( | ||
"domain" | ||
): | ||
globals_to_remove.append("url_api_vtex") | ||
|
||
# Check and mark globals for removal based on flows data if available | ||
if flows_data: | ||
if "user_api_token" in feature["globals"] and flows_data.get( | ||
"api_token" | ||
): | ||
globals_to_remove.append("user_api_token") | ||
|
||
# Remove the marked globals | ||
feature["globals"] = [ | ||
g for g in feature["globals"] if g not in globals_to_remove | ||
] | ||
|
||
return features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class FlowsClientInterface(ABC): | ||
@abstractmethod | ||
def get_user_api_token(self, user_email: str, project_uuid: str): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class IntegrationsClientInterface(ABC): | ||
@abstractmethod | ||
def get_vtex_integration_detail(self, project_uuid: str): | ||
pass |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from retail.clients.exceptions import CustomAPIException | ||
from retail.interfaces.clients.flows.interface import FlowsClientInterface | ||
|
||
|
||
class FlowsService: | ||
def __init__(self, client: FlowsClientInterface): | ||
self.client = client | ||
|
||
def get_user_api_token(self, user_email: str, project_uuid: str) -> dict: | ||
""" | ||
Retrieve the user API token for a given email and project UUID. | ||
Handles communication errors and returns None in case of failure. | ||
""" | ||
try: | ||
return self.client.get_user_api_token(user_email, project_uuid) | ||
except CustomAPIException as e: | ||
print(f"Error {e.status_code} when retrieving user API token for project {project_uuid}.") | ||
return None |
Empty file.
Oops, something went wrong.