-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
15 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
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,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 | ||
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
23 changes: 23 additions & 0 deletions
23
marketplace/core/types/channels/whatsapp_cloud/services/flows.py
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,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) | ||
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