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..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,10 +26,12 @@ 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; 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 +44,23 @@ 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(); + reader.setStatus(PixTransactionStatus.DONE).read().getData(); } 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..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 @@ -15,8 +15,11 @@ public abstract class Reader { - public Integer limit; - public Long offset; + 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; @@ -25,6 +28,9 @@ 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; } @@ -70,55 +76,77 @@ public void addFilter(String propertyName, String filterName) { )); } + public Reader nextPage() { + offset += limit; + return this; + } + private String buildFullPath() { + String path = getResourceUrl(); + path = path.concat(fillPagination()); + + String filters = applyFilters(); + if (filters != null) path = path.concat(filters); + + return path; + } + + 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; + } + + private String applyFilters() { + if (activeFilters == null || activeFilters.isEmpty()) return null; + try { - String path = getResourceUrl(); - if (activeFilters == null || activeFilters.isEmpty()) return path; + StringBuilder pathParams = new StringBuilder(); - String pathParams = ""; for (FilterVO filterVO : activeFilters) { - pathParams = concatDelimiterFilter(pathParams); + pathParams.append("&"); Field field = this.getClass().getDeclaredField(filterVO.getPropertyName()); - pathParams = pathParams - .concat(URLEncoder.encode(filterVO.getFilterKey())) - .concat("="); - - 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"); - } - } - if (limit != null) { - pathParams = concatDelimiterFilter(pathParams) - .concat("limit=") - .concat(limit.toString()); - } + pathParams = new StringBuilder(pathParams.toString() + .concat(parseFilterKey(filterVO)) + .concat("=")); - if (offset != null) { - pathParams = concatDelimiterFilter(pathParams) - .concat("offset=") - .concat(offset.toString()); + Object value = field.get(this); + pathParams = new StringBuilder(pathParams.toString().concat(parseFilterValue(value))); } - return path.concat(pathParams); - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | - IllegalAccessException unexpectedException) { + return pathParams.toString(); + } catch ( + NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException + unexpectedException + ) { throw new IllegalStateException("Erro ao parsear filtros."); } } - private String concatDelimiterFilter(String currentFilter) { - if (currentFilter.isEmpty()) return currentFilter.concat("?"); - return currentFilter.concat("&"); + 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"); } } 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(); }