提交 e9b76866 编写于 作者: J jessyan

Improve FormatPrinter and RequestInterceptor

上级 6042eba9
/**
* Copyright 2017 JessYan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2017 JessYan
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.jessyan.mvparms.demo.app;
import android.app.Application;
......@@ -54,7 +54,7 @@ public final class GlobalConfiguration implements ConfigModule {
@Override
public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
if (!BuildConfig.LOG_DEBUG){ //Release 时,让框架不再打印 Http 请求和响应的信息
if (!BuildConfig.LOG_DEBUG) { //Release 时,让框架不再打印 Http 请求和响应的信息
builder.printHttpLogLevel(RequestInterceptor.Level.NONE);
}
......
......@@ -23,6 +23,8 @@ import android.text.TextUtils;
import com.bumptech.glide.Glide;
import com.jess.arms.http.BaseUrl;
import com.jess.arms.http.GlobalHttpHandler;
import com.jess.arms.http.log.DefaultFormatPrinter;
import com.jess.arms.http.log.FormatPrinter;
import com.jess.arms.http.log.RequestInterceptor;
import com.jess.arms.http.imageloader.BaseImageLoaderStrategy;
import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy;
......@@ -67,6 +69,7 @@ public class GlobalConfigModule {
private ClientModule.RxCacheConfiguration mRxCacheConfiguration;
private AppModule.GsonConfiguration mGsonConfiguration;
private RequestInterceptor.Level mPrintHttpLogLevel;
private FormatPrinter mFormatPrinter;
private Cache.Factory mCacheFactory;
private GlobalConfigModule(Builder builder) {
......@@ -82,6 +85,7 @@ public class GlobalConfigModule {
this.mRxCacheConfiguration = builder.rxCacheConfiguration;
this.mGsonConfiguration = builder.gsonConfiguration;
this.mPrintHttpLogLevel = builder.printHttpLogLevel;
this.mFormatPrinter = builder.formatPrinter;
this.mCacheFactory = builder.cacheFactory;
}
......@@ -198,6 +202,12 @@ public class GlobalConfigModule {
return mPrintHttpLogLevel;
}
@Singleton
@Provides
FormatPrinter provideFormatPrinter(){
return mFormatPrinter == null ? new DefaultFormatPrinter() : mFormatPrinter;
}
@Singleton
@Provides
Cache.Factory provideCacheFactory(Application application) {
......@@ -226,6 +236,7 @@ public class GlobalConfigModule {
private ClientModule.RxCacheConfiguration rxCacheConfiguration;
private AppModule.GsonConfiguration gsonConfiguration;
private RequestInterceptor.Level printHttpLogLevel;
private FormatPrinter formatPrinter;
private Cache.Factory cacheFactory;
private Builder() {
......@@ -303,6 +314,13 @@ public class GlobalConfigModule {
return this;
}
public Builder formatPrinter(FormatPrinter formatPrinter){
if (formatPrinter == null)
throw new NullPointerException("FormatPrinter can not be null");
this.formatPrinter = formatPrinter;
return this;
}
public Builder cacheFactory(Cache.Factory cacheFactory) {
this.cacheFactory = cacheFactory;
return this;
......
......@@ -18,22 +18,28 @@ package com.jess.arms.http.log;
import android.text.TextUtils;
import com.jess.arms.di.module.GlobalConfigModule;
import com.jess.arms.utils.CharacterHandler;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.Request;
import timber.log.Timber;
/**
* ================================================
* 对 OkHttp 的请求和响应信息进行更规范和清晰的打印
* <p>
* 对 OkHttp 的请求和响应信息进行更规范和清晰的打印, 此类为框架默认实现, 以默认格式打印信息, 若觉得默认打印格式
* 并不能满足自己的需求, 可自行扩展自己理想的打印格式
*
* @see GlobalConfigModule.Builder#formatPrinter(FormatPrinter)
* Created by JessYan on 25/01/2018 14:51
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public class DefaultFormatPrinter {
public class DefaultFormatPrinter implements FormatPrinter {
private static final String TAG = "ArmsHttpLog";
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String DOUBLE_SEPARATOR = LINE_SEPARATOR + LINE_SEPARATOR;
......@@ -57,9 +63,6 @@ public class DefaultFormatPrinter {
private static final String CENTER_LINE = "├ ";
private static final String DEFAULT_LINE = "│ ";
private DefaultFormatPrinter() {
throw new UnsupportedOperationException("you can't instantiate me!");
}
private static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || N.equals(line) || T.equals(line) || TextUtils.isEmpty(line.trim());
......@@ -71,7 +74,8 @@ public class DefaultFormatPrinter {
* @param request
* @param bodyString
*/
static void printJsonRequest(Request request, String bodyString) {
@Override
public void printJsonRequest(Request request, String bodyString) {
final String requestBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString;
final String tag = getTag(true);
......@@ -87,7 +91,8 @@ public class DefaultFormatPrinter {
*
* @param request
*/
static void printFileRequest(Request request) {
@Override
public void printFileRequest(Request request) {
final String tag = getTag(true);
Timber.tag(tag).i(REQUEST_UP_LINE);
......@@ -100,17 +105,21 @@ public class DefaultFormatPrinter {
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况
*
* @param chainMs
* @param isSuccessful
* @param code
* @param headers
* @param bodyString
* @param segments
* @param message
* @param responseUrl
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param contentType 服务器返回数据的数据类型
* @param bodyString 服务器返回的数据(已解析)
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
static void printJsonResponse(long chainMs, boolean isSuccessful,
int code, String headers, String bodyString, List<String> segments, String message, final String responseUrl) {
@Override
public void printJsonResponse(long chainMs, boolean isSuccessful, int code, String headers, MediaType contentType,
String bodyString, List<String> segments, String message, final String responseUrl) {
bodyString = RequestInterceptor.isJson(contentType) ? CharacterHandler.jsonFormat(bodyString)
: RequestInterceptor.isXml(contentType) ? CharacterHandler.xmlFormat(bodyString) : bodyString;
final String responseBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString;
final String tag = getTag(false);
......@@ -126,16 +135,17 @@ public class DefaultFormatPrinter {
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 为 {@code null} 或不可解析的情况
*
* @param chainMs
* @param isSuccessful
* @param code
* @param headers
* @param segments
* @param message
* @param responseUrl
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
static void printFileResponse(long chainMs, boolean isSuccessful,
int code, String headers, List<String> segments, String message, final String responseUrl) {
@Override
public void printFileResponse(long chainMs, boolean isSuccessful, int code, String headers,
List<String> segments, String message, final String responseUrl) {
final String tag = getTag(false);
final String[] urlLine = {URL_TAG + responseUrl, N};
......
/*
* Copyright 2018 JessYan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jess.arms.http.log;
import com.jess.arms.di.module.GlobalConfigModule;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.Request;
/**
* ================================================
* 对 OkHttp 的请求和响应信息进行更规范和清晰的打印, 开发者可更根据自己的需求自行扩展打印格式
*
* @see DefaultFormatPrinter
* @see GlobalConfigModule.Builder#formatPrinter(FormatPrinter)
* Created by JessYan on 31/01/2018 17:36
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public interface FormatPrinter {
/**
* 打印网络请求信息, 当网络请求时 {{@link okhttp3.RequestBody}} 可以解析的情况
*
* @param request
* @param bodyString 发送给服务器的请求体中的数据(已解析)
*/
void printJsonRequest(Request request, String bodyString);
/**
* 打印网络请求信息, 当网络请求时 {{@link okhttp3.RequestBody}} 为 {@code null} 或不可解析的情况
*
* @param request
*/
void printFileRequest(Request request);
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况
*
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param contentType 服务器返回数据的数据类型
* @param bodyString 服务器返回的数据(已解析)
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
void printJsonResponse(long chainMs, boolean isSuccessful, int code, String headers, MediaType contentType,
String bodyString, List<String> segments, String message, String responseUrl);
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 为 {@code null} 或不可解析的情况
*
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
void printFileResponse(long chainMs, boolean isSuccessful, int code, String headers,
List<String> segments, String message, String responseUrl);
}
......@@ -56,6 +56,7 @@ import timber.log.Timber;
@Singleton
public class RequestInterceptor implements Interceptor {
private GlobalHttpHandler mHandler;
private final FormatPrinter mPrinter;
private final Level printLevel;
public enum Level {
......@@ -66,12 +67,13 @@ public class RequestInterceptor implements Interceptor {
}
@Inject
public RequestInterceptor(@Nullable GlobalHttpHandler handler, @Nullable Level level) {
public RequestInterceptor(@Nullable GlobalHttpHandler handler, @Nullable Level level, FormatPrinter printer) {
this.mHandler = handler;
this.mPrinter = printer;
if (level == null)
printLevel = Level.ALL;
this.printLevel = Level.ALL;
else
printLevel = level;
this.printLevel = level;
}
@Override
......@@ -83,9 +85,9 @@ public class RequestInterceptor implements Interceptor {
if (logRequest) {
//打印请求信息
if (request.body() != null && isParseable(request.body().contentType())) {
DefaultFormatPrinter.printJsonRequest(request, parseParams(request));
mPrinter.printJsonRequest(request, parseParams(request));
} else {
DefaultFormatPrinter.printFileRequest(request);
mPrinter.printFileRequest(request);
}
}
......@@ -118,13 +120,10 @@ public class RequestInterceptor implements Interceptor {
final String url = originalResponse.request().url().toString();
if (responseBody != null && isParseable(responseBody.contentType())) {
DefaultFormatPrinter.printJsonResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
isSuccessful, code, header,
isJson(responseBody.contentType()) ?
CharacterHandler.jsonFormat(bodyString) : isXml(responseBody.contentType()) ?
CharacterHandler.xmlFormat(bodyString) : bodyString, segmentList, message, url);
mPrinter.printJsonResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1), isSuccessful,
code, header, responseBody.contentType(), bodyString, segmentList, message, url);
} else {
DefaultFormatPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
mPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
isSuccessful, code, header, segmentList, message, url);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册