(20241125) Minor debugging and documentation added.

This commit is contained in:
2024-11-25 19:36:09 +05:30
parent ed415c6b3a
commit 87593baac5
+11 -9
View File
@@ -16,6 +16,7 @@
1. Quickstart: https://developers.google.com/gmail/api/quickstart/python 1. Quickstart: https://developers.google.com/gmail/api/quickstart/python
2. Labels: https://developers.google.com/gmail/api/guides/labels 2. Labels: https://developers.google.com/gmail/api/guides/labels
3. Messages: https://developers.google.com/gmail/api/reference/rest/v1/users.messages
DOWNLOADS: DOWNLOADS:
@@ -172,6 +173,7 @@ class AsyncGMailClient:
profile = self.__service.users().getProfile(userId = "me").execute() profile = self.__service.users().getProfile(userId = "me").execute()
self.__user_email = profile.get("emailAddress") self.__user_email = profile.get("emailAddress")
# Done here:
return True return True
# In case something goes wrong: # In case something goes wrong:
@@ -201,7 +203,7 @@ class AsyncGMailClient:
""" """
if await GoogleOAuth.credentials_have_expired(self.__credentials): if await GoogleOAuth.credentials_have_expired(self.__credentials):
self._printer("Refreshing Token:", self.__user_email) if not self._debug_only_errors: self._printer("Refreshing Token.", self.__user_email)
self.__credentials.refresh(Request()) self.__credentials.refresh(Request())
# ┏┓ • # ┏┓ •
@@ -245,6 +247,7 @@ class AsyncGMailClient:
await self.__ensure_token() await self.__ensure_token()
# Make the API call: # Make the API call:
if not self._debug_only_errors: self._printer("Listing All Labels.", self.__user_email)
api_response = await self.__http_client.get( api_response = await self.__http_client.get(
url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels", url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels",
headers = {"Authorization": f"Bearer {self.__credentials.token}"} headers = {"Authorization": f"Bearer {self.__credentials.token}"}
@@ -291,6 +294,7 @@ class AsyncGMailClient:
await self.__ensure_token() await self.__ensure_token()
# Make the API call: # Make the API call:
if not self._debug_only_errors: self._printer("Getting One Label.", self.__user_email)
api_response = await self.__http_client.get( api_response = await self.__http_client.get(
url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}", url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}",
headers = {"Authorization": f"Bearer {self.__credentials.token}"} headers = {"Authorization": f"Bearer {self.__credentials.token}"}
@@ -345,6 +349,7 @@ class AsyncGMailClient:
await self.__ensure_token() await self.__ensure_token()
# Make the API call: # Make the API call:
if not self._debug_only_errors: self._printer("Creating One Label.", self.__user_email)
api_response = await self.__http_client.post( api_response = await self.__http_client.post(
url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels", url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels",
headers = { headers = {
@@ -428,6 +433,7 @@ class AsyncGMailClient:
if not json_body: return success if not json_body: return success
# Make the API call: # Make the API call:
if not self._debug_only_errors: self._printer("Updating One Label.", self.__user_email)
api_response = await self.__http_client.put( api_response = await self.__http_client.put(
url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}", url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}",
headers = {"Authorization": f"Bearer {self.__credentials.token}"}, headers = {"Authorization": f"Bearer {self.__credentials.token}"},
@@ -475,6 +481,7 @@ class AsyncGMailClient:
await self.__ensure_token() await self.__ensure_token()
# Make the API call: # Make the API call:
if not self._debug_only_errors: self._printer("Deleting One Label.", self.__user_email)
api_response = await self.__http_client.delete( api_response = await self.__http_client.delete(
url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}", url = f"https://gmail.googleapis.com/gmail/v1/users/{self.__user_email}/labels/{label_id}",
headers = {"Authorization": f"Bearer {self.__credentials.token}"} headers = {"Authorization": f"Bearer {self.__credentials.token}"}
@@ -528,7 +535,7 @@ if __name__ == "__main__":
config = secrets_dict, config = secrets_dict,
redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail", redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail",
debug = True, debug = True,
debug_prefix = "OAuth (Goog) | " debug_prefix = "OAuth (Goog) | ",
) )
tokens = { tokens = {
@@ -546,36 +553,31 @@ if __name__ == "__main__":
my_gmail = AsyncGMailClient( my_gmail = AsyncGMailClient(
http_client = test_client, http_client = test_client,
credentials = await my_oauth.credentials_from_tokens(tokens = tokens) credentials = await my_oauth.credentials_from_tokens(tokens = tokens),
debug_only_errors = False
) )
await my_gmail.initialize() await my_gmail.initialize()
print(await my_gmail.user_email) print(await my_gmail.user_email)
print(await my_gmail.user_profile) print(await my_gmail.user_profile)
# print("Listing labels.")
# print(json.to_string(await my_gmail.list_labels())) # print(json.to_string(await my_gmail.list_labels()))
# print("Getting one label.")
# print(json.to_string(await my_gmail.get_label(label_id = "Label_3"))) # print(json.to_string(await my_gmail.get_label(label_id = "Label_3")))
# print("Updating one label.")
# print(json.to_string(await my_gmail.update_label( # print(json.to_string(await my_gmail.update_label(
# label_id = "Label_3", # label_id = "Label_3",
# label_name = "Updated Label 123", # label_name = "Updated Label 123",
# label_background_color = "#cc3a21" # label_background_color = "#cc3a21"
# ))) # )))
# #
# print("Getting one label.")
# print(json.to_string(await my_gmail.get_label(label_id = "Label_3"))) # print(json.to_string(await my_gmail.get_label(label_id = "Label_3")))
# print("Creating label:")
# print(json.to_string(await my_gmail.create_label( # print(json.to_string(await my_gmail.create_label(
# label_name = "Bye - Bye !", # label_name = "Bye - Bye !",
# label_text_color = "#fbc8d9", # label_text_color = "#fbc8d9",
# label_background_color = "#7a2e0b" # label_background_color = "#7a2e0b"
# ))) # )))
print("Deleting one label.")
print(json.to_string(await my_gmail.delete_label(label_id = "Label_6"))) print(json.to_string(await my_gmail.delete_label(label_id = "Label_6")))