i'm just wondering what the best practices are for retrofit2 implementation, let me explain my experience. I followed a very good tutorial to get started and use retrofit2 to interact with WP rest-api, i needed to just have the client collect data in mobility and post them: a custom post type, and one picture. So for now i'm implementing retrofit like this: Assume i have a private static OkHttpClient.Builder called "cb".
public static <S> S implementor (Class<S> endPointsClass){
return implementor (endPointsClass, null, null);
}
public static <S> S implementor (Class<S> endPointsClass, final String mToken, boolean isUpload){
if(mToken != null && !isUpload){
cb.addInterceptor(new Interceptor){
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request orig = chain.request();
ReqeustBuilder rb = orig.newBuilder()
.header("Authentication", mToken),
.method(orig.method(), orig.body());
Request req = rb.build();
return chain.proceed(req);
}
});
OkHttpClient mClient = cb.build();
Retrofit retro = builder.client(mClient).build();
return retro.create(endPointsClass);
}
}
This way, no matter what's the endpoint i made the request to, i've always my auth header attached. The problem started when i needed another interceptor to modify my request's body, i'm not sending my image as multipart, but as byte[]. So i just tried to make an overloaded copy of the method you see above like this:
public static <S> S implementor (Class<S> endPointsClass, final String mToken, boolean isUpload, final byte[] img, final String fileName){
if(mToken != null && isUpload){
cb.addInterceptor(new Interceptor){
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request orig = chain.request();
ReqeustBuilder rb = orig.newBuilder()
.header("Authentication", mToken),
.header("Content-Type", "image/jpg"),
.header("Content-Disposition", "filename=" + fileName),
.post(img), //i don't remember how i defined the body, if like this or with the RequestBody class if'u're interested i'll post the exact code when i'm back home.
Request req = rb.build();
return chain.proceed(req);
}
});
OkHttpClient mClient = cb.build();
Retrofit retro = builder.client(mClient).build();
return retro.create(endPointsClass);
}
}
When i used this method to upload images i started experiencing two problems: first my upload body as long as the headers keeps persisting through multiple requests and interceptors started adding one to another, second from the retrofit logger seemed that ALL the methods were called in sequence. I then added the condition isUpload that seemed to work around my second problem, to deal with my first problem i just created a method to cycle through every interceptor/header and delete them before adding a new one. I'm a noob programmer, just started six months ago, but if i'm not misunderstanding everything of what i've learned so far, my problems are mostly related to the fact that the OkHttpClient.Builder is static, so everytime i add an interceptor this is stored in the original reference (i guess). How is designed a well designed retrofit client? Create two differents client builders, one for upload and one general, and passing them to my implementor's methods is a bad idea? Having a non static OkHttpClient.builder and implementing a new one everytime may be good? Sorry for bothering, but i'm working very hard to learn coding and i need an advice.
Aucun commentaire:
Enregistrer un commentaire