thegrizzlylabs / sardine-android

A WebDAV library for Android
Apache License 2.0
355 stars 70 forks source link

The access of Sardine inner okHttpClient #47

Closed BrightVan closed 4 years ago

BrightVan commented 4 years ago

Sardine contains a okhttpClient, but it is private. when i use a sington to reuse the sardine instance, if "isPreemptive "set TRUE, it will cause error at switch webdav server. i want to remove the interceptor, but i can not access the okhttpClient.

BrightVan commented 4 years ago
@Override
    public void setCredentials(String username, String password, boolean isPreemptive) {
        OkHttpClient.Builder builder = client.newBuilder();
         List<Interceptor> interceptors = builder.interceptors();
            for (Interceptor interceptor : interceptors) {
                if (interceptor instanceof AuthenticationInterceptor) {
                    interceptors.remove(interceptor);
                }
            }
        if (isPreemptive) {
            builder.addInterceptor(new AuthenticationInterceptor(username, password));
        } else {
            builder.authenticator(new BasicAuthenticator(username, password));
        }
        this.client = builder.build();
    }

    public void clearCredentials() {
        OkHttpClient.Builder builder = client.newBuilder();
        builder.authenticator(Authenticator.NONE);
        this.client = builder.build();
    }
guillaume-tgl commented 4 years ago

There's a constructor of OkHttpSardine which takes a OkHttpClient as parameter. Isn't this enough to manage the credentials yourself? See https://github.com/thegrizzlylabs/sardine-android/blob/master/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java#L76

BrightVan commented 4 years ago

There's a constructor of OkHttpSardine which takes a OkHttpClient as parameter. Isn't this enough to manage the credentials yourself? See https://github.com/thegrizzlylabs/sardine-android/blob/master/src/main/java/com/thegrizzlylabs/sardineandroid/impl/OkHttpSardine.java#L76 the question is client.newBuilder().build() will create a new Object,the constructor's parameter OkHttpClient is just a init object, they are not same.anyway, i clone and custom the code as above. of cource, there is no question if create a new OkHttpSardine object every time.