import type http from "@ohos:net.http";
import type { Chain } from '../core/Chain';
import type { Interceptor } from './Interceptor';
/**
 * @ProjectName : HarmonyUtilSdk
 * @FileName : RetryInterceptor
 * @Author : yic
 * @Time : 2024/6/21 11:22
 * @Description : 文件描述
 */
export class RetryInterceptor implements Interceptor {
    private MAX: number;
    constructor(max: number = 3) {
        this.MAX = max;
    }
    async intercept(chain: Chain): Promise<http.HttpResponse> {
        const interceptorIndex = chain.requestBody.interceptorIndex;
        let response: http.HttpResponse;
        for (let tryCount = 0; tryCount < this.MAX; tryCount++) {
            chain.requestBody.interceptorIndex = interceptorIndex;
            response = await chain.process(chain);
            if (response.responseCode >= 200 && response.responseCode <= 299) {
                return response;
            }
        }
        return response;
    }
}
