Skip to content

feature: Async request interceptor #11

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Binary file modified dist/chargebee-java-2.6.8.jar
Binary file not shown.
8 changes: 8 additions & 0 deletions src/main/java/com/chargebee/AsyncRequestInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.chargebee;

import com.chargebee.internal.AsyncResult;

public interface AsyncRequestInterceptor {

public AsyncResult handleRequest(AsyncRequestWrap requestWrap) throws Exception;
}
21 changes: 21 additions & 0 deletions src/main/java/com/chargebee/AsyncRequestWrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.chargebee;

import com.chargebee.internal.HttpUtil;
import com.chargebee.internal.ListRequest;
import com.chargebee.internal.RequestBase;

public class AsyncRequestWrap<T extends RequestBase> {
public final Environment env;
public final T request;
public final boolean isListRequest;
public final HttpUtil.Method method;
public final String uri;

public AsyncRequestWrap(Environment env, T request, HttpUtil.Method method, String uri) {
this.env = env;
this.request = request;
this.method = method;
this.isListRequest = request instanceof ListRequest;
this.uri = uri;
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/chargebee/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Environment {
private static Environment defaultEnv; // singleton

private RequestInterceptor reqInterceptor;

private AsyncRequestInterceptor asyncReqInterceptor;

public Environment(String siteName, String apiKey) {
this(siteName, apiKey, null);
Expand All @@ -57,14 +59,22 @@ public static void configure(String siteName, String apikey) {
Environment.defaultEnv = new Environment(siteName, apikey);
}

public static void reqInterceptor(RequestInterceptor reqInterceptor) {
defaultConfig().reqInterceptor = reqInterceptor;
public void reqInterceptor(RequestInterceptor reqInterceptor) {
this.reqInterceptor = reqInterceptor;
}

public void asyncReqInterceptor(AsyncRequestInterceptor asyncReqInterceptor) {
this.asyncReqInterceptor = asyncReqInterceptor;
}

public RequestInterceptor reqInterceptor() {
return reqInterceptor;
}

public AsyncRequestInterceptor asyncReqInterceptor() {
return asyncReqInterceptor;
}

public static Environment defaultConfig() {
if(defaultEnv == null) {
throw new RuntimeException("The default environment has not been configured");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/chargebee/internal/AsyncResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.chargebee.internal;

public interface AsyncResult {
}
9 changes: 9 additions & 0 deletions src/main/java/com/chargebee/internal/ListRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chargebee.internal;

import com.chargebee.AsyncRequestWrap;
import com.chargebee.RequestWrap;
import com.chargebee.Environment;
import com.chargebee.ListResult;
Expand Down Expand Up @@ -59,6 +60,14 @@ public ListResult call() throws Exception {
return (ListResult) (env.reqInterceptor() != null ? env.reqInterceptor().handleRequest(c) : c.call());

}

public final AsyncResult asyncRequest(Environment env) throws IOException, Exception {
if(env.asyncReqInterceptor() == null) {
throw new RuntimeException("Async Request interceptor cannot be null");
}
return env.asyncReqInterceptor().handleRequest(new AsyncRequestWrap(env, this, HttpUtil.Method.GET, this.uri));

}

private static ListResult _request(Environment env, ListRequest req) throws IOException {
if (env == null) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/chargebee/internal/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public Result call() throws Exception {
return (Result) (env.reqInterceptor() != null ? env.reqInterceptor().handleRequest(c) : c.call());
}

public final AsyncResult asyncRequest(Environment env) throws Exception {
if(env.asyncReqInterceptor() == null) {
throw new RuntimeException("Async Request interceptor cannot be null");
}
return env.asyncReqInterceptor().handleRequest(new AsyncRequestWrap(env, this, this.httpMeth, this.uri));
}

private static Result _request(Environment env, Request<?> req) throws IOException {
if (env == null) {
throw new RuntimeException("Environment cannot be null");
Expand All @@ -52,5 +59,4 @@ private static Result _request(Environment env, Request<?> req) throws IOExcepti
public Params params() {
return params;
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/chargebee/internal/RequestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public Params params() {
return params;
}

public Map<String,String> headers() {
return headers;
}

public U header(String headerName,String headerValue){
headers.put(headerName, headerValue);
return (U)this;
Expand Down
80 changes: 75 additions & 5 deletions src/main/java/com/chargebee/models/Invoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,9 @@ public static ChargeAddonRequest chargeAddon() throws IOException {
return new ChargeAddonRequest(Method.POST, uri);
}

public static Request stopDunning(String id) throws IOException {
public static StopDunningRequest stopDunning(String id) throws IOException {
String uri = uri("invoices", nullCheck(id), "stop_dunning");
return new Request(Method.POST, uri);
return new StopDunningRequest(Method.POST, uri);
}

public static ImportInvoiceRequest importInvoice() throws IOException {
Expand Down Expand Up @@ -864,9 +864,9 @@ public static AddAddonChargeRequest addAddonCharge(String id) throws IOException
return new AddAddonChargeRequest(Method.POST, uri);
}

public static Request close(String id) throws IOException {
public static CloseRequest close(String id) throws IOException {
String uri = uri("invoices", nullCheck(id), "close");
return new Request(Method.POST, uri);
return new CloseRequest(Method.POST, uri);
}

public static CollectPaymentRequest collectPayment(String id) throws IOException {
Expand Down Expand Up @@ -1217,6 +1217,24 @@ public Params params() {
}
}

public static class StopDunningRequest extends Request<StopDunningRequest> {

private StopDunningRequest(Method httpMeth, String uri) {
super(httpMeth, uri);
}

public StopDunningRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


@Override
public Params params() {
return params;
}
}

public static class ImportInvoiceRequest extends Request<ImportInvoiceRequest> {

private ImportInvoiceRequest(Method httpMeth, String uri) {
Expand Down Expand Up @@ -1642,6 +1660,12 @@ private ApplyPaymentsRequest(Method httpMeth, String uri) {
super(httpMeth, uri);
}

public ApplyPaymentsRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


public ApplyPaymentsRequest transactionId(int index, String transactionId) {
params.addOpt("transactions[id][" + index + "]", transactionId);
return this;
Expand All @@ -1657,7 +1681,13 @@ public static class ApplyCreditsRequest extends Request<ApplyCreditsRequest> {
private ApplyCreditsRequest(Method httpMeth, String uri) {
super(httpMeth, uri);
}


public ApplyCreditsRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


public ApplyCreditsRequest creditNoteId(int index, String creditNoteId) {
params.addOpt("credit_notes[id][" + index + "]", creditNoteId);
return this;
Expand Down Expand Up @@ -1838,6 +1868,10 @@ public AddChargeRequest avalaraServiceType(Integer avalaraServiceType) {
return this;
}

public AddChargeRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}

public AddChargeRequest lineItemDateFrom(Timestamp lineItemDateFrom) {
params.addOpt("line_item[date_from]", lineItemDateFrom);
Expand Down Expand Up @@ -1879,6 +1913,12 @@ public AddAddonChargeRequest addonUnitPrice(Integer addonUnitPrice) {
}


public AddAddonChargeRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


public AddAddonChargeRequest lineItemDateFrom(Timestamp lineItemDateFrom) {
params.addOpt("line_item[date_from]", lineItemDateFrom);
return this;
Expand All @@ -1895,6 +1935,24 @@ public Params params() {
}
}

public static class CloseRequest extends Request<CloseRequest> {

private CloseRequest(Method httpMeth, String uri) {
super(httpMeth, uri);
}

public CloseRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


@Override
public Params params() {
return params;
}
}

public static class CollectPaymentRequest extends Request<CollectPaymentRequest> {

private CollectPaymentRequest(Method httpMeth, String uri) {
Expand All @@ -1919,6 +1977,12 @@ public CollectPaymentRequest paymentSourceId(String paymentSourceId) {
}


public CollectPaymentRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


@Override
public Params params() {
return params;
Expand Down Expand Up @@ -2173,6 +2237,12 @@ public UpdateDetailsRequest poNumber(String poNumber) {
}


public UpdateDetailsRequest comment(String comment) {
params.addOpt("comment", comment);
return this;
}


public UpdateDetailsRequest billingAddressFirstName(String billingAddressFirstName) {
params.addOpt("billing_address[first_name]", billingAddressFirstName);
return this;
Expand Down