|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import logging |
| 4 | +from uuid import uuid4 |
| 5 | +from hashlib import sha1 |
| 6 | + |
| 7 | +from sentry.http import safe_urlopen |
| 8 | +from sentry.newsletter.base import Newsletter |
| 9 | +from sentry.utils import json |
| 10 | + |
| 11 | +logger = logging.getLogger('sentry.newsletter') |
| 12 | + |
| 13 | + |
| 14 | +def get_install_id(): |
| 15 | + from sentry import options |
| 16 | + install_id = options.get('sentry:install-id') |
| 17 | + if not install_id: |
| 18 | + install_id = sha1(uuid4().bytes).hexdigest() |
| 19 | + options.set('sentry:install-id', install_id) |
| 20 | + return install_id |
| 21 | + |
| 22 | + |
| 23 | +class RemoteNewsletter(Newsletter): |
| 24 | + enabled = True |
| 25 | + endpoint = 'https://sentry.io/remote/newsletter/subscription/' |
| 26 | + |
| 27 | + def __init__(self, endpoint=None): |
| 28 | + if endpoint is not None: |
| 29 | + self.endpoint = endpoint |
| 30 | + |
| 31 | + def update_subscription(self, user, **kwargs): |
| 32 | + kwargs['user_id'] = user.id |
| 33 | + kwargs['email'] = user.email |
| 34 | + kwargs['referral'] = 'onpremise' |
| 35 | + kwargs['install_id'] = get_install_id() |
| 36 | + try: |
| 37 | + response = safe_urlopen( |
| 38 | + self.endpoint, |
| 39 | + method='POST', |
| 40 | + headers={'Content-Type': 'application/json'}, |
| 41 | + data=json.dumps(kwargs), |
| 42 | + ) |
| 43 | + response.raise_for_status() |
| 44 | + return response.json() |
| 45 | + except Exception: |
| 46 | + logger.exception('update.failed') |
| 47 | + return None |
| 48 | + |
| 49 | + def get_subscriptions(self, user): |
| 50 | + try: |
| 51 | + response = safe_urlopen( |
| 52 | + self.endpoint, |
| 53 | + method='GET', |
| 54 | + params={ |
| 55 | + 'user_id': user.id, |
| 56 | + 'install_id': get_install_id(), |
| 57 | + }, |
| 58 | + ) |
| 59 | + response.raise_for_status() |
| 60 | + return response.json() |
| 61 | + except Exception: |
| 62 | + logger.exception('fetch.failed') |
| 63 | + return None |
0 commit comments