Search in sources :

Example 1 with Telemetry

use of com.auth0.net.Telemetry in project auth0-java by auth0.

the class ManagementAPI method buildNetworkingClient.

/**
 * Given a set of options, it creates a new instance of the {@link OkHttpClient}
 * configuring them according to their availability.
 *
 * @param options the options to set to the client.
 * @return a new networking client instance configured as requested.
 */
private OkHttpClient buildNetworkingClient(HttpOptions options) {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    final ProxyOptions proxyOptions = options.getProxyOptions();
    if (proxyOptions != null) {
        // Set proxy
        clientBuilder.proxy(proxyOptions.getProxy());
        // Set authentication, if present
        final String proxyAuth = proxyOptions.getBasicAuthentication();
        if (proxyAuth != null) {
            clientBuilder.proxyAuthenticator(new Authenticator() {

                private static final String PROXY_AUTHORIZATION_HEADER = "Proxy-Authorization";

                @Override
                public okhttp3.Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header(PROXY_AUTHORIZATION_HEADER) != null) {
                        return null;
                    }
                    return response.request().newBuilder().header(PROXY_AUTHORIZATION_HEADER, proxyAuth).build();
                }
            });
        }
    }
    configureLogging(options.getLoggingOptions());
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost(options.getMaxRequestsPerHost());
    dispatcher.setMaxRequests(options.getMaxRequests());
    return clientBuilder.addInterceptor(logging).addInterceptor(telemetry).addInterceptor(new RateLimitInterceptor(options.getManagementAPIMaxRetries())).connectTimeout(options.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(options.getReadTimeout(), TimeUnit.SECONDS).dispatcher(dispatcher).build();
}
Also used : ProxyOptions(com.auth0.client.ProxyOptions) IOException(java.io.IOException) RateLimitInterceptor(com.auth0.net.RateLimitInterceptor)

Example 2 with Telemetry

use of com.auth0.net.Telemetry in project auth0-java by auth0.

the class ManagementAPITest method shouldDisableTelemetryInterceptor.

@Test
public void shouldDisableTelemetryInterceptor() {
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
    assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
    api.doNotSendTelemetry();
    for (Interceptor i : api.getClient().interceptors()) {
        if (i instanceof TelemetryInterceptor) {
            TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
            assertThat(telemetry.isEnabled(), is(false));
        }
    }
}
Also used : TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) RateLimitInterceptor(com.auth0.net.RateLimitInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.Test)

Example 3 with Telemetry

use of com.auth0.net.Telemetry in project auth0-java by auth0.

the class ManagementAPITest method shouldUseCustomTelemetry.

@Test
public void shouldUseCustomTelemetry() {
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
    assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
    Telemetry currentTelemetry = null;
    for (Interceptor i : api.getClient().interceptors()) {
        if (i instanceof TelemetryInterceptor) {
            TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
            currentTelemetry = interceptor.getTelemetry();
        }
    }
    assertThat(currentTelemetry, is(notNullValue()));
    Telemetry newTelemetry = Mockito.mock(Telemetry.class);
    api.setTelemetry(newTelemetry);
    Telemetry updatedTelemetry = null;
    for (Interceptor i : api.getClient().interceptors()) {
        if (i instanceof TelemetryInterceptor) {
            TelemetryInterceptor interceptor = (TelemetryInterceptor) i;
            updatedTelemetry = interceptor.getTelemetry();
        }
    }
    assertThat(updatedTelemetry, is(newTelemetry));
}
Also used : TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) Telemetry(com.auth0.net.Telemetry) TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) RateLimitInterceptor(com.auth0.net.RateLimitInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.Test)

Example 4 with Telemetry

use of com.auth0.net.Telemetry in project auth0-java by auth0.

the class AuthAPI method buildNetworkingClient.

/**
 * Given a set of options, it creates a new instance of the {@link OkHttpClient}
 * configuring them according to their availability.
 *
 * @param options the options to set to the client.
 * @return a new networking client instance configured as requested.
 */
private OkHttpClient buildNetworkingClient(HttpOptions options) {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    final ProxyOptions proxyOptions = options.getProxyOptions();
    if (proxyOptions != null) {
        // Set proxy
        clientBuilder.proxy(proxyOptions.getProxy());
        // Set authentication, if present
        final String proxyAuth = proxyOptions.getBasicAuthentication();
        if (proxyAuth != null) {
            clientBuilder.proxyAuthenticator(new Authenticator() {

                private static final String PROXY_AUTHORIZATION_HEADER = "Proxy-Authorization";

                @Override
                public okhttp3.Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header(PROXY_AUTHORIZATION_HEADER) != null) {
                        return null;
                    }
                    return response.request().newBuilder().header(PROXY_AUTHORIZATION_HEADER, proxyAuth).build();
                }
            });
        }
    }
    configureLogging(options.getLoggingOptions());
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(options.getMaxRequests());
    dispatcher.setMaxRequestsPerHost(options.getMaxRequestsPerHost());
    return clientBuilder.addInterceptor(logging).addInterceptor(telemetry).connectTimeout(options.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(options.getReadTimeout(), TimeUnit.SECONDS).dispatcher(dispatcher).build();
}
Also used : PasswordlessSmsResponse(com.auth0.json.auth.PasswordlessSmsResponse) PasswordlessEmailResponse(com.auth0.json.auth.PasswordlessEmailResponse) ProxyOptions(com.auth0.client.ProxyOptions) Request(com.auth0.net.Request) IOException(java.io.IOException)

Example 5 with Telemetry

use of com.auth0.net.Telemetry in project auth0-java by auth0.

the class ManagementAPITest method shouldAddAndEnableTelemetryInterceptor.

@Test
public void shouldAddAndEnableTelemetryInterceptor() {
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
    assertThat(api.getClient().interceptors(), hasItem(isA(TelemetryInterceptor.class)));
    for (Interceptor i : api.getClient().interceptors()) {
        if (i instanceof TelemetryInterceptor) {
            TelemetryInterceptor telemetry = (TelemetryInterceptor) i;
            assertThat(telemetry.isEnabled(), is(true));
        }
    }
}
Also used : TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) TelemetryInterceptor(com.auth0.net.TelemetryInterceptor) RateLimitInterceptor(com.auth0.net.RateLimitInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.Test)

Aggregations

RateLimitInterceptor (com.auth0.net.RateLimitInterceptor)4 TelemetryInterceptor (com.auth0.net.TelemetryInterceptor)3 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)3 Test (org.junit.Test)3 ProxyOptions (com.auth0.client.ProxyOptions)2 Telemetry (com.auth0.net.Telemetry)2 IOException (java.io.IOException)2 PasswordlessEmailResponse (com.auth0.json.auth.PasswordlessEmailResponse)1 PasswordlessSmsResponse (com.auth0.json.auth.PasswordlessSmsResponse)1 Request (com.auth0.net.Request)1 Test (org.junit.jupiter.api.Test)1