78 lines
2.0 KiB
PHP
Executable File
78 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\Prology\Api;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
class Api
|
|
{
|
|
|
|
protected $token;
|
|
protected $logHelper;
|
|
protected $objectManager;
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function init() {
|
|
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$this->logHelper = $this->objectManager->create('\IpSupply\Prology\Helper\LogHelper');
|
|
$this->logHelper->setDefaultName();
|
|
}
|
|
|
|
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, $query)
|
|
{
|
|
$client = new Client(['http_errors' => false]);
|
|
$headers = $this->getHeaders();
|
|
$response = $client->get(
|
|
$url,
|
|
[
|
|
'headers' => $headers,
|
|
'query' => $query
|
|
]
|
|
);
|
|
return $response;
|
|
}
|
|
|
|
public function post($url, $data)
|
|
{
|
|
$this->init();
|
|
$headers = $this->getHeaders();
|
|
$client = new Client(['http_errors' => false]);
|
|
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, json_encode($data));
|
|
$response = $client->sendAsync($request)->wait();
|
|
if ($response->getStatusCode() != 200) {
|
|
$this->logHelper->write("------------begin---------");
|
|
$this->logHelper->write("StatusCode :" . $response->getStatusCode() );
|
|
$this->logHelper->write(time());
|
|
$this->logHelper->write($url);
|
|
$this->logHelper->write(json_encode($headers));
|
|
$this->logHelper->write(json_encode($data));
|
|
$this->logHelper->write("------------end---------");
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
}
|