-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailAPI.py
More file actions
59 lines (42 loc) · 1.96 KB
/
MailAPI.py
File metadata and controls
59 lines (42 loc) · 1.96 KB
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
from base64 import encode
from urllib.parse import urlencode
from MailBox import MailBox
import requests
from requests.auth import HTTPBasicAuth
import json
class MailAPI:
def __init__(self,mail_url,username,password):
self.mail_url = mail_url
self.username = username
self.password = password
def getData(self,request_url):
data = requests.get(request_url,auth=HTTPBasicAuth(self.username,self.password))
if data.ok:
return json.loads(data.content)
else:
return "Error en la peticion"
def get_mailbox_Data(self,mail_box):
url_mailbox = self.mail_url+"/admin/api/v1/boxes/"+mail_box
return self.getData(url_mailbox)
def post(self,url,data):
headers = {"Content-Type": "application/json "}
response = requests.post(url,data=data,headers=headers,auth=HTTPBasicAuth(self.username,self.password))
return response
def patch(self,url,data):
headers = {"Content-Type": "application/json "}
response = requests.patch(url,data=data,headers=headers,auth=HTTPBasicAuth(self.username,self.password))
return response
def create_mailbox(self,mailbox:MailBox):
url_mailbox = self.mail_url+"/admin/api/v1/boxes"
data_json = json.dumps(mailbox.to_create())
return self.post(url_mailbox,data_json)
def patch_mailbox(self, mailbox:MailBox, new_password):
url_mailbox = "{}/admin/api/v1/boxes/{}".format(self.mail_url,mailbox.email)
data_json = json.dumps(mailbox.patch_json(new_password))
return self.patch(url_mailbox,data_json)
def update_quota(self, mailbox:MailBox, storageLimit, countLimit):
url_mailbox = "{}/admin/api/v1/boxes/{}/quota/".format(self.mail_url,mailbox.email)
print(url_mailbox)
data_json = json.dumps(mailbox.to_update_quota(storageLimit, countLimit))
print(data_json)
return self.patch(url_mailbox,data_json)