示例#1
0
def deals():
    """
    Deals endpoint fetches all deals in HubSpot API
    Data is pulled every time from the API and stored to MongoDb.
    Then is restored back and returned as table template
    """
    token = Token.objects.first()
    if token is None:
        return redirect(url_for('home_page.get'))
    elif not token.is_valid():
        new_token = HubSpotApi.get_new_token(token.refresh_token)
        token.update(**new_token)
        token.save()
    try:
        hubspot_api = HubSpotApi(token)
        deal_endpoint = Endpoint.create('deals')
        response = deal_endpoint.fetch_data(hubspot_api=hubspot_api)
        Deal.objects.all().delete()
        deals = [Deal(**data) for data in response]
        if deals:
            Deal.objects.insert(deals, load_bulk=False)
    except Exception:
        logger.exception(f'Error accessing requested endpoint {deal_endpoint.get_url()}')
        Token.objects.all().delete()
        return redirect(url_for('home_page.get'))
    return render_template('deals.html', deals=json.loads(Deal.objects().to_json()))
示例#2
0
def test_get_auth_url():
    resp = HubSpotApi.get_auth_url(auth_callback_url='https://localhost',
                                   scopes=['contacts'])
    expected_url = ''.join([
        'https://app.hubspot.com/oauth/authorize?',
        'response_type=code&client_id=105c5a0a-0ea',
        'f-41de-ba45-58350a44c82a&redirect_uri=htt',
        'ps%3A%2F%2Flocalhost&scope=contacts&state=random_string'
    ])
    assert resp == expected_url
示例#3
0
def get():
    """
    landing endpoint for start the OAuth2 process.
    The auth url with the registered callback is returned to the user
    to call the HubSpot and authorize the data fetching.

    """
    token = Token.objects.first()
    if token is None:
        auth_url = HubSpotApi.get_auth_url(AUTH_REDIRECT_URL, SCOPES)
        return redirect(auth_url)
    elif not token.is_valid():
        try:
            new_token = HubSpotApi.get_new_token(token.refresh_token)
            token.update(**new_token)
            token.save()
        except Exception:
            logger.exception('Error updating access token')
            abort(500)
    return redirect(url_for('.deals'))
示例#4
0
def callback():
    """
    Step 2 of OAuth2 process. This callback is requested by the HubSpot Api on the redirect response.

    The token can now be fetched using the code provided on the redirect call received.

    Once the token is persisted, the redirect for deals endpoint is returned
    """
    code = request.values.get("code")
    token = HubSpotApi.fetch_token(code=code, auth_resp_url=request.url, auth_callback_url=AUTH_REDIRECT_URL)
    try:
        Token(**token).save()
    except Exception:
        logger.exception('Error storing new token')
        abort(500)
    return redirect(url_for('.deals'))
示例#5
0
def test_invalid_scope():
    with pytest.raises(InvalidScope):
        HubSpotApi.get_auth_url(auth_callback_url='', scopes=['invalid'])
示例#6
0
def test_get_new_token(mocked_token):
    token = HubSpotApi.get_new_token(refresh_token='some_token')
    assert token == 'a token'
示例#7
0
def test_get_new_token_error():
    with pytest.raises(AuthorizationError):
        HubSpotApi.get_new_token(refresh_token='some_token')
示例#8
0
def test_fetch_token(mocked_token):
    token = HubSpotApi.fetch_token(code='some_code',
                                   auth_resp_url='',
                                   auth_callback_url='')
    assert token == 'a token'
示例#9
0
def test_fetch_token_error():
    with pytest.raises(AuthorizationError):
        HubSpotApi.fetch_token(code='some_code',
                               auth_resp_url='',
                               auth_callback_url='')