83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\Adapter\API;
|
|
|
|
use \App\Entities\HPILOEntity;
|
|
|
|
// 참고:https://github.com/SyntaxPhoenix/iloclient
|
|
class Adapter
|
|
{
|
|
private $_entity = null;
|
|
protected $_client = null;
|
|
protected $_debug = false;
|
|
public function __construct($entity, $debug = false)
|
|
{
|
|
$this->_entity = $entity;
|
|
$this->_debug = $debug;
|
|
}
|
|
protected function getEntity(): HPILOEntity
|
|
{
|
|
return $this->_entity;
|
|
}
|
|
protected function getServerInfo($scheme = "https://", $delimeter = ":"): string
|
|
{
|
|
return $scheme . $this->getEntity()->getIP() . $delimeter . $this->getEntity()->getPort();
|
|
}
|
|
protected function getAccountInfo($type = 'basic'): array
|
|
{
|
|
//type: basic , digest
|
|
return array($this->getEntity()->getID(), $this->getEntity()->getPassword(), $type);
|
|
}
|
|
final public function get(string $url): object
|
|
{
|
|
return $this->requestURL($url, 'GET');
|
|
}
|
|
final public function post(string $url, array $datas): object
|
|
{
|
|
return $this->requestURL($url, 'POST', $datas);
|
|
}
|
|
|
|
protected function getClient()
|
|
{
|
|
if (is_null($this->_client)) {
|
|
// 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl#
|
|
// ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ]
|
|
$options = [
|
|
'baseURI' => $this->getServerInfo(),
|
|
'auth' => $this->getAccountInfo(),
|
|
'verify' => getenv('hpilo.verify') == 'true' ? true : false,
|
|
'cookie' => HPILOS['PATH'] . getenv('hpilo.cookie.file'),
|
|
];
|
|
if ($this->_debug) {
|
|
$options['debug'] = HPILOS['PATH'] . getenv('hpilo.debug.file'); //or true
|
|
}
|
|
$this->_client = \Config\Services::curlrequest($options);
|
|
}
|
|
return $this->_client;
|
|
}
|
|
protected function requestURL(string $url, string $method, array $datas = []): object
|
|
{
|
|
// dd($this->getClient());
|
|
$options = array();
|
|
switch ($method) {
|
|
case 'POST':
|
|
$response = $this->getClient()->setBody($datas)->request($method, $url, $options);
|
|
break;
|
|
case 'HEAD':
|
|
break;
|
|
default:
|
|
$response = $this->getClient()->request($method, $url, $options);
|
|
break;
|
|
}
|
|
dd($response);
|
|
if ($response->getStatusCode() != 200) {
|
|
throw new \Exception(sprintf(
|
|
"오류가 발생하였습니다.\n%s\n%s",
|
|
$response->getHeaderLine('content-type'),
|
|
$response->getBody()
|
|
));
|
|
}
|
|
return json_decode($response->getBody());
|
|
}
|
|
}
|