use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project twittered by redouane59.
the class RequestHelperV2 method getAsyncRequest.
public <T> Future<Response> getAsyncRequest(String url, Map<String, String> parameters, IAPIEventListener listener, final Class<? extends T> targetClass) {
if (parameters != null) {
url += parameters.entrySet().stream().map(p -> p.getKey() + "=" + p.getValue()).reduce((p1, p2) -> p1 + "&" + p2).map(s -> "?" + s).orElse("");
}
OAuthRequest request = new OAuthRequest(Verb.GET, url);
signRequest(request);
return getService().execute(request, new OAuthAsyncRequestCallback<Response>() {
@Override
public void onThrowable(Throwable t) {
LOGGER.error(t.getMessage(), t);
}
@Override
public void onCompleted(Response response) {
try {
tweetStreamConsumer.consumeStream(listener, response, targetClass);
} catch (Exception e) {
onThrowable(e);
}
}
});
}
use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project android-oauth-handler by codepath.
the class OAuthTokenClient method fetchAccessToken.
// Get the access token by exchanging the requestToken to the defined URL
// Once receiving the access token, fires the onReceivedAccessToken method on the handler
public void fetchAccessToken(final Token requestToken, final Uri uri) {
Uri authorizedUri = uri;
if (service.getVersion() == "1.0") {
if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
OAuth10aService oAuth10aService = (OAuth10aService) service;
oAuth10aService.getAccessTokenAsync(oAuth1RequestToken, oauth_verifier, new OAuthAsyncRequestCallback<OAuth1AccessToken>() {
@Override
public void onCompleted(OAuth1AccessToken oAuth1AccessToken) {
setAccessToken(oAuth1AccessToken);
handler.onReceivedAccessToken(oAuth1AccessToken, service.getVersion());
}
@Override
public void onThrowable(Throwable e) {
handler.onFailure(new OAuthException(e.getMessage()));
}
});
} else {
// verifier was null
throw new OAuthException("No verifier code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
}
} else if (service.getVersion() == "2.0") {
if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
String code = authorizedUri.getQueryParameter(OAuthConstants.CODE);
OAuth20Service oAuth20Service = (OAuth20Service) service;
oAuth20Service.getAccessToken(code, new OAuthAsyncRequestCallback<OAuth2AccessToken>() {
@Override
public void onCompleted(OAuth2AccessToken accessToken) {
setAccessToken(accessToken);
handler.onReceivedAccessToken(accessToken, service.getVersion());
}
@Override
public void onThrowable(Throwable t) {
}
});
} else {
// verifier was null
handler.onFailure(new OAuthException("No code was returned with uri '" + uri + "' " + "and access token cannot be retrieved"));
}
}
}
use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project scribejava by scribejava.
the class ArmeriaHttpClient method doExecuteAsync.
private <T> CompletableFuture<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, Supplier<HttpData> contentSupplier, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
// Get the URI and Path
final URI uri = URI.create(completeUrl);
final String path = getServicePath(uri);
// Fetch/Create WebClient instance for a given Endpoint
final WebClient client = getClient(uri);
// Build HTTP request
final RequestHeadersBuilder headersBuilder = RequestHeaders.of(getHttpMethod(httpVerb), path).toBuilder();
headersBuilder.add(headers.entrySet());
if (userAgent != null) {
headersBuilder.add(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}
// Build the request body and execute HTTP request
final HttpResponse response;
if (httpVerb.isPermitBody()) {
// POST, PUT, PATCH and DELETE methods
final HttpData contents = contentSupplier.get();
if (httpVerb.isRequiresBody() && contents == null) {
// POST or PUT methods
throw new IllegalArgumentException("Contents missing for request method " + httpVerb.name());
}
if (headersBuilder.contentType() == null) {
headersBuilder.contentType(MediaType.FORM_DATA);
}
if (contents != null) {
response = client.execute(headersBuilder.build(), contents);
} else {
response = client.execute(headersBuilder.build());
}
} else {
response = client.execute(headersBuilder.build());
}
// Aggregate HTTP response (asynchronously) and return the result Future
return response.aggregate().thenApply(aggregatedResponse -> whenResponseComplete(callback, converter, aggregatedResponse)).exceptionally(throwable -> completeExceptionally(callback, throwable));
}
Aggregations