<?php


namespace App\Lib;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use AliYunLog;
use Constants;


/**
 * api接口请求类
 * Class HttpService
 * @package App\Services
 */
class Http
{
    protected $client;
    public function __construct()
    {
        $this->client = new Client;
    }


    /**
     * @param string $url
     * @param array $data
     * @param array $option
     * @param array $header
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function get(string $url, array $data = [] ,$option = [], array $header = [])
    {
        if ($data) {
            $queryStr = http_build_query($data);
            $url .= (false !== strpos($url, '?') ? '&' : '?') . "{$queryStr}";
        }
        !empty($header) && $option['headers'] = $header;
        try {
            (isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;

            $response = $this->client->request('GET', $url ,$option);
            $this->debugHttp($option, $response);
        } catch (GuzzleException $e) {
            $exception = $e->getMessage();
            $log = array(
                'url' => $url,
                'parameter'=> $data,
                'exceptionMessage' => $exception
            );

            SentryLog::addLog(json_encode($log));

            AliYunLog::recordLog('error', Constants::CURL_REQUEST_ERROR, Constants::message(Constants::CURL_REQUEST_ERROR). json_encode($log));

            return response()->json($exception, 200);
        }
        return $response->getBody();
    }

    /**
     * @param string $url
     * @param array $data
     * @param array $option
     * @param array $header
     * @param string $responseType
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function post(string $url, array $data = [], array $option = [], array $header =[], $responseType = 'formType')
    {
        try {
            !empty($header) && $option['headers'] = $header;
            (isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;
            if ($responseType == 'json')
            {
                $option['body'] = json_encode($data);
                $response = $this->client->request('POST', $url, $option);
            }
            else
            {
                $option['form_params'] = $data;
                $response = $this->client->request('POST', $url, $option);

            }
            $this->debugHttp($option, $response);

        }catch (GuzzleException $e){
            $exception = $e->getMessage();
            $log = array(
                'url' => $url,
                'parameter'=> $data,
                'exceptionMessage' => $exception
            );
            SentryLog::addLog(json_encode($log));
            return response()->json($exception, 200);
        }


        return $response->getBody();
    }


    /**
     * @param string $url
     * @param array $data
     * @param array $option
     * @param array $header
     * @param string $responseType
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function put(string $url, array $data = [], array $option = [], array $header =[], $responseType = 'formType')
    {
        try {
            !empty($header) && $option['headers'] = $header;
            (isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;
            if ($responseType == 'json')
            {
                $option['body'] = json_encode($data);
                $response = $this->client->request('PUT', $url, $option);
            }
            else
            {
                $option['form_params'] = $data;
                $response = $this->client->request('PUT', $url, $option);

            }
            $this->debugHttp($option, $response);

        }catch (GuzzleException $e){
            $exception = $e->getMessage();
            $log = array(
                'url' => $url,
                'parameter'=> $data,
                'exceptionMessage' => $exception
            );
            SentryLog::addLog(json_encode($log));
            return response()->json($exception, 200);
        }
        return $response->getBody();
    }


    /**
     * http 请求调试
     * @param $option
     * @param $response
     */
    private function debugHttp($option, $response)
    {
        if (isset($_GET['debug_http']) && $_GET['debug_http'] == 911) {

            echo "=====request data======\r\n";
            print_r($option);
            echo '=====request data end====='."\r\n";

            echo '=====respond data====='."\r\n";
            $body = $response->getBody();
            echo  $body . "\r\n";
            echo '=====respond data end====='."\r\n";
        }
    }
}