Skip to content

Commit

Permalink
Merge pull request #39 from gogovan/twilio
Browse files Browse the repository at this point in the history
updated twilio
  • Loading branch information
ggx-jacechun authored Sep 14, 2023
2 parents 34fe104 + bede1cf commit 1b588c4
Showing 1 changed file with 19 additions and 36 deletions.
55 changes: 19 additions & 36 deletions ggvlib/twilio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@ class ContentType(BaseModel):
def content_validator(cls, values: dict):
if values.get("category") == "list-picker":
if not values.get("items"):
raise ValueError(
"list-picker content requires an 'items' value"
)
raise ValueError("list-picker content requires an 'items' value")
if not values.get("button"):
raise ValueError(
"list-picker content requires an 'button' value"
)
raise ValueError("list-picker content requires an 'button' value")
if values.get("category") in ("quick-reply", "call-to-action"):
if not values.get("actions"):
raise ValueError(
"list-picker content requires an 'items' value"
)
raise ValueError("list-picker content requires an 'items' value")
return values

def to_dict(self) -> dict:
Expand Down Expand Up @@ -94,8 +88,7 @@ def to_dict(cls) -> dict:
**{k: v for k, v in cls.dict().items() if k not in exclude},
**{
"types": {
f"twilio/{t.category}": t.to_dict()
for t in cls.content_types
f"twilio/{t.category}": t.to_dict() for t in cls.content_types
}
},
)
Expand Down Expand Up @@ -149,9 +142,7 @@ def to_dict(cls) -> dict:
content = {to_form_field(k): v for k, v in d.items() if v}
if list(content.keys()) == ["Sid"]:
required = [k for k in d.keys() if k != "sid"]
raise ValueError(
f"One of the following values is required: {required}"
)
raise ValueError(f"One of the following values is required: {required}")
return content


Expand Down Expand Up @@ -229,9 +220,7 @@ def post_urlencoded_form(
if response.status_code == accept_status_code:
return response.json()
else:
raise Exception(
f"Client did not accept request: {response.json()}"
)
raise Exception(f"Client did not accept request: {response.json()}")


class MessagingServiceApiClient(Client):
Expand All @@ -244,9 +233,7 @@ def get_all(self) -> dict:
Returns:
dict: _description_
"""
return requests.get(
url=self.api_base_url, headers=self.json_headers
).json()
return requests.get(url=self.api_base_url, headers=self.json_headers).json()

def get(self, service_sid: str) -> dict:
"""_summary_
Expand Down Expand Up @@ -362,7 +349,13 @@ def async_send_content(
Returns:
List[aiohttp.ClientResponse]: A list of responses from the client
"""
loop = asyncio.get_event_loop()
try:
loop = asyncio.get_event_loop()
except RuntimeError as ex:
if "There is no current event loop in thread" in str(ex):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
return loop.run_until_complete(
self._async_send_content(message_batch=message_batch)
)
Expand All @@ -384,9 +377,7 @@ async def _async_send_content(
headers=self.form_headers, trust_env=True
) as session:
for request in message_batch:
tasks.append(
self._post_async(session=session, payload=request)
)
tasks.append(self._post_async(session=session, payload=request))
response_data = await asyncio.gather(*tasks)
await session.close()
return response_data
Expand Down Expand Up @@ -431,9 +422,7 @@ def check_approval_status(self, content_sid: str) -> dict:
if response.status_code == 200:
return response.json()
else:
raise Exception(
f"Client did not accept request: {response.json()}"
)
raise Exception(f"Client did not accept request: {response.json()}")

def delete_content(self, content_sid: str) -> None:
response = requests.delete(
Expand All @@ -443,9 +432,7 @@ def delete_content(self, content_sid: str) -> None:
if response.status_code == 204:
return
elif response.status_code == 404:
logger.warning(
f"The requested content_sid '{content_sid}' does not exist."
)
logger.warning(f"The requested content_sid '{content_sid}' does not exist.")
else:
raise Exception(f"Client did not accept request: {str(response)}")

Expand All @@ -465,9 +452,7 @@ def submit_content_for_approval(
if response.status_code == 201:
return response.json()
else:
raise Exception(
f"Client did not accept request: {response.json()}"
)
raise Exception(f"Client did not accept request: {response.json()}")

def create_content(self, payload: ContentCreateRequest) -> dict:
"""Used for creating content with the Content API
Expand All @@ -489,6 +474,4 @@ def create_content(self, payload: ContentCreateRequest) -> dict:
if response.status_code == 201:
return response.json()
else:
raise Exception(
f"Client did not accept request: {response.json()}"
)
raise Exception(f"Client did not accept request: {response.json()}")

0 comments on commit 1b588c4

Please sign in to comment.