Skip to content

Commit

Permalink
Implement HTTP upload and remove methods
Browse files Browse the repository at this point in the history
This commit adds `upload` and `remove` methods to the HTTP class and implements the Uploadable interface. Additionally, minor changes are made for better typing and syntax consistency across different classes. A badge for deploy workflow is added in the README.
  • Loading branch information
Pavlusha311245 committed Apr 6, 2024
1 parent 4c34510 commit 2a032d5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<p align="center">
<a href="https://packagist.org/packages/pavlusha311245/unit-php-sdk"><img src="https://img.shields.io/packagist/v/Pavlusha311245/unit-php-sdk?labelColor=%231e293b&color=%23702963&link=https%3A%2F%2Fpackagist.org%2Fpackages%2Fpavlusha311245%2Funit-php-sdk" alt="packagist link"></a>
<a href="https://unit-sdk.pavlusha.me/"><img src="https://img.shields.io/website?url=https%3A%2F%2Funit-sdk.pavlusha.me%2F&label=documentation&link=https%3A%2F%2Funit-sdk.pavlusha.me%2F" alt="documentation link"></a>
<img src="https://github.com/Pavlusha311245/nginx-unit-php-sdk/actions/workflows/deploy_codecov.yaml/badge.svg" alt="documentation link">
<a href="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk" >
<img src="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk/graph/badge.svg?token=FGTTDSJ7BX" alt="Codecov dabge"/>
</a>
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function loadUpstreams(array $data): void
}

$servers = array_map(
fn($v, $k) => new Server($v, $k['weight'] ?? 1),
fn ($v, $k) => new Server($v, $k['weight'] ?? 1),
array_keys($upstreamData['servers']),
array_values($upstreamData['servers'])
);
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function setJsModule(array|string $js_module): void
$request->setMethod(HttpMethodsEnum::DELETE)->send($this->getEndpoint());
}

private function getEndpoint()
private function getEndpoint(): string
{
return '/config/settings';
}
Expand Down
22 changes: 21 additions & 1 deletion src/Config/Settings/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use UnitPhpSdk\Contracts\Arrayable;
use UnitPhpSdk\Contracts\Jsonable;
use UnitPhpSdk\Contracts\Uploadable;
use UnitPhpSdk\Enums\HttpMethodsEnum;
use UnitPhpSdk\Http\UnitRequest;

class Http implements Arrayable, Jsonable
class Http implements Arrayable, Jsonable, Uploadable
{
/**
*
Expand Down Expand Up @@ -199,4 +202,21 @@ public function toJson(int $options = 0): string
{
return json_encode($this->toArray(), $options);
}

#[\Override] public function upload(UnitRequest $request)
{
$request->setMethod(HttpMethodsEnum::PUT)->send($this->getEndpoint(), true, [
'json' => array_filter($this->toArray(), fn ($value) => $value !== null)
]);
}

#[\Override] public function remove(UnitRequest $request)
{
$request->setMethod(HttpMethodsEnum::DELETE)->send($this->getEndpoint());
}

private function getEndpoint(): string
{
return '/config/settings/http';
}
}
5 changes: 3 additions & 2 deletions src/Http/UnitRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use UnitPhpSdk\Enums\HttpMethodsEnum;
use UnitPhpSdk\Exceptions\UnitException;

/**
Expand Down Expand Up @@ -60,9 +61,9 @@ private function parseAddress(string $address): string
*
* @param mixed $method
*/
public function setMethod(string $method): self
public function setMethod(string|HttpMethodsEnum $method): self
{
$this->method = mb_strtoupper($method);
$this->method = is_string($method) ? mb_strtoupper($method) : $method->value;

return $this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class Unit implements UnitInterface
public function __construct(
private readonly string $address,
private readonly ?string $socket = null
)
{
) {
$this->request = new UnitRequest(
address: $this->address,
socket: $this->socket
Expand Down

0 comments on commit 2a032d5

Please sign in to comment.