<?php


namespace Api\PhpUtils\Common;


use Api\PhpUtils\Http\Request;

class TimeOut
{

    private $timeout_95;
    private $timeout_99;
    private $retry_95;
    private $retry_99;


    /**
     * TimeOut constructor.
     * @param int $timeout_95 毫秒
     * @param int $timeout_99 毫秒
     * @param int $retry_95   重试次数
     * @param int $retry_99   重试次数
     */
    public function __construct($timeout_95 = 300, $timeout_99 = 800, $retry_95 = 1, $retry_99 = 2)
    {
        $this->timeout_95   = $timeout_95;
        $this->timeout_99   = $timeout_99;
        $this->retry_95     = $retry_95;
        $this->retry_99     = $retry_99;
    }


    /**
     * GET请求
     * @param $url
     * @param array $params
     * @param array $headers
     * @return array|bool|mixed
     */
    public function runGet($url, $params = [], $headers = []){

        $res = (new Request())->get($url, $params, $this->timeout_95, $headers, $this->retry_95);
        if($res["code"] != 999999){
            return $res['response'];
        }

        $res = (new Request())->get($url, $params, $this->timeout_99, $headers, $this->retry_99);
        if($res["code"] != 999999){
            return $res['response'];
        }

        return false;
    }


    public function runPost($url, $params = [], $content_type = null, $headers = []){

        $res = (new Request())->post($url, $params, $this->timeout_95, $content_type, $headers, $this->retry_95);
        if($res["code"] != 999999){
            return $res['response'];
        }

        $res = (new Request())->post($url, $params, $this->timeout_99, $content_type, $headers, $this->retry_99);
        if($res["code"] != 999999){
            return $res['response'];
        }

        return false;
    }
}