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

Add facility to customize HttpRequests #48

Open
wants to merge 1 commit 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
28 changes: 28 additions & 0 deletions src/main/java/com/asana/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ public static Client basicAuth(String apiKey, HttpTransport httpTransport) {
return new Client(new BasicAuthDispatcher(apiKey, httpTransport));
}

/**
* @param apiKey Basic Auth API key
* @param requestInitializer The request initializer to use for requests
* @return Client instance
*/
public static Client basicAuth(String apiKey, HttpRequestInitializer requestInitializer) {
return new Client(new BasicAuthDispatcher(apiKey, requestInitializer));
}

/**
* @param accessToken Personal Access Token
* @return Client instance
Expand All @@ -252,6 +261,15 @@ public static Client accessToken(String accessToken, HttpTransport httpTransport
return new Client(new AccessTokenDispatcher(accessToken, httpTransport));
}

/**
* @param accessToken Personal Access Token
* @param requestInitializer The request initializer to use for requests
* @return Client instance
*/
public static Client accessToken(String accessToken, HttpRequestInitializer requestInitializer) {
return new Client(new AccessTokenDispatcher(accessToken, requestInitializer));
}

/**
* @param app OAuth application instance
* @return Client instance
Expand All @@ -268,4 +286,14 @@ public static Client oauth(OAuthApp app) {
public static Client oauth(OAuthApp app, HttpTransport httpTransport) {
return new Client(new OAuthDispatcher(app, httpTransport));
}

/**
* @param app OAuth application instance
* @param requestInitializer The request initializer to use for requests
* @return Client instance
*/
public static Client oauth(OAuthApp app, HttpRequestInitializer requestInitializer) {
return new Client(new OAuthDispatcher(app, requestInitializer));
}

}
10 changes: 6 additions & 4 deletions src/main/java/com/asana/dispatcher/AccessTokenDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.asana.dispatcher;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;

import java.io.IOException;

Expand All @@ -16,6 +13,11 @@ public AccessTokenDispatcher(String accessToken) {
this.accessToken = accessToken;
}

public AccessTokenDispatcher(String accessToken, HttpRequestInitializer requestInitializer) {
super(requestInitializer);
this.accessToken = accessToken;
}

public AccessTokenDispatcher(String accessToken, HttpTransport httpTransport) {
super(httpTransport);
this.accessToken = accessToken;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/asana/dispatcher/BasicAuthDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.asana.dispatcher;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;

import java.io.IOException;

Expand All @@ -15,6 +12,11 @@ public BasicAuthDispatcher(String apiKey) {
this.apiKey = apiKey;
}

public BasicAuthDispatcher(String apiKey, HttpRequestInitializer requestInitializer) {
super(requestInitializer);
this.apiKey = apiKey;
}

public BasicAuthDispatcher(String apiKey, HttpTransport httpTransport) {
super(httpTransport);
this.apiKey = apiKey;
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/asana/dispatcher/Dispatcher.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
package com.asana.dispatcher;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.IOException;

public abstract class Dispatcher {
protected HttpTransport httpTransport;
protected HttpRequestInitializer httpRequestInitializer;


public Dispatcher() {
this.httpTransport = new NetHttpTransport();
this(new NetHttpTransport());
}

public Dispatcher(HttpRequestInitializer requestInitializer) {
this(new NetHttpTransport(), requestInitializer);
}

public Dispatcher(HttpTransport httpTransport) {
this(httpTransport, null);
}

public Dispatcher(HttpTransport httpTransport, HttpRequestInitializer requestInitializer) {
this.httpTransport = httpTransport;
this.httpRequestInitializer = requestInitializer;
}

public HttpRequest buildRequest(String method, GenericUrl url, HttpContent content) throws IOException {
return httpTransport.createRequestFactory().buildRequest(method, url, content);
HttpRequestFactory requestFactory;
if(httpRequestInitializer == null) {
requestFactory = httpTransport.createRequestFactory();
} else {
requestFactory = httpTransport.createRequestFactory(httpRequestInitializer);
}
return requestFactory.buildRequest(method, url, content);
}

public void sleep(long millis) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/asana/dispatcher/OAuthDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.asana.dispatcher;

import com.asana.OAuthApp;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;

import java.io.IOException;

Expand All @@ -16,6 +13,11 @@ public OAuthDispatcher(OAuthApp app) {
this.app = app;
}

public OAuthDispatcher(OAuthApp app, HttpRequestInitializer requestInitializer) {
super(requestInitializer);
this.app = app;
}

public OAuthDispatcher(OAuthApp app, HttpTransport httpTransport) {
super(httpTransport);
this.app = app;
Expand Down