Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Source Paypal Transaction: report full error message details, updated check method #8580

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#

import json
import logging
import time
from abc import ABC
Expand All @@ -16,6 +17,30 @@
from dateutil.parser import isoparse


class PaypalHttpException(Exception):
"""Just for formatting the exception as Square"""
midavadim marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, error: requests.exceptions.HTTPError):
self.error = error

def __str__(self):
message = repr(self.error)

if self.error.response.content:
content = self.error.response.content.decode()
try:
details = json.loads(content)
except json.decoder.JSONDecodeError:
details = content

message = f"{message} Details: {details}"

return message

def __repr__(self):
return self.__str__()


def get_endpoint(is_sandbox: bool = False) -> str:
if is_sandbox:
endpoint = "https://api-m.sandbox.paypal.com"
Expand Down Expand Up @@ -227,6 +252,12 @@ def stream_slices(

return slices

def _send_request(self, request: requests.PreparedRequest, request_kwargs: Mapping[str, Any]) -> requests.Response:
try:
return super()._send_request(request, request_kwargs)
except requests.exceptions.HTTPError as http_error:
raise PaypalHttpException(http_error)


class Transactions(PaypalTransactionStream):
"""List Paypal Transactions on a specific date range
Expand Down Expand Up @@ -351,9 +382,24 @@ def check_connection(self, logger, config) -> Tuple[bool, any]:

# Try to initiate a stream and validate input date params
try:
# validate input date ranges
Transactions(authenticator=authenticator, **config).validate_input_dates()

# validate if Paypal is able to extract data for given start_data
start_date = isoparse(config["start_date"])
end_date = start_date + timedelta(days=1)
stream_slice = {
"start_date": start_date.isoformat(),
"end_date": end_date.isoformat(),
}
records = Transactions(authenticator=authenticator, **config).read_records(sync_mode="full", stream_slice=stream_slice)
list(records)
midavadim marked this conversation as resolved.
Show resolved Hide resolved

except Exception as e:
return False, e
if "Data for the given start date is not available" in repr(e):
midavadim marked this conversation as resolved.
Show resolved Hide resolved
return False, f"Data for the given start date ({config['start_date']}) is not available, please use more recent start date"
else:
return False, e

return True, None

Expand Down