Search in sources :

Example 76 with HttpUrl

use of okhttp3.HttpUrl in project BoltBot by DiscordBolt.

the class TwitchUser method getUserData.

public Optional<TwitchUserData> getUserData(String username) throws IOException {
    HttpUrl userURL = HttpUrl.parse(USER_INFO_URL).newBuilder().addQueryParameter("login", username).build();
    Request request = new Request.Builder().url(userURL).addHeader("Client-ID", api.getClientID()).addHeader("Accept", "application/vnd.twitchtv.v5+json").build();
    try (Response response = api.getClient().newCall(request).execute()) {
        TwitchUserDataResponse users = api.getGson().fromJson(response.body().string(), TwitchUserDataResponse.class);
        return users.getUsers().stream().findFirst();
    }
}
Also used : TwitchUserDataResponse(com.discordbolt.boltbot.system.twitch.objects.TwitchUserDataResponse) Response(okhttp3.Response) Request(okhttp3.Request) TwitchUserDataResponse(com.discordbolt.boltbot.system.twitch.objects.TwitchUserDataResponse) HttpUrl(okhttp3.HttpUrl)

Example 77 with HttpUrl

use of okhttp3.HttpUrl in project vimeo-networking-java by vimeo.

the class VimeoClient method getCodeGrantAuthorizationURI.

// </editor-fold>
// -----------------------------------------------------------------------------------------------------
// Authentication
// -----------------------------------------------------------------------------------------------------
// <editor-fold desc="Authentication">
/**
 * Provides a URI that can be opened in a web view that will prompt for login and permissions
 * or used currently logged in users credentials.
 * <p>
 * If the user accepts your app, they are redirected to your redirect_uri along with two parameters:
 * {@link Vimeo#CODE_GRANT_RESPONSE_TYPE} or {@link Vimeo#CODE_GRANT_STATE}
 *
 * @return The URI that should be opened in a web view
 * @see <a href="https://developer.vimeo.com/api/authentication#generate-redirect">Vimeo API Docs</a>
 */
@SuppressWarnings("WeakerAccess")
public String getCodeGrantAuthorizationURI() {
    mCurrentCodeGrantState = UUID.randomUUID().toString();
    // Will look like the following: https://api.vimeo.com/oauth/authorize?<UTF8 encoded params>
    final HttpUrl baseUrl = HttpUrl.parse(mConfiguration.getBaseUrl());
    final HttpUrl uri = new HttpUrl.Builder().scheme(baseUrl.scheme()).host(baseUrl.host()).encodedPath(Vimeo.CODE_GRANT_PATH).addQueryParameter(Vimeo.PARAMETER_REDIRECT_URI, mConfiguration.mCodeGrantRedirectURI).addQueryParameter(Vimeo.PARAMETER_RESPONSE_TYPE, Vimeo.CODE_GRANT_RESPONSE_TYPE).addQueryParameter(Vimeo.PARAMETER_STATE, mCurrentCodeGrantState).addQueryParameter(Vimeo.PARAMETER_SCOPE, mConfiguration.mScope).addQueryParameter(Vimeo.PARAMETER_CLIENT_ID, mConfiguration.mClientID).build();
    return uri.toString();
}
Also used : HttpUrl(okhttp3.HttpUrl)

Example 78 with HttpUrl

use of okhttp3.HttpUrl in project vimeo-networking-java by vimeo.

