worked on http client's post, get, put and delete requests

This commit is contained in:
Tony Roy 2021-03-12 15:26:05 -04:00
commit 0184bb81dc

View file

@ -16,7 +16,7 @@ class HttpClient implements HttpClientInterface
public function getClient() public function getClient()
{ {
if (!isset($this->client)) { if (!isset($this->client)) {
$this->client = new \GuzzleHttp\Client(config('netbox.client_options', [])); $this->client = new \GuzzleHttp\Client(config('netbox.client_options'));
return $this->client; return $this->client;
} }
@ -40,9 +40,16 @@ class HttpClient implements HttpClientInterface
* @return mixed * @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public function get($path="", $body = []) public function get($path="", $query = [])
{ {
return $this->request($path, $body, 'GET'); $response = $this->getClient()->request(
'GET',
config('netbox.panels.default.key').$path,
[
'query' => $query
]
);
return json_decode((string)$response->getBody(), true);
} }
/** /**
@ -52,7 +59,14 @@ class HttpClient implements HttpClientInterface
*/ */
public function post($path="", $body = []) public function post($path="", $body = [])
{ {
return $this->request($path, $body, 'POST'); $response = $this->getClient()->request(
'POST',
config('netbox.panels.default.key').$path,
[
'form_params' => $body
]
);
return json_decode((string)$response->getBody(), true);
} }
/** /**
@ -62,7 +76,14 @@ class HttpClient implements HttpClientInterface
*/ */
public function put($path="", $body = []) public function put($path="", $body = [])
{ {
return $this->request($path, $body, 'PUT'); $response = $this->getClient()->request(
'PUT',
config('netbox.panels.default.key').$path,
[
'form_params' => $body
]
);
return json_decode((string)$response->getBody(), true);
} }
/** /**
@ -71,26 +92,14 @@ class HttpClient implements HttpClientInterface
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public function delete($path="", $body = []) public function delete($path="", $body = [])
{
return $this->request($path, $body, 'DELETE');
}
/**
* @param $body
* @param $method
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function request($path="", $body, $method)
{ {
$response = $this->getClient()->request( $response = $this->getClient()->request(
$method, 'DELETE',
$path, config('netbox.panels.default.key').$path,
[ [
'form_params' => $body 'form_params' => $body
] ]
); );
return json_decode((string)$response->getBody(), true); return json_decode((string)$response->getBody(), true);
} }