(20241202) ??

This commit is contained in:
2024-12-02 18:46:48 +05:30
parent 4d8b40a6c7
commit aed746190d
7 changed files with 684 additions and 19 deletions
+25 -18
View File
@@ -201,7 +201,7 @@ class AsyncGMailClient(AsyncGoogleBase):
if api_response.httpCode in [200]:
api_response.success = True
api_json = await api_response.get_json()
api_response.data = {label.pop("name"): label for label in api_json.get("labels", [])}
api_response.data = {label["name"]: label for label in api_json.get("labels", [])}
# Done here:
return api_response
@@ -417,7 +417,7 @@ class AsyncGMailClient(AsyncGoogleBase):
async def __list_messages_on_page(
self,
tokens: GoogleAuthTokens,
count: int = 100,
max_count: int = 100,
query: str = None,
label_ids: List[str] | str = None,
include_spam_and_trash: bool = False,
@@ -434,7 +434,7 @@ class AsyncGMailClient(AsyncGoogleBase):
1. https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list
2. https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message
:param tokens: The object that holds the access token to the service.
:param count: The no. of messages to fetch.
:param max_count: The no. of messages to fetch.
:param query: Any query filter that is supported by GMail.
:param label_ids: The list of labels' ids that the mails must have on them.
:param include_spam_and_trash: Whether, or not, you would like to include mails categorized as spam and trash.
@@ -453,7 +453,7 @@ class AsyncGMailClient(AsyncGoogleBase):
# Build the needed params:
params_json = {
"maxResults": count,
"maxResults": max_count,
"includeSpamTrash": include_spam_and_trash
}
if query: params_json["q"] = query
@@ -461,7 +461,7 @@ class AsyncGMailClient(AsyncGoogleBase):
if label_ids: params_json["labelIds"] = label_ids if isinstance(label_ids, list) else [label_ids]
# Make the API call:
if not self._debug_only_errors: self._printer("Listing Messages for Page.", user_id, count, next_page_token)
if not self._debug_only_errors: self._printer("Listing Messages for Page.", user_id, max_count, next_page_token)
api_response = await self.get(
url = f"https://gmail.googleapis.com/gmail/v1/users/{user_id}/messages",
headers = {"Authorization": f"Bearer {tokens.accessToken}"},
@@ -473,7 +473,7 @@ class AsyncGMailClient(AsyncGoogleBase):
api_response.success = True
api_json = await api_response.get_json()
api_response.data = {
"messages": {m.pop("id"): m for m in api_json.get("messages", [])},
"messages": {m["id"]: m for m in api_json.get("messages", [])},
"nextPageToken": api_json.get("nextPageToken"),
"resultSizeEstimate": api_json["resultSizeEstimate"],
}
@@ -484,7 +484,7 @@ class AsyncGMailClient(AsyncGoogleBase):
async def list_messages(
self,
tokens: GoogleAuthTokens,
count: int = 100,
max_count: int = 100,
query: str = None,
label_ids: List[str] | str = None,
include_spam_and_trash: bool = False,
@@ -498,7 +498,7 @@ class AsyncGMailClient(AsyncGoogleBase):
1. https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list
2. https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message
:param tokens: The object that holds the access token to the service.
:param count: The no. of messages to fetch.
:param max_count: The no. of messages to fetch.
:param query: Any query filter that is supported by GMail.
:param label_ids: The list of labels' ids that the mails must have on them.
:param include_spam_and_trash: Whether, or not, you would like to include mails categorized as spam and trash.
@@ -532,8 +532,8 @@ class AsyncGMailClient(AsyncGoogleBase):
# Let's figure out how many times we'll have to loop through the process to retrieve the target no. of
# messages. Google allows you to fetch info about at most 500 messages in one go.
max_per_call = 500 # ... because Google allows at most 500 entries in one call.
iterations_needed = int(math.ceil(count / max_per_call))
last_iteration_count = count - int((max_per_call * (iterations_needed - 1)))
iterations_needed = int(math.ceil(max_count / max_per_call))
last_iteration_count = max_count - int((max_per_call * (iterations_needed - 1)))
# Run the loop those many times:
results_size_estimate = 0
@@ -543,12 +543,12 @@ class AsyncGMailClient(AsyncGoogleBase):
if iterations_needed > 1:
if iteration_no < (iterations_needed - 1): iteration_count = max_per_call
else: iteration_count = last_iteration_count
else: iteration_count = count
else: iteration_count = max_count
# Retrieve the messages for this page:
iteration_response = await self.__list_messages_on_page(
tokens = tokens,
count = iteration_count,
max_count = iteration_count,
query = query,
label_ids = label_ids,
include_spam_and_trash = include_spam_and_trash,
@@ -957,13 +957,20 @@ if __name__ == "__main__":
# print(my_mail.get_raw_message(as_base64 = False))
# Test some feature:
# response = await my_gmail.list_messages(
# tokens = test_tokens,
# )
response = await my_gmail.get_message(
tokens = test_tokens,
message_id = "1936bfbfc912b86f"
auth_url = await my_gmail.get_authorization_url(
state = "1234567890",
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
approval_prompt = "force",
user_email = "pskhushal@gmail.com"
)
print("AUTH URL:", auth_url)
response = await my_gmail.list_labels(
tokens = test_tokens,
)
# response = await my_gmail.get_message(
# tokens = test_tokens,
# message_id = "1936bfbfc912b86f"
# )
print("SUCCESS:", response.success)
print("SUMMARY:", response.to_markdown())
print("\n\n---\n\n")