Skip to content

Commit 9cf7d5c

Browse files
axshaniilyamerman
andauthored
get files as bytes (unit-finance#239)
Co-authored-by: Ilya Merman <[email protected]>
1 parent 9c561d4 commit 9cf7d5c

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

e2e_tests/statement_test.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@
66
token = os.environ.get('TOKEN')
77
client = Unit("https://api.s.unit.sh", token)
88

9+
910
def test_list_and_get_statements():
1011
statements = client.statements.list(ListStatementParams(2)).data
1112
for s in statements:
1213
assert s.type == "accountStatementDTO"
1314

1415
params = GetStatementParams(s.id)
1516
html_statement = client.statements.get(params).data
16-
assert "<!DOCTYPE html>" in html_statement
17+
assert "<!DOCTYPE html>" in str(html_statement)
1718

1819
params = GetStatementParams(s.id, customer_id=s.relationships["customer"].id)
1920
html_statement = client.statements.get(params).data
20-
assert "<!DOCTYPE html>" in html_statement
21+
assert "<!DOCTYPE html>" in str(html_statement)
2122

2223
account_id = s.relationships["account"].id
2324
pdf_response = client.statements.get_bank_verification(account_id).data
24-
assert "PDF" in pdf_response
25+
assert "PDF" in str(pdf_response)
2526

2627
params = GetStatementParams(s.id, "pdf")
2728
pdf_statement = client.statements.get(params).data
28-
assert "PDF" in pdf_statement
29-
30-
test_list_and_get_statements()
29+
assert "PDF" in str(pdf_statement)

unit/api/statement_resource.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,33 @@ class StatementResource(BaseResource):
88
def __init__(self, configuration: Configuration):
99
super().__init__("statements", configuration)
1010

11-
def get(self, params: GetStatementParams) -> Union[UnitResponse[str], UnitError]:
11+
def get(self, params: GetStatementParams) -> Union[UnitResponse[bytes], UnitError]:
1212
parameters = {"language": params.language}
1313
if params.customer_id:
1414
parameters["filter[customerId]"] = params.customer_id
1515

1616
response = super().get(f"{self.resource}/{params.statement_id}/{params.output_type}", parameters)
17-
if response.status_code == 200:
18-
return UnitResponse[str](response.text, None)
17+
18+
if super().is_20x(response.status_code):
19+
return UnitResponse[bytes](response.content, None)
1920
else:
2021
return UnitError.from_json_api(response.json())
2122

22-
def get_bank_verification(self, account_id: str, include_proof_of_funds: Optional[bool] = False) -> Union[UnitResponse[str], UnitError]:
23+
def get_bank_verification(self, account_id: str, include_proof_of_funds: Optional[bool] = False) -> \
24+
Union[UnitResponse[bytes], UnitError]:
2325
response = super().get(f"{self.resource}/{account_id}/bank/pdf",
2426
{"includeProofOfFunds": include_proof_of_funds})
25-
if response.status_code == 200:
26-
return UnitResponse[str](response.text, None)
27+
28+
if super().is_20x(response.status_code):
29+
return UnitResponse[bytes](response.content, None)
2730
else:
2831
return UnitError.from_json_api(response.json())
2932

3033
def list(self, params: ListStatementParams = None) -> Union[UnitResponse[List[StatementDTO]], UnitError]:
3134
params = params or ListStatementParams()
3235
response = super().get(self.resource, params.to_dict())
33-
if response.status_code == 200:
36+
37+
if super().is_20x(response.status_code):
3438
data = response.json().get("data")
3539
return UnitResponse[StatementDTO](DtoDecoder.decode(data), None)
3640
else:

0 commit comments

Comments
 (0)