-
Notifications
You must be signed in to change notification settings - Fork 64
/
mail_client.py
122 lines (102 loc) · 3.92 KB
/
mail_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import requests
import argparse
import pprint # For pretty printing
SERVER = 'http://localhost:5000'
def send_mail(recipient: str, sender: str, subject: str, body: str) -> bool:
"""
Sends a mail entry to the server by making a POST request to the /mail endpoint.
The JSON body of the request contains the following keys:
- recipient
- sender
- subject
- body
Args:
recipient (str): The recipient of the mail
sender (str): The sender of the mail
subject (str): The subject of the mail
body (str): The body of the mail
Returns:
bool: True if the mail was sent successfully, False otherwise
"""
mail_entry = {
'recipient': recipient,
'sender': sender,
'subject': subject,
'body': body,
}
response = requests.post(f'{SERVER}/mail', json=mail_entry)
pprint.pprint(response.json())
def get_inbox(recipient: str) -> None:
"""TODO: fill out this docstring (using the send_mail docstring as a guide)
"""
response = requests.get(f'{SERVER}/mail/inbox/{recipient}')
pprint.pprint(response.json())
def get_sent(sender: str) -> None:
"""TODO: fill out this docstring (using the send_mail docstring as a guide)
"""
response = requests.get(f'{SERVER}/mail/sent/{sender}')
pprint.pprint(response.json())
def get_mail(mail_id: str) -> None:
"""TODO: fill out this docstring (using the send_mail docstring as a guide)
"""
response = requests.get(f'{SERVER}/mail/{mail_id}')
pprint.pprint(response.json())
def delete_mail(mail_id: str) -> None:
"""TODO: fill out this docstring (using the send_mail docstring as a guide)
"""
response = requests.delete(f'{SERVER}/mail/{mail_id}')
pprint.pprint(response.json())
# Command Line Interface
# making CLIs with argparse may be helpful for you in the future
# see if you can understand what each line is doing
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description='Mail Client')
subparsers = parser.add_subparsers(dest='command')
subparsers.required = True
send_parser = subparsers.add_parser('send', help='Send a mail')
send_parser.add_argument('body', help='The body of the mail')
send_parser.add_argument(
'-t', "--to",
dest="recipient",
help='The recipient of the mail'
)
send_parser.add_argument(
'-f', "--from",
dest="sender",
help='The sender of the mail'
)
send_parser.add_argument(
'-s', "--subject",
help='The subject of the mail',
default="No Subject"
)
inbox_parser = subparsers.add_parser('inbox', help='Get your inbox')
inbox_parser.add_argument('-u', "--user", help='The recipient of the mail')
sent_parser = subparsers.add_parser('sent', help='Get your sent mail')
sent_parser.add_argument('-u', "--user", help='The sender of the mail')
get_parser = subparsers.add_parser('get', help='Get a mail')
get_parser.add_argument('mail_id', help='The id of the mail')
delete_parser = subparsers.add_parser('delete', help='Delete a mail')
delete_parser.add_argument('mail_id', help='The id of the mail')
return parser
def main():
parser = get_parser()
args = parser.parse_args()
if args.command == 'send':
send_mail(args.recipient, args.sender, args.subject, args.body)
elif args.command == 'inbox':
get_inbox(args.user)
elif args.command == 'sent':
get_sent(args.user)
elif args.command == 'get':
get_mail(args.mail_id)
elif args.command == 'delete':
delete_mail(args.mail_id)
# TODO: run the code!
# to run the code, open a terminal and type:
# python mail_client.py --help
# For example, to send a mail, type:
# python mail_client.py send -t "recipient" -f "sender" -s "subject" "body"
# you'll need to demo sending, receiving, and deleting mail for checkoff.
if __name__ == '__main__':
main()