Skip to content

Commit

Permalink
Merge pull request #124 from ohtake/connector
Browse files Browse the repository at this point in the history
Allow to use custom HttpConnector when only OAuth token is given
  • Loading branch information
kohsuke committed Sep 4, 2014
2 parents 09557df + d6627b1 commit 95fbf92
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 30 deletions.
44 changes: 14 additions & 30 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
Expand All @@ -42,12 +40,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -74,13 +70,6 @@ public class GitHub {

private HttpConnector connector = HttpConnector.DEFAULT;

/**
* Connects to GitHub.com
*/
private GitHub(String login, String oauthAccessToken, String password) throws IOException {
this (GITHUB_URL, login, oauthAccessToken, password);
}

/**
* Creates a client API root object.
*
Expand Down Expand Up @@ -114,10 +103,13 @@ private GitHub(String login, String oauthAccessToken, String password) throws IO
* Secret OAuth token.
* @param password
* User's password. Always used in conjunction with the {@code login} parameter
* @param connector
* HttpConnector to use. Pass null to use default connector.
*/
private GitHub(String apiUrl, String login, String oauthAccessToken, String password) throws IOException {
/* package */ GitHub(String apiUrl, String login, String oauthAccessToken, String password, HttpConnector connector) throws IOException {
if (apiUrl.endsWith("/")) apiUrl = apiUrl.substring(0, apiUrl.length()-1); // normalize
this.apiUrl = apiUrl;
if (null != connector) this.connector = connector;

if (oauthAccessToken!=null) {
encodedAuthorization = "token "+oauthAccessToken;
Expand All @@ -139,15 +131,7 @@ private GitHub(String apiUrl, String login, String oauthAccessToken, String pass
* Obtains the credential from "~/.github"
*/
public static GitHub connect() throws IOException {
Properties props = new Properties();
File homeDir = new File(System.getProperty("user.home"));
FileInputStream in = new FileInputStream(new File(homeDir, ".github"));
try {
props.load(in);
} finally {
IOUtils.closeQuietly(in);
}
return new GitHub(GITHUB_URL,props.getProperty("login"), props.getProperty("oauth"),props.getProperty("password"));
return GitHubBuilder.fromPropertyFile().build();
}

/**
Expand All @@ -159,15 +143,15 @@ public static GitHub connect() throws IOException {
* For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated.
*/
public static GitHub connectToEnterprise(String apiUrl, String oauthAccessToken) throws IOException {
return connectUsingOAuth(apiUrl, oauthAccessToken);
return new GitHubBuilder().withEndpoint(apiUrl).withOAuthToken(oauthAccessToken).build();
}

public static GitHub connectToEnterprise(String apiUrl, String login, String password) throws IOException {
return new GitHub(apiUrl, login, null, password);
return new GitHubBuilder().withEndpoint(apiUrl).withPassword(login, password).build();
}

public static GitHub connect(String login, String oauthAccessToken) throws IOException {
return new GitHub(login,oauthAccessToken,null);
return new GitHubBuilder().withOAuthToken(oauthAccessToken, login).build();
}

/**
Expand All @@ -176,27 +160,27 @@ public static GitHub connect(String login, String oauthAccessToken) throws IOExc
* Use {@link #connectUsingPassword(String, String)} or {@link #connectUsingOAuth(String)}.
*/
public static GitHub connect(String login, String oauthAccessToken, String password) throws IOException {
return new GitHub(login,oauthAccessToken,password);
return new GitHubBuilder().withOAuthToken(oauthAccessToken, login).withPassword(login, password).build();
}

public static GitHub connectUsingPassword(String login, String password) throws IOException {
return new GitHub(login,null,password);
return new GitHubBuilder().withPassword(login, password).build();
}

public static GitHub connectUsingOAuth(String oauthAccessToken) throws IOException {
return new GitHub(null, oauthAccessToken, null);
return new GitHubBuilder().withOAuthToken(oauthAccessToken).build();
}

public static GitHub connectUsingOAuth(String githubServer, String oauthAccessToken) throws IOException {
return new GitHub(githubServer,null, oauthAccessToken,null);
return new GitHubBuilder().withEndpoint(githubServer).withOAuthToken(oauthAccessToken).build();
}
/**
* Connects to GitHub anonymously.
*
* All operations that requires authentication will fail.
*/
public static GitHub connectAnonymously() throws IOException {
return new GitHub(null,null,null);
return new GitHubBuilder().build();
}

/**
Expand Down Expand Up @@ -475,5 +459,5 @@ public boolean isCredentialValid() throws IOException {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

private static final String GITHUB_URL = "https://api.github.com";
/* package */ static final String GITHUB_URL = "https://api.github.com";
}
68 changes: 68 additions & 0 deletions src/main/java/org/kohsuke/github/GitHubBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.kohsuke.github;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
* @since 1.59
*/
public class GitHubBuilder {
private String endpoint = GitHub.GITHUB_URL;
private String user;
private String password;
private String oauthToken;
private HttpConnector connector;

public GitHubBuilder() {
}

public static GitHubBuilder fromPropertyFile() throws IOException {
File homeDir = new File(System.getProperty("user.home"));
File propertyFile = new File(homeDir, ".github");
return fromPropertyFile(propertyFile.getPath());
}
public static GitHubBuilder fromPropertyFile(String propertyFileName) throws IOException {
Properties props = new Properties();
FileInputStream in = null;
try {
in = new FileInputStream(propertyFileName);
props.load(in);
} finally {
IOUtils.closeQuietly(in);
}
GitHubBuilder self = new GitHubBuilder();
self.withOAuthToken(props.getProperty("oauth"), props.getProperty("login"));
self.withPassword(props.getProperty("login"), props.getProperty("password"));
return self;
}

public GitHubBuilder withEndpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
public GitHubBuilder withPassword(String user, String password) {
this.user = user;
this.password = password;
return this;
}
public GitHubBuilder withOAuthToken(String oauthToken) {
return withOAuthToken(oauthToken, null);
}
public GitHubBuilder withOAuthToken(String oauthToken, String user) {
this.oauthToken = oauthToken;
this.user = user;
return this;
}
public GitHubBuilder withConnector(HttpConnector connector) {
this.connector = connector;
return this;
}

public GitHub build() throws IOException {
return new GitHub(endpoint, user, oauthToken, password, connector);
}
}

0 comments on commit 95fbf92

Please sign in to comment.