the class LoggingInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    if (ClientLogger.getLogLevel().ordinal() <= LogLevel.DEBUG.ordinal()) {
        Request request = chain.request();
        HttpUrl httpUrl = request.url();
        long t1 = System.nanoTime();
        ClientLogger.d("--------- REQUEST ---------");
        ClientLogger.d("METHOD: " + request.method());
        ClientLogger.d("ENDPOINT: " + httpUrl.encodedPath());
        try {
            if (request.body() != null && shouldLogVerbose()) {
                ClientLogger.v("QUERY: " + httpUrl.query());
                ClientLogger.v("--------- REQUEST BODY ---------");
                Request copy = request.newBuilder().build();
                Buffer requestBuffer = new Buffer();
                copy.body().writeTo(requestBuffer);
                verboseLogLongJson(requestBuffer.readUtf8());
                ClientLogger.v("--------- REQUEST BODY END ---------");
            }
        } catch (Exception e) {
            ClientLogger.e("Exception in LoggingInterceptor", e);
        }
        ClientLogger.d("--------- REQUEST END ---------");
        Response response = chain.proceed(request);
        long t2 = System.nanoTime();
        ClientLogger.d("--------- RESPONSE ---------");
        ClientLogger.d("ENDPOINT: " + httpUrl.encodedPath());
        ClientLogger.d("STATUS CODE: " + response.code());
        ClientLogger.d(String.format("REQUEST TIME: %.1fms", (t2 - t1) / 1e6d));
        String responseBodyString = response.body().string();
        if (shouldLogVerbose()) {
            ClientLogger.v("--------- RESPONSE BODY ---------");
            verboseLogLongJson(responseBodyString);
            ClientLogger.v("--------- RESPONSE BODY END ---------");
        }
        ClientLogger.d("--------- RESPONSE END ---------");
        // You need to reconstruct the body because it can only be read once 1/27/16 [KV]
        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), responseBodyString)).build();
    } else {
        return chain.proceed(chain.request());
    }
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) IOException(java.io.IOException)

Example 79 with HttpUrl

use of okhttp3.HttpUrl in project vimeo-networking-java by vimeo.

the class BaseUrlInterceptorTest method test_includeThenExcludePathsForBaseUrl_Reset_Exclues.

@Test
public void test_includeThenExcludePathsForBaseUrl_Reset_Exclues() throws Exception {
    HttpUrl httpUrl = HttpUrl.parse("http://localhost");
    mBaseUrlInterceptor.includePathsForBaseUrl(httpUrl, "/me", "/you");
    mBaseUrlInterceptor.resetBaseUrl();
    mBaseUrlInterceptor.excludePathsForBaseUrl(httpUrl, "/me", "/you");
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/me"), createTestUrlForPath(null)));
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/you"), createTestUrlForPath(null)));
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/them"), httpUrl));
}
Also used : HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 80 with HttpUrl

use of okhttp3.HttpUrl in project vimeo-networking-java by vimeo.

the class BaseUrlInterceptorTest method test_includePathsForBaseUrl_PreSanitizedPaths_SuccessfullyIntercepts.

@Test
public void test_includePathsForBaseUrl_PreSanitizedPaths_SuccessfullyIntercepts() throws Exception {
    HttpUrl httpUrl = HttpUrl.parse("http://localhost");
    mBaseUrlInterceptor.includePathsForBaseUrl(httpUrl, "/me", "/you");
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/me"), httpUrl));
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/you"), httpUrl));
    mBaseUrlInterceptor.intercept(createVerificationChain(createTestUrlForPath("/them"), createTestUrlForPath(null)));
}
Also used : HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

HttpUrl (okhttp3.HttpUrl)302 Request (okhttp3.Request)130 Test (org.junit.Test)86 Response (okhttp3.Response)69 IOException (java.io.IOException)61 MockResponse (okhttp3.mockwebserver.MockResponse)40 Cookie (okhttp3.Cookie)35 ArrayList (java.util.ArrayList)28 OkHttpClient (okhttp3.OkHttpClient)27 MockWebServer (okhttp3.mockwebserver.MockWebServer)26 InputStream (java.io.InputStream)21 InputStreamReader (java.io.InputStreamReader)20 HashMap (java.util.HashMap)19 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 RequestBody (okhttp3.RequestBody)18 JsonParseException (com.google.gson.JsonParseException)13 File (java.io.File)12 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)10 List (java.util.List)10 Test (org.junit.jupiter.api.Test)10