2020-10-08 15:42:52 +00:00
|
|
|
from zeep.client import Client as ZeepClient, Settings as ZeepSettings
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
def __init__(self, sympa_url: str):
|
|
|
|
self.sympa_url = sympa_url
|
2020-10-08 16:27:33 +00:00
|
|
|
self.zeep = ZeepClient(sympa_url + "/wsdl", settings=ZeepSettings(strict=False))
|
2020-10-08 15:42:52 +00:00
|
|
|
|
2020-10-08 16:27:33 +00:00
|
|
|
def login(self, email: str, password: str) -> None:
|
2020-10-08 15:42:52 +00:00
|
|
|
"""
|
|
|
|
Login into the API. Set a cookie for future connexions.
|
|
|
|
"""
|
|
|
|
result = self.zeep.service.login(email, password)
|
|
|
|
element = result._raw_elements[0]
|
|
|
|
self.cookie = element.text
|
|
|
|
self.zeep.settings.extra_http_headers = [("Cookie", f"sympa_session={element.text}")]
|
|
|
|
if self.checkCookie() != email:
|
|
|
|
# FIXME Better exception
|
|
|
|
raise Exception("Unknown error: given cookie is invalid")
|
|
|
|
print("Successfully authenticated!")
|
|
|
|
|
2020-10-08 16:27:33 +00:00
|
|
|
def checkCookie(self) -> str:
|
2020-10-08 15:42:52 +00:00
|
|
|
"""
|
|
|
|
From the current cookie, retrieve the email address.
|
|
|
|
"""
|
|
|
|
result = self.zeep.service.checkCookie()
|
|
|
|
element = result._raw_elements[0]
|
|
|
|
return element.text
|
2020-10-08 16:27:33 +00:00
|
|
|
|
|
|
|
def amI(self, mailing_list: str, function: str, email: str) -> bool:
|
|
|
|
"""
|
|
|
|
Check if the given `email` is a member of type `function` in the `mailing_list`.
|
|
|
|
The function parameter is one between subscriber, editor or owner.
|
|
|
|
"""
|
|
|
|
if function not in ["subscriber", "editor", "owner"]:
|
|
|
|
raise ValueError("function of a mailing list member must be subscriber, editor or owner.")
|
|
|
|
result = self.zeep.service.amI(mailing_list, function, email)
|
|
|
|
element = result._raw_elements[0]
|
|
|
|
return element.text == "true"
|