From e9c3fa8b91316320db26df4b8d03581df98d7ca4 Mon Sep 17 00:00:00 2001 From: Joao Pedro Truchinski Borba Date: Wed, 20 Nov 2024 23:24:55 -0300 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20pagina=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/jpdev/asaassdk/doc/Examples.java | 20 +++- .../jpdev/asaassdk/rest/action/Reader.java | 36 +++--- .../rest/pix/transaction/PixTransaction.java | 108 ++++++++++++++++++ 3 files changed, 149 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java index 5af41ec..f91f3a8 100644 --- a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java +++ b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java @@ -30,6 +30,7 @@ import io.github.jpdev.asaassdk.rest.pix.qrcode.PixQrCode; import io.github.jpdev.asaassdk.rest.pix.qrcode.decode.PixDecodedQrCode; import io.github.jpdev.asaassdk.rest.pix.transaction.PixTransaction; +import io.github.jpdev.asaassdk.rest.pix.transaction.PixTransactionReader; import io.github.jpdev.asaassdk.rest.subscription.Subscription; import io.github.jpdev.asaassdk.rest.subscription.SubscriptionCycle; import io.github.jpdev.asaassdk.rest.transfer.Transfer; @@ -42,13 +43,30 @@ import io.github.jpdev.asaassdk.utils.Money; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.List; public class Examples { public static void main(String[] args) { Asaas.initSandbox(Secret.getAccessToken()); // Initialize the SDK with your access token - transfer(); + paging(); + } + + private static void paging() { + PixTransactionReader reader = PixTransaction.reader(); + ResourceSet page0 = reader.read(); + ResourceSet page1 = reader.nextPage().read(); + + ArrayList pixTransactions = new ArrayList<>(); + pixTransactions.addAll(page0.getData()); + pixTransactions.addAll(page1.getData()); + + for (PixTransaction pixTransaction : pixTransactions) { + System.out.println(pixTransaction.getId().equals("6b212665-4963-460d-9005-3805281790a4")); + } } private static void pixTransaction() { diff --git a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java index 16678c9..9b70328 100644 --- a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java +++ b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java @@ -15,8 +15,8 @@ public abstract class Reader { - public Integer limit; - public Long offset; + public int limit = 10; + public long offset = 0; public List activeFilters; @@ -70,9 +70,16 @@ public void addFilter(String propertyName, String filterName) { )); } + public Reader nextPage() { + offset += limit; + return this; + } + private String buildFullPath() { try { String path = getResourceUrl(); + path = path.concat(fillPagination()); + if (activeFilters == null || activeFilters.isEmpty()) return path; String pathParams = ""; @@ -98,18 +105,6 @@ private String buildFullPath() { } } - if (limit != null) { - pathParams = concatDelimiterFilter(pathParams) - .concat("limit=") - .concat(limit.toString()); - } - - if (offset != null) { - pathParams = concatDelimiterFilter(pathParams) - .concat("offset=") - .concat(offset.toString()); - } - return path.concat(pathParams); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException unexpectedException) { @@ -121,4 +116,17 @@ private String concatDelimiterFilter(String currentFilter) { if (currentFilter.isEmpty()) return currentFilter.concat("?"); return currentFilter.concat("&"); } + + private String fillPagination() { + String pathParams = ""; + pathParams = concatDelimiterFilter(pathParams) + .concat("limit=") + .concat(String.valueOf(limit)); + + pathParams = concatDelimiterFilter(pathParams) + .concat("offset=") + .concat(String.valueOf(offset)); + + return pathParams; + } } diff --git a/src/main/java/io/github/jpdev/asaassdk/rest/pix/transaction/PixTransaction.java b/src/main/java/io/github/jpdev/asaassdk/rest/pix/transaction/PixTransaction.java index 3bfabda..50ec339 100644 --- a/src/main/java/io/github/jpdev/asaassdk/rest/pix/transaction/PixTransaction.java +++ b/src/main/java/io/github/jpdev/asaassdk/rest/pix/transaction/PixTransaction.java @@ -45,6 +45,114 @@ public PixTransaction() { } + public String getId() { + return id; + } + + public Object getTransferId() { + return transferId; + } + + public String getEndToEndIdentifier() { + return endToEndIdentifier; + } + + public Object getFinality() { + return finality; + } + + public BigDecimal getValue() { + return value; + } + + public BigDecimal getChangeValue() { + return changeValue; + } + + public BigDecimal getRefundedValue() { + return refundedValue; + } + + public Date getDateCreated() { + return dateCreated; + } + + public Date getEffectiveDate() { + return effectiveDate; + } + + public Date getScheduledDate() { + return scheduledDate; + } + + public PixTransactionStatus getStatus() { + return status; + } + + public PixTransactionType getType() { + return type; + } + + public PixTransactionOriginType getOriginType() { + return originType; + } + + public String getConciliationIdentifier() { + return conciliationIdentifier; + } + + public String getDescription() { + return description; + } + + public String getTransactionReceiptUrl() { + return transactionReceiptUrl; + } + + public int getChargedFeeValue() { + return chargedFeeValue; + } + + public boolean isCanBeRefunded() { + return canBeRefunded; + } + + public String getRefundDisabledReason() { + return refundDisabledReason; + } + + public String getRefusalReason() { + return refusalReason; + } + + public boolean isCanBeCanceled() { + return canBeCanceled; + } + + public Object getOriginalTransaction() { + return originalTransaction; + } + + public PixTransactionExternalAccount getExternalAccount() { + return externalAccount; + } + + public Object getQrCode() { + return qrCode; + } + + public Object getPayment() { + return payment; + } + + public String getAddressKey() { + return addressKey; + } + + public PixAddressKeyType getAddressKeyType() { + return addressKeyType; + } + public static PixTransactionReader reader() { return new PixTransactionReader(); } From 0d6fb9602116d8ffec1b864b8276bbe108f6f616 Mon Sep 17 00:00:00 2001 From: Joao Pedro Truchinski Borba Date: Wed, 20 Nov 2024 23:30:20 -0300 Subject: [PATCH 2/5] chore: validate max allowed value --- src/main/java/io/github/jpdev/asaassdk/doc/Examples.java | 8 -------- .../java/io/github/jpdev/asaassdk/rest/action/Reader.java | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java index f91f3a8..cb93f4e 100644 --- a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java +++ b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java @@ -59,14 +59,6 @@ private static void paging() { PixTransactionReader reader = PixTransaction.reader(); ResourceSet page0 = reader.read(); ResourceSet page1 = reader.nextPage().read(); - - ArrayList pixTransactions = new ArrayList<>(); - pixTransactions.addAll(page0.getData()); - pixTransactions.addAll(page1.getData()); - - for (PixTransaction pixTransaction : pixTransactions) { - System.out.println(pixTransaction.getId().equals("6b212665-4963-460d-9005-3805281790a4")); - } } private static void pixTransaction() { diff --git a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java index 9b70328..f7e8fbb 100644 --- a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java +++ b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java @@ -18,6 +18,8 @@ public abstract class Reader { public int limit = 10; public long offset = 0; + private static final int LIMIT_MAX_VALUE = 100; + public List activeFilters; public Integer getLimit() { @@ -25,6 +27,8 @@ public Integer getLimit() { } public Reader setLimit(Integer limit) { + if (limit > LIMIT_MAX_VALUE) throw new IllegalArgumentException("Limit cannot be greater than " + LIMIT_MAX_VALUE); + this.limit = limit; return this; } From 42679dbd45a0dfffceaaeb7d0bca3e1af8507386 Mon Sep 17 00:00:00 2001 From: Joao Pedro Truchinski Borba Date: Wed, 20 Nov 2024 23:31:04 -0300 Subject: [PATCH 3/5] chore: validate max allowed value --- src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java index f7e8fbb..0e67db6 100644 --- a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java +++ b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java @@ -18,6 +18,7 @@ public abstract class Reader { public int limit = 10; public long offset = 0; + private static final int LIMIT_MIN_VALUE = 1; private static final int LIMIT_MAX_VALUE = 100; public List activeFilters; @@ -28,6 +29,7 @@ public Integer getLimit() { public Reader setLimit(Integer limit) { if (limit > LIMIT_MAX_VALUE) throw new IllegalArgumentException("Limit cannot be greater than " + LIMIT_MAX_VALUE); + if (limit < LIMIT_MIN_VALUE) throw new IllegalArgumentException("Limit cannot be less than " + LIMIT_MIN_VALUE); this.limit = limit; return this; From f05a19d42ecae37d79505729cad8979ddfdc29c3 Mon Sep 17 00:00:00 2001 From: Joao Pedro Truchinski Borba Date: Wed, 20 Nov 2024 23:42:08 -0300 Subject: [PATCH 4/5] chore: improving code --- .../jpdev/asaassdk/rest/action/Reader.java | 78 +++++++++++-------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java index 0e67db6..c01b0c9 100644 --- a/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java +++ b/src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java @@ -82,40 +82,13 @@ public Reader nextPage() { } private String buildFullPath() { - try { - String path = getResourceUrl(); - path = path.concat(fillPagination()); - - if (activeFilters == null || activeFilters.isEmpty()) return path; - - String pathParams = ""; - for (FilterVO filterVO : activeFilters) { - pathParams = concatDelimiterFilter(pathParams); - Field field = this.getClass().getDeclaredField(filterVO.getPropertyName()); - pathParams = pathParams - .concat(URLEncoder.encode(filterVO.getFilterKey())) - .concat("="); + String path = getResourceUrl(); + path = path.concat(fillPagination()); - Object value = field.get(this); - if (value instanceof String || value instanceof Enum) { - pathParams = pathParams - .concat(value.toString()); - } else if (value instanceof Integer) { - pathParams = pathParams - .concat(value.toString()); - } else if (value instanceof Date) { - pathParams = pathParams - .concat(CustomDateUtils.toString((Date) value, CustomDateUtils.DATE)); - } else { - throw new IllegalStateException("Filtro não mapeado"); - } - } + String filters = applyFilters(); + if (filters != null) path = path.concat(filters); - return path.concat(pathParams); - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | - IllegalAccessException unexpectedException) { - throw new IllegalStateException("Erro ao parsear filtros."); - } + return path; } private String concatDelimiterFilter(String currentFilter) { @@ -135,4 +108,45 @@ private String fillPagination() { return pathParams; } + + private String applyFilters() { + if (activeFilters == null || activeFilters.isEmpty()) return null; + + try { + StringBuilder pathParams = new StringBuilder(); + + for (FilterVO filterVO : activeFilters) { + pathParams.append("&"); + Field field = this.getClass().getDeclaredField(filterVO.getPropertyName()); + + pathParams = new StringBuilder(pathParams.toString() + .concat(parseFilterKey(filterVO)) + .concat("=")); + + Object value = field.get(this); + pathParams = new StringBuilder(pathParams.toString().concat(parseFilterValue(value))); + } + + return pathParams.toString(); + } catch ( + NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException + unexpectedException + ) { + throw new IllegalStateException("Erro ao parsear filtros."); + } + } + + private String parseFilterKey(FilterVO filterVO) { + return URLEncoder.encode(filterVO.getFilterKey()); + } + + private String parseFilterValue(Object value) { + if (value instanceof String || value instanceof Enum || value instanceof Integer) { + return value.toString(); + } else if (value instanceof Date) { + return CustomDateUtils.toString((Date) value, CustomDateUtils.DATE); + } + + throw new IllegalStateException("Filtro não mapeado"); + } } From 21ff664b58bd228cd5c1857aa13b97c8b177e588 Mon Sep 17 00:00:00 2001 From: Joao Pedro Truchinski Borba Date: Wed, 20 Nov 2024 23:42:18 -0300 Subject: [PATCH 5/5] chore: improving code --- src/main/java/io/github/jpdev/asaassdk/doc/Examples.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java index cb93f4e..eabcafc 100644 --- a/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java +++ b/src/main/java/io/github/jpdev/asaassdk/doc/Examples.java @@ -26,6 +26,7 @@ import io.github.jpdev.asaassdk.rest.pix.addresskey.PixAddressKey; import io.github.jpdev.asaassdk.rest.pix.enums.PixAddressKeyStatus; import io.github.jpdev.asaassdk.rest.pix.enums.PixAddressKeyType; +import io.github.jpdev.asaassdk.rest.pix.enums.PixTransactionStatus; import io.github.jpdev.asaassdk.rest.pix.enums.PixTransactionType; import io.github.jpdev.asaassdk.rest.pix.qrcode.PixQrCode; import io.github.jpdev.asaassdk.rest.pix.qrcode.decode.PixDecodedQrCode; @@ -59,6 +60,7 @@ private static void paging() { PixTransactionReader reader = PixTransaction.reader(); ResourceSet page0 = reader.read(); ResourceSet page1 = reader.nextPage().read(); + reader.setStatus(PixTransactionStatus.DONE).read().getData(); } private static void pixTransaction() {