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

Get catalog and template task requests and responses to send to Flows #419

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 27 additions & 1 deletion marketplace/clients/facebook/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time
import json

from requests_toolbelt.utils import dump

from django.conf import settings

from marketplace.clients.base import RequestClient
Expand Down Expand Up @@ -151,7 +153,31 @@ def list_all_catalogs(self, wa_business_id):
all_catalog_ids.append(item["id"])
all_catalogs.append(item)

return all_catalog_ids, all_catalogs
request, response_data = self.request_http_log(response)

return all_catalog_ids, all_catalogs, request, response_data, initial_url

def request_http_log(self, response):
request_delim = ">!>!>! "
response_delim = "<!<!<! "

data = dump.dump_response(
response,
request_prefix=request_delim.encode("utf-8"),
response_prefix=response_delim.encode("utf-8"),
).decode("utf-8")

request_lines = data.split(request_delim)

response_lines = request_lines[-1].split(response_delim)

request_lines[-1] = response_lines[0]
response_lines = response_lines[1:]

request = "".join(request_lines)
response = "".join(response_lines)

return request, response

def destroy_feed(self, feed_id):
url = self.get_url + f"{feed_id}"
Expand Down
22 changes: 18 additions & 4 deletions marketplace/clients/flows/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ def update_vtex_integration_status(self, project_uuid, user_email, action):
)
return True

def update_catalogs(self, flow_object_uuid, catalogs_data):
data = {"data": catalogs_data}
def update_catalogs(
self, flow_object_uuid, catalogs_data, requests_data, response_data, urls_data
):
data = {
"data": catalogs_data,
"requests": requests_data,
"responses": response_data,
"urls": urls_data,
}
url = f"{self.base_url}/catalogs/{flow_object_uuid}/update-catalog/"

response = self.make_request(
Expand Down Expand Up @@ -105,8 +112,15 @@ def update_vtex_products(self, products, flow_object_uuid, dict_catalog):
)
return response

def update_facebook_templates(self, flow_object_uuid, fba_templates):
data = {"data": fba_templates}
def update_facebook_templates(
self, flow_object_uuid, fba_templates, request, response_data, url_request
):
data = {
"data": fba_templates,
"request": request,
"response": response_data,
"url": url_request,
}
url = f"{self.base_url}/template/{flow_object_uuid}/"

response = self.make_request(
Expand Down
23 changes: 19 additions & 4 deletions marketplace/wpp_products/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ def sync_catalogs(self):
local_catalog_ids = set(
self.app.catalogs.values_list("facebook_catalog_id", flat=True)
)
all_catalogs_id, all_catalogs = self._list_all_catalogs()
(
all_catalogs_id,
all_catalogs,
all_requests,
all_responses,
all_urls,
) = self._list_all_catalogs()

if all_catalogs_id:
self._update_catalogs_on_flows(all_catalogs)
self._update_catalogs_on_flows(
all_catalogs, all_requests, all_responses, all_urls
)
self._sync_local_catalogs(all_catalogs_id, local_catalog_ids)
except Exception as e:
logger.error(f"Error during sync process for App {self.app.name}: {e}")
Expand All @@ -82,10 +90,17 @@ def _list_all_catalogs(self):
)
return [], []

def _update_catalogs_on_flows(self, all_catalogs):
def _update_catalogs_on_flows(
self, all_catalogs, all_requests, all_responses, all_urls
):
try:
print("EPAA")
self.flows_client.update_catalogs(
str(self.app.flow_object_uuid), all_catalogs
str(self.app.flow_object_uuid),
all_catalogs,
all_requests,
all_responses,
all_urls,
)
except Exception as e:
logger.error(
Expand Down
13 changes: 10 additions & 3 deletions marketplace/wpp_templates/requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from marketplace.clients.facebook.client import FacebookClient

from marketplace.core.types.channels.whatsapp_base.exceptions import (
FacebookApiException,
Expand All @@ -17,16 +18,22 @@ def __init__(self, access_token: str) -> None:
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self._access_token}"}

def list_template_messages(self, waba_id: str) -> dict:
def list_template_messages(self, waba_id: str) -> tuple:
url = (
f"https://graph.facebook.com/{WHATSAPP_VERSION}/{waba_id}/message_templates"
)
params = dict(
limit=999,
access_token=self._access_token,
)
response = requests.get(
url=f"https://graph.facebook.com/{WHATSAPP_VERSION}/{waba_id}/message_templates",
url,
params=params,
)
return response.json()

request, response_data = FacebookClient.request_http_log(response)

return response.json(), request, response_data, url

def get_template_namespace(self, waba_id: str) -> dict:
params = dict(
Expand Down
11 changes: 9 additions & 2 deletions marketplace/wpp_templates/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def refresh_whatsapp_templates_from_facebook():
access_token = APPTYPES.get("wpp-cloud").get_access_token(app)

template_message_request = TemplateMessageRequest(access_token=access_token)
templates = template_message_request.list_template_messages(waba_id)
(
templates,
request,
response_data,
url,
) = template_message_request.list_template_messages(waba_id)

if templates.get("error"):
logger.error(
Expand All @@ -49,7 +54,9 @@ def refresh_whatsapp_templates_from_facebook():

templates = templates.get("data", [])
try:
flows_client.update_facebook_templates(str(app.flow_object_uuid), templates)
flows_client.update_facebook_templates(
str(app.flow_object_uuid), templates, request, response_data, url
)
except Exception as error:
logger.error(
f"An error occurred when sending facebook templates to flows: "
Expand Down
Loading
Loading