Created
March 18, 2014 21:35
-
-
Save mr-archano/9630241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gertherb.api; | |
import com.squareup.okhttp.internal.Base64; | |
import retrofit.Endpoint; | |
import retrofit.RequestInterceptor; | |
import retrofit.RestAdapter; | |
import static retrofit.RestAdapter.LogLevel.FULL; | |
public class GithubApiFactory { | |
private static final String X_OAUTH_BASIC = "x-oauth-basic"; | |
public GithubApi createAuthorizationApi(String username, String password) { | |
Endpoint endpoint = new Endpoint() { | |
@Override | |
public String getUrl() { | |
return "https://api.github.com"; | |
} | |
@Override | |
public String getName() { | |
return "Live"; | |
} | |
}; | |
RequestInterceptor interceptor = createAuthorizationRequestInterceptor(username, password); | |
RestAdapter restAdapter = createRestAdapter(endpoint, interceptor); | |
return restAdapter.create(GithubApi.class); | |
} | |
public GithubApi createLoggedInApi(String token) { | |
return createAuthorizationApi(token, X_OAUTH_BASIC); | |
} | |
private RequestInterceptor createAuthorizationRequestInterceptor(final String username, final String password) { | |
return new RequestInterceptor() { | |
@Override | |
public void intercept(RequestFacade request) { | |
request.addHeader("Accept", "application/vnd.github.v3+json"); | |
request.addHeader("Authorization", "Basic " + Base64.encode((username + ":" + password).getBytes())); | |
} | |
}; | |
} | |
private RestAdapter createRestAdapter(Endpoint endpoint, RequestInterceptor interceptor) { | |
return new RestAdapter.Builder() | |
.setEndpoint(endpoint) | |
.setRequestInterceptor(interceptor) | |
.setLogLevel(FULL) | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gertherb.api; | |
import rx.Observable; | |
import rx.Observer; | |
import rx.functions.Func1; | |
public class GithubApiPlayground { | |
private static final String CLIENT_ID = "GERTHERB_CLIENT_ID_HERE"; | |
private static final String CLIENT_SECRET = "GERTHERB_CLIENT_SECRET_HERE"; | |
private GithubApi api; | |
private GithubApiFactory githubApiFactory; | |
public void setUp() { | |
api = createApi(); | |
} | |
private GithubApi createApi() { | |
githubApiFactory = new GithubApiFactory(); | |
return githubApiFactory.createAuthorizationApi("hal9002", "haln1v1da"); | |
} | |
public void testGetsAuthorization() { | |
Observable<TokenResponse> authorize = api.authorize(CLIENT_ID, TokenRequest.from(CLIENT_SECRET)); | |
Observable<String> repos = authorize.flatMap(new Func1<TokenResponse, Observable<String>>() { | |
@Override | |
public Observable<String> call(TokenResponse tokenResponse) { | |
return githubApiFactory.createLoggedInApi(tokenResponse.getToken()).getUserDetails(); | |
} | |
}); | |
repos.subscribe(new Observer<String>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
@Override | |
public void onNext(String s) { | |
System.out.println(s); | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
GithubApiPlayground playground = new GithubApiPlayground(); | |
playground.setUp(); | |
playground.testGetsAuthorization(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment