magento2-docker/app/code/IpSupply/QuoteIn/Api/Api.php

68 lines
1.4 KiB
PHP
Executable File

<?php
namespace IpSupply\QuoteIn\Api;
use GuzzleHttp\Client;
class Api
{
protected $token;
public function __construct()
{
}
public function setToken($token)
{
$this->token = $token;
}
public function getToken()
{
return $this->token;
}
public function getHeaders()
{
$headers = array();
if (!empty($this->token)) {
$headers["Authorization"] = "Bearer ".$this->token;
$headers["Content-Type"] = "application/json";
}
return $headers;
}
public function get($url) {
$client = new Client();
$headers = $this->getHeaders();
$response = $client->get(
$url,[
'headers' => $headers
]
);
return $response;
}
public function post($url, $data) {
$client = new Client();
$headers = $this->getHeaders();
$response = $client->post(
$url,[
'form_params' => $data,
'headers' => $headers
]
);
return $response;
}
public function postV2($url, $data) {
$client = new Client();
$headers = $this->getHeaders();
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, json_encode($data));
$response = $client->sendAsync($request)->wait();
return $response;
}
}