Skip to content

Commit

Permalink
feature: create treshold endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Sep 29, 2023
1 parent 8d5e6c0 commit 2d7a067
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 15 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(

Check warning on line 18 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L17-L18

Added lines #L17 - L18 were not covered by tests
"Cannot use both 'data' and 'json' arguments simultaneously."
)

response = requests.request(

Check warning on line 22 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L22

Added line #L22 was not covered by tests
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)

Check warning on line 39 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L32-L39

Added lines #L32 - L39 were not covered by tests

return response

Check warning on line 41 in marketplace/clients/base.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/base.py#L41

Added line #L41 was not covered by tests
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 = {

Check warning on line 10 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L10

Added line #L10 was not covered by tests
"client_id": settings.OIDC_RP_CLIENT_ID,
"client_secret": settings.OIDC_RP_CLIENT_SECRET,
"grant_type": "client_credentials",
}
request = self.make_request(

Check warning on line 15 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L15

Added line #L15 was not covered by tests
url=settings.OIDC_OP_TOKEN_ENDPOINT, method="POST", data=data
)

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

Check warning on line 19 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L19

Added line #L19 was not covered by tests

return f"Bearer {token}"

Check warning on line 21 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L21

Added line #L21 was not covered by tests

@property
def headers(self):
return {

Check warning on line 25 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L25

Added line #L25 was not covered by tests
"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()

Check warning on line 34 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L33-L34

Added lines #L33 - L34 were not covered by tests

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

Check warning on line 37 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L37

Added line #L37 was not covered by tests

response = self.make_request(

Check warning on line 39 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L39

Added line #L39 was not covered by tests
url, method="GET", headers=self.authentication_instance.headers
)
return response.json()

Check warning on line 42 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L42

Added line #L42 was not covered by tests

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

Check warning on line 46 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L45-L46

Added lines #L45 - L46 were not covered by tests

response = self.make_request(

Check warning on line 48 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L48

Added line #L48 was not covered by tests
url,
method="PATCH",
headers=self.authentication_instance.headers,
json=payload,
)
return response

Check warning on line 54 in marketplace/clients/flows/client.py

View check run for this annotation

Codecov / codecov/patch

marketplace/clients/flows/client.py#L54

Added line #L54 was not covered by tests
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

Check warning on line 131 in marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py#L130-L131

Added lines #L130 - L131 were not covered by tests

@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)

Check warning on line 143 in marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py#L141-L143

Added lines #L141 - L143 were not covered by tests

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

Check warning on line 146 in marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py#L145-L146

Added lines #L145 - L146 were not covered by tests

return Response(data={"Update has success"})

Check warning on line 148 in marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/catalogs/views/view.py#L148

Added line #L148 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, client):
self.client = client

Check warning on line 34 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L34

Added line #L34 was not covered by tests

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")

Check warning on line 39 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L37-L39

Added lines #L37 - L39 were not covered by tests
Expand Down Expand Up @@ -64,7 +63,11 @@ def disable_catalog(self, catalog):
def get_connected_catalog(self, app):
waba_id = self.get_app_facebook_credentials(app=app).get("wa_waba_id")
response = self.client.get_connected_catalog(waba_id=waba_id)

Check warning on line 65 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L64-L65

Added lines #L64 - L65 were not covered by tests
return response.get("data")[0].get("id") if response else []

if len(response.get("data")) > 0:
return response.get("data")[0].get("id")

Check warning on line 68 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L67-L68

Added lines #L67 - L68 were not covered by tests

return []

Check warning on line 70 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L70

Added line #L70 was not covered by tests

def toggle_cart(self, app, enable=True):
business_phone_number_id = self.get_app_facebook_credentials(app=app).get(

Check warning on line 73 in marketplace/core/types/channels/whatsapp_cloud/services/facebook.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/facebook.py#L73

Added line #L73 was not covered by tests
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

Check warning on line 3 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L3

Added line #L3 was not covered by tests

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"]

Check warning on line 11 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L9-L11

Added lines #L9 - L11 were not covered by tests

self.client.update_config(

Check warning on line 13 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L13

Added line #L13 was not covered by tests
data=flows_config, flow_object_uuid=app.flow_object_uuid
)

return True

Check warning on line 17 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L17

Added line #L17 was not covered by tests

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

Check warning on line 21 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L20-L21

Added lines #L20 - L21 were not covered by tests

return self._update_flows_config(app)

Check warning on line 23 in marketplace/core/types/channels/whatsapp_cloud/services/flows.py

View check run for this annotation

Codecov / codecov/patch

marketplace/core/types/channels/whatsapp_cloud/services/flows.py#L23

Added line #L23 was not covered by tests
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 2d7a067

Please sign in to comment.