Create HttpClient.php
This commit is contained in:
parent
33c43101de
commit
f01edb01ed
1 changed files with 82 additions and 0 deletions
82
src/HttpClient/HttpClient.php
Normal file
82
src/HttpClient/HttpClient.php
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue