package com.live.mylivewallpaper.request;

import okhttp3.Interceptor;
import okhttp3.Response;

import java.io.IOException;

public class ProgressInterceptor implements Interceptor {

    private final ProgressResponseBody.ProgressListener progressListener;

    public ProgressInterceptor(ProgressResponseBody.ProgressListener progressListener) {
        this.progressListener = progressListener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        String fileLength = originalResponse.header("File-Length");
        if (fileLength == null) {
            fileLength = "0";
        }
        return originalResponse.newBuilder()
                .body(new ProgressResponseBody(originalResponse.body(), fileLength, progressListener))
                .build();
    }
}

