netbox-php/src/HttpClient/HttpClient.php

82 lines
1.6 KiB
PHP
Raw Normal View History

2021-03-10 18:34:24 -04:00
<?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);
}
}