Instantly share code, notes, and snippets.

Embed
What would you like to do?
A logger interceptor for OkHttp 2.2+ and Retrofit 2.0.0-beta1
package net.sarmady.contactcarswithtabs.utils;
import com.squareup.okhttp.Request;
import java.io.IOException;
import okio.Buffer;
/**
* Created by mSobhy on 8/30/15.<br/>
*/
public class BufferReader {
public static String bodyToString(Request request) {
try {
Request copy = request.newBuilder().build();
Buffer buffer = new Buffer();
if (copy.body() != null) {
copy.body().writeTo(buffer);
return buffer.readUtf8();
} else {
return null;
}
} catch (IOException e) {
return "did not work";
}
}
}
package net.sarmady.contactcarswithtabs.rest.interceptors;
import android.util.Log;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import net.sarmady.contactcarswithtabs.Constants;
import java.io.IOException;
import okio.Buffer;
public class LoggingInterceptor implements Interceptor {
private static final String F_BREAK = " %n";
private static final String F_URL = " %s";
private static final String F_TIME = " in %.1fms";
private static final String F_HEADERS = "%s";
private static final String F_RESPONSE = F_BREAK + "Response: %d";
private static final String F_BODY = "body: %s";
private static final String F_BREAKER = F_BREAK + "-------------------------------------------" + F_BREAK;
private static final String F_REQUEST_WITHOUT_BODY = F_URL + F_TIME + F_BREAK + F_HEADERS;
private static final String F_RESPONSE_WITHOUT_BODY = F_RESPONSE + F_BREAK + F_HEADERS + F_BREAKER;
private static final String F_REQUEST_WITH_BODY = F_URL + F_TIME + F_BREAK + F_HEADERS + F_BODY + F_BREAK;
private static final String F_RESPONSE_WITH_BODY = F_RESPONSE + F_BREAK + F_HEADERS + F_BODY + F_BREAK + F_BREAKER;
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Response response = chain.proceed(request);
long t2 = System.nanoTime();
MediaType contentType = null;
String bodyString = null;
if (response.body() != null) {
contentType = response.body().contentType();
bodyString = response.body().string();
}
double time = (t2 - t1) / 1e6d;
switch (request.method()) {
case "GET":
Log.d(Constants.Logger.logTag(this), String.format("GET " + F_REQUEST_WITHOUT_BODY + F_RESPONSE_WITH_BODY, request.url(), time, request.headers(), response.code(), response.headers(), stringifyResponseBody(bodyString)));
break;
case "POST":
Log.d(Constants.Logger.logTag(this), String.format("POST " + F_REQUEST_WITH_BODY + F_RESPONSE_WITH_BODY, request.url(), time, request.headers(), stringifyRequestBody(request), response.code(), response.headers(), stringifyResponseBody(bodyString)));
break;
case "PUT":
Log.d(Constants.Logger.logTag(this), String.format("PUT " + F_REQUEST_WITH_BODY + F_RESPONSE_WITH_BODY, request.url(), time, request.headers(), request.body().toString(), response.code(), response.headers(), stringifyResponseBody(bodyString)));
break;
case "DELETE":
Log.d(Constants.Logger.logTag(this), String.format("DELETE " + F_REQUEST_WITHOUT_BODY + F_RESPONSE_WITHOUT_BODY, request.url(), time, request.headers(), response.code(), response.headers()));
break;
}
if (response.body() != null) {
ResponseBody body = ResponseBody.create(contentType, bodyString);
return response.newBuilder().body(body).build();
} else {
return response;
}
}
private static String stringifyRequestBody(Request request) {
try {
Request copy = request.newBuilder().build();
Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
} catch (IOException e) {
return "did not work";
}
}
public String stringifyResponseBody(String responseBody) {
return responseBody;
}
}
/**
* Created by mSobhy on 7/1/15.
*/
public class RestClient {
private UserService userService;
public RestClient() {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new SecurityOfficerInterceptor());
client.interceptors().add(new LoggingInterceptor());
Retrofit restAdapter = new Retrofit.Builder()
.client(client)
.baseUrl(URLs.getServerUrl())
.addConverterFactory(GsonConverterFactory.create(gson()))
.build();
userService = restAdapter.create(UserService.class);
}
}
package net.sarmady.contactcarswithtabs.rest.interceptors;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import net.sarmady.contactcarswithtabs.rest.SecurityOfficer;
import net.sarmady.contactcarswithtabs.utils.BufferReader;
import java.io.IOException;
/**
* Created by mSobhy on 8/30/15.<br/>
*/
public class SecurityOfficerInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String requestBody = null;
if (request.body() != null) {
requestBody = BufferReader.bodyToString(request);
}
String authorizationHeader = SecurityOfficer.getAuthorizationHeader(request.method(), request.urlString(), requestBody);
Request wrapperRequest = request.newBuilder().header("Authorization", authorizationHeader).build();
return chain.proceed(wrapperRequest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment