Created
November 23, 2017 13:44
-
-
Save staakk/aa512aa2d70ebed5abd659b1de1da20d to your computer and use it in GitHub Desktop.
Dagger2 module for Retrofit with authorization, and Java 8 DateTime mappers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import org.threeten.bp.LocalDateTime; | |
import javax.inject.Singleton; | |
import dagger.Module; | |
import dagger.Provides; | |
import okhttp3.OkHttpClient; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Converter; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.jackson.JacksonConverterFactory; | |
@Module | |
public class NetModule { | |
@Singleton | |
@Provides | |
Converter.Factory provideConverterFactory() { | |
SimpleModule module = new SimpleModule(); | |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); | |
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); | |
ObjectMapper objectMapper = new ObjectMapper() | |
.registerModule(module); | |
return JacksonConverterFactory.create(objectMapper); | |
} | |
@Singleton | |
@Provides | |
HttpLoggingInterceptor provideHttpLoggingInterceptor() { | |
return new HttpLoggingInterceptor() | |
.setLevel(HttpLoggingInterceptor.Level.BODY); | |
} | |
@Singleton | |
@Provides | |
AuthenticationInterceptor provideAuthorizationInterceptor() { | |
return new AuthenticationInterceptor(); | |
} | |
@Singleton | |
@Provides | |
OkHttpClient provideHttpClient(HttpLoggingInterceptor logging, AuthenticationInterceptor auth) { | |
return new OkHttpClient.Builder() | |
.addInterceptor(logging) | |
.addInterceptor(auth) | |
.build(); | |
} | |
@Singleton | |
@Provides | |
Retrofit provideRetrofit(OkHttpClient okHttpClient, Converter.Factory converterFactory) { | |
return new Retrofit.Builder() | |
.baseUrl("URL") | |
.client(okHttpClient) | |
.addConverterFactory(converterFactory) | |
.addConverterFactory(new LocalDateTimeConverterFactory()) | |
.build(); | |
} | |
} | |
public class LocalDateTimeConverterFactory extends Converter.Factory { | |
@Override | |
public Converter<LocalDateTime, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) { | |
if (!LocalDateTime.class.equals(type)) return null; | |
return LocalDateTimeConverter.INSTANCE; | |
} | |
private static class LocalDateTimeConverter implements Converter<LocalDateTime, String> { | |
final static LocalDateTimeConverter INSTANCE = new LocalDateTimeConverter(); | |
@Override | |
public String convert(LocalDateTime value) throws IOException { | |
return value.format(DateTimeFormat.API_DATE_FORMAT); | |
} | |
} | |
} | |
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { | |
static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); | |
@Override | |
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | |
JsonToken token = p.getCurrentToken(); | |
if (token == JsonToken.VALUE_STRING) { | |
return LocalDateTime.from(dateTimeFormatter.parse(p.getValueAsString())); | |
} | |
return null; | |
} | |
} | |
public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> { | |
public LocalDateTimeSerializer() { | |
super(LocalDateTime.class); | |
} | |
@Override | |
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException { | |
gen.writeString(DateTimeFormat.API_DATE_FORMAT.format(value)); | |
} | |
} | |
@Singleton | |
public class AuthenticationInterceptor implements Interceptor { | |
private String authToken; | |
@Inject | |
public AuthenticationInterceptor() { | |
} | |
public void setAuthToken(String authToken) { | |
this.authToken = authToken; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
String noAuth = request.header("no-auth"); | |
Request.Builder builder = request.newBuilder(); | |
if (noAuth == null) { | |
builder.header("Authorization", authToken); | |
} else { | |
builder.removeHeader("no-auth"); | |
} | |
request = builder.build(); | |
return chain.proceed(request); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return obj instanceof AuthenticationInterceptor | |
&& authToken.contentEquals(((AuthenticationInterceptor) obj).authToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment