<?php

namespace App\Classes\Modules\Orders\Services;

use Psr\Http\Message\ResponseInterface;

class FetchesDataFromYDPortal
{

    /**
     * @param string $url
     * @param string $method
     * @param $body
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function clientRequest(string $url, string $method, $body){
        
        $client = new \GuzzleHttp\Client();

        $body['apicode'] = env('YD_API_CODE');

        $request = $client->requestAsync($method, $url, ['query' => $body]);

        return $request->wait();
    }

    /**
     * @param ResponseInterface $request
     * @return mixed
     */
    public function getResponseBody(ResponseInterface $request){
        $content = $request->getBody()->getContents();
        $content =  str_replace("\r",'', $content);
        $content =  str_replace("\n",'', $content);
        return json_decode($content);
    }
}