示例#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 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'))
示例#3
0
def test_get_new_token(mocked_token):
    token = HubSpotApi.get_new_token(refresh_token='some_token')
    assert token == 'a token'
示例#4
0
def test_get_new_token_error():
    with pytest.raises(AuthorizationError):
        HubSpotApi.get_new_token(refresh_token='some_token')