Skip to content

Commit

Permalink
Revert "updated async loop"
Browse files Browse the repository at this point in the history
  • Loading branch information
ggx-jacechun authored Sep 14, 2023
1 parent f397199 commit 74fa4af
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions ggvlib/twilio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ 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 @@ -88,7 +94,8 @@ 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 @@ -142,7 +149,9 @@ 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 @@ -220,7 +229,9 @@ 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 @@ -233,7 +244,9 @@ 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 @@ -349,13 +362,7 @@ def async_send_content(
Returns:
List[aiohttp.ClientResponse]: A list of responses from the client
"""
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()
loop = asyncio.get_event_loop()
return loop.run_until_complete(
self._async_send_content(message_batch=message_batch)
)
Expand All @@ -377,7 +384,9 @@ 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 @@ -422,7 +431,9 @@ 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 @@ -432,7 +443,9 @@ 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 @@ -452,7 +465,9 @@ 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 @@ -474,4 +489,6 @@ 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 74fa4af

Please sign in to comment.