82 lines
1.6 KiB
PHP
82 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace wickedsoft\NetBox\HttpClient;
|
||
|
|
|
||
|
|
class HttpClient implements HttpClientInterface
|
||
|
|
{
|
||
|
|
/** @var \GuzzleHttp\Client */
|
||
|
|
protected $client;
|
||
|
|
|
||
|
|
/** @var array */
|
||
|
|
protected $options = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return \GuzzleHttp\Client
|
||
|
|
*/
|
||
|
|
public function getClient()
|
||
|
|
{
|
||
|
|
if (!isset($this->client)) {
|
||
|
|
$this->client = new \GuzzleHttp\Client(config('netbox.client_options', []));
|
||
|
|
|
||
|
|
return $this->client;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->client;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $client
|
||
|
|
* @return \GuzzleHttp\Client
|
||
|
|
*/
|
||
|
|
public function setClient($client)
|
||
|
|
{
|
||
|
|
$this->client = $client;
|
||
|
|
|
||
|
|
return $this->client;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $body
|
||
|
|
* @return mixed
|
||
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
||
|
|
*/
|
||
|
|
public function post($body = [])
|
||
|
|
{
|
||
|
|
return $this->request($body, 'POST');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $body
|
||
|
|
* @param $method
|
||
|
|
* @return mixed
|
||
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
||
|
|
*/
|
||
|
|
public function request($body, $method)
|
||
|
|
{
|
||
|
|
$response = $this->getClient()->request(
|
||
|
|
$method,
|
||
|
|
$this->getOptions()['base_url'],
|
||
|
|
[
|
||
|
|
'form_params' => $body
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return json_decode((string)$response->getBody(), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function getOptions()
|
||
|
|
{
|
||
|
|
return $this->options;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $options
|
||
|
|
*/
|
||
|
|
public function setOptions(array $options)
|
||
|
|
{
|
||
|
|
$this->options = array_merge($this->options, $options);
|
||
|
|
}
|
||
|
|
}
|