Example #1
0
async def http_request(url,
                       request_session,
                       method=None,
                       data=None,
                       headers=None,
                       timeout=DEFAULT_TIMEOUT,
                       is_json=True):
    response = await _requests_http_request(url, request_session, method, data
                                            or {}, headers or {}, timeout)
    if is_json or response.headers['Content-Type'] == 'application/json':
        try:
            response_body = await response.json()

        # TODO fix this error handling since we are no longer decoding json...
        except JSONDecodeError:
            raise PlaidError.from_response({
                'error_message': response.text,
                'error_type': 'API_ERROR',
                'error_code': 'INTERNAL_SERVER_ERROR',
                'display_message': None,
                'request_id': '',
                'causes': [],
            })
        if response_body.get('error_type'):
            raise PlaidError.from_response(response_body)
        else:
            return response_body
    else:
        return response.content
Example #2
0
def _http_request(url,
                  method=None,
                  data=None,
                  headers=None,
                  timeout=DEFAULT_TIMEOUT,
                  is_json=True):
    response = _requests_http_request(url, method, data or {}, headers or {},
                                      timeout)

    if is_json or response.headers['Content-Type'] == 'application/json':
        try:
            response_body = json.loads(response.text)
        except JSONDecodeError:
            raise PlaidError.from_response({
                'error_message': response.text,
                'error_type': 'API_ERROR',
                'error_code': 'INTERNAL_SERVER_ERROR',
                'display_message': None,
                'request_id': '',
                'causes': [],
            })
        if response_body.get('error_type'):
            raise PlaidError.from_response(response_body)
        else:
            return response_body
    else:
        return response.content
Example #3
0
def http_request(
        url,
        method=None,
        data=None,
        headers=None,
        timeout=DEFAULT_TIMEOUT,
        is_json=True):
    response = _requests_http_request(
        url,
        method,
        data or {},
        headers or {},
        timeout)

    if is_json or response.headers['Content-Type'] == 'application/json':
        try:
            response_body = json.loads(response.text)
        except JSONDecodeError:
            raise PlaidError.from_response({
                'error_message': response.text,
                'error_type': 'API_ERROR',
                'error_code': 'INTERNAL_SERVER_ERROR',
                'display_message': None,
                'request_id': '',
                'causes': [],
            })
        if response_body.get('error_type'):
            raise PlaidError.from_response(response_body)
        else:
            return response_body
    else:
        return response.content
Example #4
0
def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT):
    response = _requests_http_request(url, method, data or {}, timeout)
    try:
        response_body = json.loads(response.text)
    except json.JSONDecodeError:
        raise PlaidError.from_response({
            'error_message': response.text,
            'error_type': 'API_ERROR',
            'error_code': 'INTERNAL_SERVER_ERROR',
            'display_message': None,
        })
    if response_body.get('error_type'):
        raise PlaidError.from_response(response_body)
    else:
        return response_body
Example #5
0
def create_link_token(user):
    """Create a Plaid link token.

    Args:
        user (User): Current user in session.
    """
    try:
        body = {
            "client_name": "Mesada",
            "country_codes": settings.PLAID_COUNTRIES,
            "language": "en",
            "user": {
                "client_user_id": str(user.id),
                "legal_name": f"{user.first_name} {user.last_name}",
                "phone_number": str(user.phone),
                "email_address": user.email,
            },
            "products": settings.PLAID_PRODUCTS,
        }

        response = client.LinkToken.create(body)

        return response
    except PlaidError as e:
        raise PlaidError(e)
Example #6
0
def test_from_response():
    response = {
        'display_message': None,
        'error_type': 'API_ERROR',
        'error_code': 'INTERNAL_SERVER_ERROR',
        'error_message': 'an unexpected error occurred',
        'request_id': 'abc123',
        'causes': [
            {
                'error_type': 'API_ERROR',
                'error_code': 'INTERNAL_SERVER_ERROR',
                'error_message': 'an unexpected error occurred',
                'item_id': '456',
            },
        ],
    }

    error = PlaidError.from_response(response)
    assert isinstance(error, APIError)
    assert error.code == 'INTERNAL_SERVER_ERROR'
    assert error.message == 'an unexpected error occurred'

    assert len(error.causes) == 1
    cause = error.causes[0]
    assert cause.type == 'API_ERROR'
    assert cause.code == 'INTERNAL_SERVER_ERROR'
    assert cause.message == 'an unexpected error occurred'
    assert cause.item_id == '456'
Example #7
0
def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT):
    response = _requests_http_request(url, method, data or {}, timeout)
    response_body = json.loads(response.text)
    if response_body.get('error_type'):
        raise PlaidError.from_response(response_body)
    else:
        return response_body
def test_from_response():
    response = {
        'display_message':
        None,
        'error_type':
        'API_ERROR',
        'error_code':
        'INTERNAL_SERVER_ERROR',
        'error_message':
        'an unexpected error occurred',
        'request_id':
        'abc123',
        'causes': [
            {
                'error_type': 'API_ERROR',
                'error_code': 'INTERNAL_SERVER_ERROR',
                'error_message': 'an unexpected error occurred',
                'item_id': '456',
            },
        ],
    }

    error = PlaidError.from_response(response)
    assert isinstance(error, APIError)
    assert error.code == 'INTERNAL_SERVER_ERROR'
    assert error.message == 'an unexpected error occurred'

    assert len(error.causes) == 1
    cause = error.causes[0]
    assert cause.type == 'API_ERROR'
    assert cause.code == 'INTERNAL_SERVER_ERROR'
    assert cause.message == 'an unexpected error occurred'
    assert cause.item_id == '456'
Example #9
0
def test_from_response():
    response = {
        'display_message': None,
        'error_type': 'API_ERROR',
        'error_code': 'INTERNAL_SERVER_ERROR',
        'error_message': 'an unexpected error occurred',
        'request_id': 'abc123'
    }

    error = PlaidError.from_response(response)
    assert isinstance(error, APIError)
    assert error.code == 'INTERNAL_SERVER_ERROR'