Search in sources :

Example 51 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project sonarqube by SonarSource.

the class OAuthRestClient method executeRequest.

public static Response executeRequest(String requestUrl, OAuth20Service scribe, OAuth2AccessToken accessToken) throws IOException {
    OAuthRequest request = new OAuthRequest(Verb.GET, requestUrl);
    scribe.signRequest(accessToken, request);
    try {
        Response response = scribe.execute(request);
        if (!response.isSuccessful()) {
            throw unexpectedResponseCode(requestUrl, response);
        }
        return response;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException(e);
    } catch (ExecutionException e) {
        throw new IllegalStateException(e);
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) ExecutionException(java.util.concurrent.ExecutionException)

Example 52 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldReadResponseStream.

@Test
public void shouldReadResponseStream() throws Exception {
    final String expectedResponseBody = "response body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
    final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, StreamUtils.getStreamContents(response.getStream()));
    final RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET", recordedRequest.getMethod());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 53 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldSendPostRequest.

@Test
public void shouldSendPostRequest() throws Exception {
    final String expectedResponseBody = "response body";
    final String expectedRequestBody = "request body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    // request with body
    OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
    request.setPayload(expectedRequestBody);
    Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
    // request with empty body
    request = new OAuthRequest(Verb.POST, baseUrl.toString());
    response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertEquals("", recordedRequest.getBody().readUtf8());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 54 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldSendGetRequest.

@Test
public void shouldSendGetRequest() throws Exception {
    final String expectedResponseBody = "response body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
    final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    final RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET", recordedRequest.getMethod());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 55 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class ObjectMother method createSampleOAuthRequestPort80v2.

public static OAuthRequest createSampleOAuthRequestPort80v2() {
    final OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com:80/test");
    request.addOAuthParameter(OAuthConstants.TIMESTAMP, "123456");
    request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, "AS#$^*@&");
    request.addOAuthParameter(OAuthConstants.CALLBACK, "http://example/callback");
    request.addOAuthParameter(OAuthConstants.SIGNATURE, "OAuth-Signature");
    return request;
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest)

Aggregations

OAuthRequest (com.github.scribejava.core.model.OAuthRequest)107 Response (com.github.scribejava.core.model.Response)85 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)62 Scanner (java.util.Scanner)60 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)48 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)45 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)21 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)21 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)20 Random (java.util.Random)16 OAuthConfig (com.github.scribejava.core.model.OAuthConfig)13 Test (org.junit.Test)11 HttpUrl (okhttp3.HttpUrl)9 MockResponse (okhttp3.mockwebserver.MockResponse)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 DefaultApi20 (com.github.scribejava.core.builder.api.DefaultApi20)4 ExecutionException (java.util.concurrent.ExecutionException)4