Skip to content

Commit

Permalink
Merge branch 'feature/wpp-products-catalog-cart' of github.com:weni-a…
Browse files Browse the repository at this point in the history
…i/weni-integrations-engine into develop
  • Loading branch information
elitonzky committed Sep 29, 2023
2 parents 9140f0d + 5a21174 commit bb6090d
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 14 deletions.
26 changes: 20 additions & 6 deletions marketplace/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,37 @@

class RequestClient:
def make_request(
self, url: str, method: str, headers=None, data=None, params=None, files=None
self,
url: str,
method: str,
headers=None,
data=None,
params=None,
files=None,
json=None,
):
if data and json:
raise ValueError(
"Cannot use both 'data' and 'json' arguments simultaneously."
)

response = requests.request(
method=method,
url=url,
headers=headers,
json=data,
json=json,
data=data,
timeout=60,
params=params,
files=files,
)
if response.status_code >= 500:
raise CustomAPIException(status_code=response.status_code)
elif response.status_code >= 400:
raise CustomAPIException(
detail=response.json() if response.text else response.text,
status_code=response.status_code,
)
try:
detail = response.json()
except ValueError:
detail = response.text
raise CustomAPIException(detail=detail, status_code=response.status_code)

return response
54 changes: 54 additions & 0 deletions marketplace/clients/flows/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Client for connection with flows"""


from django.conf import settings
from marketplace.clients.base import RequestClient


class InternalAuthentication(RequestClient):
def __get_module_token(self):
data = {
"client_id": settings.OIDC_RP_CLIENT_ID,
"client_secret": settings.OIDC_RP_CLIENT_SECRET,
"grant_type": "client_credentials",
}
request = self.make_request(
url=settings.OIDC_OP_TOKEN_ENDPOINT, method="POST", data=data
)

token = request.json().get("access_token")

return f"Bearer {token}"

@property
def headers(self):
return {
"Content-Type": "application/json; charset: utf-8",
"Authorization": self.__get_module_token(),
}


class FlowsClient(RequestClient):
def __init__(self):
self.base_url = settings.FLOWS_REST_ENDPOINT
self.authentication_instance = InternalAuthentication()

def detail_channel(self, flow_object_uuid):
url = f"{self.base_url}/api/v2/internals/channel/{str(flow_object_uuid)}"

response = self.make_request(
url, method="GET", headers=self.authentication_instance.headers
)
return response.json()

def update_config(self, data, flow_object_uuid):
payload = {"config": data}
url = f"{self.base_url}/api/v2/internals/channel/{flow_object_uuid}/"

response = self.make_request(
url,
method="PATCH",
headers=self.authentication_instance.headers,
json=payload,
)
return response
11 changes: 10 additions & 1 deletion marketplace/core/types/channels/whatsapp_cloud/catalogs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from marketplace.core.types.channels.whatsapp_cloud.catalogs.views.view import (
CatalogViewSet,
CommerceSettingsViewSet,
TresholdViewset,
)


Expand Down Expand Up @@ -52,4 +53,12 @@
),
]

urlpatterns = catalog_patterns + commerce_settings_patterns
treshold = [
path(
"<uuid:app_uuid>/update-treshold/",
TresholdViewset.as_view({"post": "update_treshold"}),
name="update-treshold",
)
]

urlpatterns = catalog_patterns + commerce_settings_patterns + treshold
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
from marketplace.core.types.channels.whatsapp_cloud.services.facebook import (
FacebookService,
)
from marketplace.core.types.channels.whatsapp_cloud.services.flows import (
FlowsService,
)
from marketplace.wpp_products.models import Catalog
from marketplace.applications.models import App

from marketplace.clients.facebook.client import FacebookClient
from marketplace.clients.flows.client import FlowsClient

from marketplace.wpp_products.serializers import (
CatalogSerializer,
ToggleVisibilitySerializer,
TresholdSerializer,
)


class CatalogBaseViewSet(viewsets.ViewSet):
class BaseViewSet(viewsets.ViewSet):
fb_service_class = FacebookService
fb_client_class = FacebookClient

Expand All @@ -29,15 +36,15 @@ def fb_service(self): # pragma: no cover
self._fb_service = self.fb_service_class(self.fb_client_class())
return self._fb_service


class CatalogViewSet(BaseViewSet):
serializer_class = CatalogSerializer

def _get_catalog(self, catalog_uuid, app_uuid):
return get_object_or_404(
Catalog, uuid=catalog_uuid, app__uuid=app_uuid, app__code="wpp-cloud"
)


class CatalogViewSet(CatalogBaseViewSet):
serializer_class = CatalogSerializer

def retrieve(self, request, app_uuid, catalog_uuid, *args, **kwargs):
catalog = self._get_catalog(catalog_uuid, app_uuid)

Expand Down Expand Up @@ -77,7 +84,7 @@ def disable_catalog(self, request, app_uuid, catalog_uuid, *args, **kwargs):
return Response(response)


class CommerceSettingsViewSet(CatalogBaseViewSet):
class CommerceSettingsViewSet(BaseViewSet):
serializer_class = ToggleVisibilitySerializer

@action(detail=False, methods=["GET"])
Expand Down Expand Up @@ -111,3 +118,31 @@ def get_active_catalog(self, request, app_uuid, *args, **kwargs):
app = get_object_or_404(App, uuid=app_uuid, code="wpp-cloud")
response = self.fb_service.get_connected_catalog(app)
return Response(response)


class TresholdViewset(BaseViewSet):
serializer_class = TresholdSerializer

flows_service_class = FlowsService
flows_client_class = FlowsClient

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._flows_service = None

@property
def flows_service(self): # pragma: no cover
if not self._flows_service:
self._flows_service = self.flows_service_class(self.flows_client_class())
return self._flows_service

@action(detail=True, methods=["POST"])
def update_treshold(self, request, app_uuid, *args, **kwargs):
app = get_object_or_404(App, uuid=app_uuid, code="wpp-cloud")
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)

treshold = serializer.validated_data["treshold"]
self.flows_service.update_treshold(app, treshold)

return Response(data={"Update has success"})
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, client):
self.client = client

def get_app_facebook_credentials(self, app):
print("REAL")
wa_business_id = app.config.get("wa_business_id")
wa_waba_id = app.config.get("wa_waba_id")
wa_phone_number_id = app.config.get("wa_phone_number_id")
Expand Down
23 changes: 23 additions & 0 deletions marketplace/core/types/channels/whatsapp_cloud/services/flows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class FlowsService:
def __init__(self, client):
self.client = client

def _update_flows_config(self, app):
"""
synchronize Flows channel configuration.
"""
detail_channel = self.client.detail_channel(app.flow_object_uuid)
flows_config = detail_channel["config"]
flows_config["treshold"] = app.config["treshold"]

self.client.update_config(
data=flows_config, flow_object_uuid=app.flow_object_uuid
)

return True

def update_treshold(self, app, treshold):
app.config["treshold"] = treshold
app.save()

return self._update_flows_config(app)
4 changes: 4 additions & 0 deletions marketplace/wpp_products/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ class Meta:

class ToggleVisibilitySerializer(serializers.Serializer):
enable = serializers.BooleanField()


class TresholdSerializer(serializers.Serializer):
treshold = serializers.FloatField()

0 comments on commit bb6090d

Please sign in to comment.