mc-cms-namelessmc/core/classes/Core/HttpClient.php

264 lines
8.1 KiB
PHP
Raw Permalink Normal View History

<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Profiling\Debugbar\Profiler;
use GuzzleHttp\Profiling\Middleware as ProfilingMiddleware;
use Psr\Http\Message\ResponseInterface;
/**
* Provides simple methods to make GET & POST HTTP requests.
* Wrapper around GuzzleHttp\Client.
*
* @see GuzzleHttp\Client
* @package NamelessMC\Core
* @author Aberdeener
* @version 2.0.0-pr13
* @license MIT
*/
class HttpClient
{
2022-03-02 08:29:02 -08:00
private ?ResponseInterface $_response;
private string $_error;
private function __construct(?ResponseInterface $response, string $error)
{
$this->_response = $response;
$this->_error = $error;
}
/**
* Make a GET request to a URL.
* Failures will automatically be logged along with the error.
*
* @param string $url URL to send request to.
* @param array $options Options to set with the GuzzleClient.
* @return HttpClient New HttpClient instance.
*/
public static function get(string $url, array $options = []): HttpClient
{
2022-03-29 16:07:27 -07:00
$guzzleClient = self::createClient($options);
$error = '';
try {
$response = $guzzleClient->get($url);
} catch (GuzzleException $exception) {
$error = $exception->getMessage();
2022-05-04 21:12:06 -07:00
Log::getInstance()->log(Log::Action('misc/curl_error'), $exception->getMessage());
}
return new HttpClient(
$response ?? null,
$error
);
}
/**
* Make a POST request to a URL.
* Failures will automatically be logged along with the error.
*
* @param string $url URL to send request to.
* @param string|array $data JSON request body to attach to request, or array of key value pairs if form-urlencoded.
* @param array $options Options to set with the GuzzleClient.
* @return HttpClient New HttpClient instance.
*/
public static function post(string $url, $data, array $options = []): HttpClient
{
2022-03-29 16:07:27 -07:00
$guzzleClient = self::createClient($options);
$error = '';
try {
$response = $guzzleClient->post($url, [
// if the data is an array, we assume they want to send it as form-urlencoded, otherwise it's json
is_array($data) ? 'form_params' : 'body' => $data,
]);
} catch (GuzzleException $exception) {
$error = $exception->getMessage();
Log::getInstance()->log(Log::Action('misc/curl_error'), $exception->getMessage());
}
return new HttpClient(
$response ?? null,
$error
);
}
/**
* Make a PUT request to a URL.
* Failures will automatically be logged along with the error.
*
* @param string $url URL to send request to.
* @param string|array $data JSON request body to attach to request, or array of key value pairs if form-urlencoded.
* @param array $options Options to set with the GuzzleClient.
* @return HttpClient New HttpClient instance.
*/
public static function put(string $url, $data, array $options = []): HttpClient
{
$guzzleClient = self::createClient($options);
$error = '';
try {
$response = $guzzleClient->put($url, [
// if the data is an array, we assume they want to send it as form-urlencoded, otherwise it's json
is_array($data) ? 'form_params' : 'body' => $data,
]);
} catch (GuzzleException $exception) {
$error = $exception->getMessage();
Log::getInstance()->log(Log::Action('misc/curl_error'), $exception->getMessage());
}
return new HttpClient(
$response ?? null,
$error
);
}
/**
* Make a PATCH request to a URL.
* Failures will automatically be logged along with the error.
*
* @param string $url URL to send request to.
* @param string|array $data JSON request body to attach to request, or array of key value pairs if form-urlencoded.
* @param array $options Options to set with the GuzzleClient.
* @return HttpClient New HttpClient instance.
*/
public static function patch(string $url, $data, array $options = []): HttpClient
{
$guzzleClient = self::createClient($options);
$error = '';
try {
$response = $guzzleClient->patch($url, [
// if the data is an array, we assume they want to send it as form-urlencoded, otherwise it's json
is_array($data) ? 'form_params' : 'body' => $data,
]);
} catch (GuzzleException $exception) {
$error = $exception->getMessage();
Log::getInstance()->log(Log::Action('misc/curl_error'), $exception->getMessage());
}
return new HttpClient(
$response ?? null,
$error
);
}
/**
* Make a DELETE request to a URL.
* Failures will automatically be logged along with the error.
*
* @param string $url URL to send request to.
* @param string|array $data JSON request body to attach to request, or array of key value pairs if form-urlencoded.
* @param array $options Options to set with the GuzzleClient.
* @return HttpClient New HttpClient instance.
*/
public static function delete(string $url, $data, array $options = []): HttpClient
{
$guzzleClient = self::createClient($options);
$error = '';
try {
$response = $guzzleClient->delete($url, [
// if the data is an array, we assume they want to send it as form-urlencoded, otherwise it's json
is_array($data) ? 'form_params' : 'body' => $data,
]);
} catch (GuzzleException $exception) {
$error = $exception->getMessage();
Log::getInstance()->log(Log::Action('misc/curl_error'), $exception->getMessage());
}
return new HttpClient(
$response ?? null,
$error
);
}
/**
* Make a new Guzzle Client instance and attach it to the debug bar to display requests.
*
* @param array $options Options to provide Guzzle instance.
* @return Client New Guzzle instance.
*/
public static function createClient(array $options = []): Client
{
$debugBar = DebugBarHelper::getInstance()->getDebugBar();
$stack = HandlerStack::create();
if ($debugBar !== null) {
$stack->unshift(new ProfilingMiddleware(
new Profiler($debugBar->getCollector('time'))
));
}
2022-03-29 16:07:27 -07:00
return new Client(array_merge([
'timeout' => 5.0,
'handler' => $stack,
], $options));
}
2021-12-07 22:14:12 -08:00
/**
* Get the response body.
2021-12-07 22:14:12 -08:00
*
* @return string The response body
*/
public function contents(): string
{
2022-03-02 08:29:02 -08:00
return $this->_response->getBody()->getContents();
}
/**
* Get the response body as a decoded JSON object.
*
* @param bool $assoc Whether to decode the JSON as a PHP array if true or PHP object.
2022-01-30 19:51:57 -08:00
* @return mixed The response body
*/
public function json(bool $assoc = false)
{
2022-03-10 09:38:10 -08:00
return json_decode($this->contents(), $assoc);
2021-12-07 22:14:12 -08:00
}
/**
* Get the response HTTP status code.
2021-12-07 22:14:12 -08:00
*
* @return int The response code
*/
public function getStatus(): int
{
return $this->_response->getStatusCode();
2021-12-07 22:14:12 -08:00
}
/**
* Check if the response has an error.
2021-12-07 22:14:12 -08:00
*
* @return bool Whether the response has an error or not
*/
public function hasError(): bool
{
return $this->getError() !== '';
2021-12-07 22:14:12 -08:00
}
/**
* Get the error message.
2021-12-07 22:14:12 -08:00
*
* @return string The error message
*/
public function getError(): string
{
2022-03-02 08:29:02 -08:00
if ($this->_error !== '') {
2022-04-13 19:13:35 -07:00
return Output::getClean($this->_error);
2022-03-02 08:29:02 -08:00
}
if ($this->_response === null) {
return '$this->_response is null';
}
return '';
2021-12-07 22:14:12 -08:00
}
}