Skip to content

Commit

Permalink
Merge branch 'create-and-delete-label' (PR jeremyephron#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyephron committed Mar 11, 2023
2 parents 8f667c7 + 4f92f1b commit cfb53bf
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions simplegmail/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,75 @@ def list_labels(self, user_id: str = 'me') -> List[Label]:
labels = [Label(name=x['name'], id=x['id']) for x in res['labels']]
return labels

def create_label(
self,
name: str,
user_id: str = 'me'
) -> Label:
"""
Creates a new label.
Args:
name: The display name of the new label.
user_id: The user's email address. By default, the authenticated
user.
Returns:
The created Label object.
Raises:
googleapiclient.errors.HttpError: There was an error executing the
HTTP request.
"""

body = {
"name": name,

# TODO: In the future, can add the following fields:
# "messageListVisibility"
# "labelListVisibility"
# "color"
}

try:
res = self.service.users().labels().create(
userId=user_id,
body=body
).execute()

except HttpError as error:
# Pass along the error
raise error

else:
return Label(res['name'], res['id'])

def delete_label(self, label: Label, user_id: str = 'me') -> None:
"""
Deletes a label.
Args:
label: The label to delete.
user_id: The user's email address. By default, the authenticated
user.
Raises:
googleapiclient.errors.HttpError: There was an error executing the
HTTP request.
"""

try:
self.service.users().labels().delete(
userId=user_id,
id=label.id
).execute()

except HttpError as error:
# Pass along the error
raise error

def _get_messages_from_refs(
self,
user_id: str,
Expand Down

0 comments on commit cfb53bf

Please sign in to comment.