pyceo/ceo_common/model/RemoteMailmanService.py

36 lines
1.3 KiB
Python
Raw Normal View History

2021-08-23 09:59:01 -04:00
from flask import g
2021-07-24 17:09:10 -04:00
from zope import component
from zope.interface import implementer
2021-08-04 01:54:21 -04:00
from ..errors import UserAlreadySubscribedError, NoSuchListError, \
UserNotSubscribedError
2021-07-24 17:09:10 -04:00
from ..interfaces import IMailmanService, IConfig, IHTTPClient
@implementer(IMailmanService)
class RemoteMailmanService:
def __init__(self):
cfg = component.getUtility(IConfig)
2021-08-03 19:19:33 -04:00
self.mailman_host = cfg.get('ceod_mailman_host')
2021-07-24 17:09:10 -04:00
self.http_client = component.getUtility(IHTTPClient)
def subscribe(self, address: str, mailing_list: str):
2021-08-23 09:59:01 -04:00
resp = self.http_client.post(
self.mailman_host, f'/api/mailman/{mailing_list}/{address}',
principal=g.sasl_user)
2021-08-04 01:54:21 -04:00
if not resp.ok:
if resp.status_code == 409:
raise UserAlreadySubscribedError()
elif resp.status_code == 404:
raise NoSuchListError()
raise Exception(resp.json())
2021-07-24 17:09:10 -04:00
def unsubscribe(self, address: str, mailing_list: str):
2021-08-23 09:59:01 -04:00
resp = self.http_client.delete(
self.mailman_host, f'/api/mailman/{mailing_list}/{address}',
principal=g.sasl_user)
2021-08-04 01:54:21 -04:00
if not resp.ok:
if resp.status_code == 404:
raise UserNotSubscribedError()
raise Exception(resp.json())