Esempio n. 1
0
    def test_auth(self, requests_mock, attributes, use_auth_header):
        braze_client = BrazeClient(api_key="API_KEY",
                                   use_auth_header=use_auth_header)
        headers = {"Content-Type": "application/json"}
        mock_json = {"message": "success", "errors": ""}
        requests_mock.post(ANY,
                           json=mock_json,
                           status_code=200,
                           headers=headers)

        braze_client.user_track(attributes=attributes)
        request = requests_mock.last_request
        if use_auth_header:
            assert "api_key" not in request.json()
            assert "Authorization" in request.headers
            assert request.headers["Authorization"].startswith("Bearer ")
        else:
            assert "api_key" in request.json()
            assert "Authorization" not in request.headers
Esempio n. 2
0
def send_email(email, data):
    """
    Send email through Braze
    """
    event_properties = _get_event_properties(data)
    braze_client = BrazeClient(
        api_key=settings.EDX_BRAZE_API_KEY,
        api_url=settings.EDX_BRAZE_API_SERVER,
        app_id='',
    )

    try:
        attributes = None
        external_id = braze_client.get_braze_external_id(email)
        if external_id:
            event_properties.update({'external_id': external_id})
        else:
            braze_client.create_braze_alias(emails=[email],
                                            alias_label='save_for_later')
            user_alias = {
                'alias_label': 'save_for_later',
                'alias_name': email,
            }
            event_properties.update({'user_alias': user_alias})
            attributes = [{
                'user_alias': user_alias,
                'pref-lang': data.get('pref-lang', 'en')
            }]

        braze_client.track_user(events=[event_properties],
                                attributes=attributes)

        event_type = data.get('type')
        event_data = {
            'user_id': data.get('user_id'),
            'category': 'save-for-later',
            'type': event_type,
            'send_to_self': data.get('send_to_self'),
        }
        if event_type == 'program':
            program = data.get('program')
            event_data.update({'program_uuid': program.get('uuid')})
        elif event_type == 'course':
            course = data.get('course')
            event_data.update({'course_key': str(course.id)})

        tracker.emit(
            USER_SAVE_FOR_LATER_REMINDER_EMAIL_SENT if data.get('reminder')
            else USER_SAVE_FOR_LATER_EMAIL_SENT, event_data)
    except Exception:  # pylint: disable=broad-except
        log.warning('Unable to send save for later email ', exc_info=True)
        return False
    else:
        return True
Esempio n. 3
0
def send_email(request, email, data):
    """
    Send email through Braze
    """
    event_properties = _get_event_properties(data)
    braze_client = BrazeClient(
        api_key=settings.EDX_BRAZE_API_KEY,
        api_url=settings.EDX_BRAZE_API_SERVER,
        app_id='',
    )

    try:
        attributes = None
        external_id = braze_client.get_braze_external_id(email)
        if external_id:
            event_properties.update({'external_id': external_id})
        else:
            braze_client.create_braze_alias(emails=[email],
                                            alias_label='save_for_later')
            user_alias = {
                'alias_label': 'save_for_later',
                'alias_name': email,
            }
            event_properties.update({'user_alias': user_alias})
            attributes = [{
                'user_alias':
                user_alias,
                'pref-lang':
                request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME, 'en')
            }]

        braze_client.track_user(events=[event_properties],
                                attributes=attributes)
        tracker.emit(
            USER_SENT_EMAIL_SAVE_FOR_LATER, {
                'user_id':
                request.user.id,
                'category':
                'save-for-later',
                'type':
                event_properties.get('type'),
                'send_to_self':
                bool(not request.user.is_anonymous
                     and request.user.email == email),
            })
    except Exception:  # pylint: disable=broad-except
        log.warning('Unable to send save for later email ', exc_info=True)
        return False
    else:
        return True
Esempio n. 4
0
def braze_client():
    return BrazeClient(api_key="API_KEY")
Esempio n. 5
0
from braze.client import BrazeClient

client = BrazeClient(api_key="YOUR_API_KEY")

# For create - update users
r = client.user_track(
    attributes=[
        {
            "external_id": "1",
            "first_name": "First name",
            "last_name": "Last name",
            "email": "*****@*****.**",
            "status": "Active",
            # And other fields ...
        }
    ],
    events=None,  # if we don't want to send events we set it to None
    purchases=None,
)
if r["success"]:
    # do our magic here
    print("Success!")
    print(r)
else:
    print(r["client_error"])
    print(r["errors"])

# For delete users by external_id or appboy_id
r = client.user_delete(external_ids=["1"], braze_ids=None)
if r["success"]:
    # do our magic here