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

1.6 snapshot #17

Merged
merged 5 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion src/main/java/io/github/jpdev/asaassdk/doc/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PixTransaction> page0 = reader.read();
ResourceSet<PixTransaction> page1 = reader.nextPage().read();
reader.setStatus(PixTransactionStatus.DONE).read().getData();
}

private static void pixTransaction() {
Expand Down
106 changes: 67 additions & 39 deletions src/main/java/io/github/jpdev/asaassdk/rest/action/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

public abstract class Reader<T> {

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<FilterVO> activeFilters;

Expand All @@ -25,6 +28,9 @@ public Integer getLimit() {
}

public Reader<T> 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;
}
Expand Down Expand Up @@ -70,55 +76,77 @@ public void addFilter(String propertyName, String filterName) {
));
}

public Reader<T> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down