Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Feb 27, 2024
1 parent c5dd2fe commit bb6ae82
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,39 @@ public function head(string $path, array|RequestQuery $params = []): mixed
return $this->executeRequest($request);
}

public function post(string $path, string|array $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
public function post(string $path, string|array|null $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
{
$this->contentType($contentType ?? static::JSON_CONTENT_TYPE);

$request = $this->requestFactory->createRequest(
'POST',
$this->baseUrl . $path . $this->queryToString($params)
)->withBody($this->applySentBodyParsing($body));
);

if ($body) {
$request->withBody($this->applySentBodyParsing($body));
}

return $this->executeRequest($request);
}

public function put(string $path, string|array $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
public function put(string $path, string|array|null $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
{
$this->contentType($contentType ?? static::JSON_CONTENT_TYPE);

$request = $this->requestFactory->createRequest(
'PUT',
$this->baseUrl . $path . $this->queryToString($params)
)->withBody($this->applySentBodyParsing($body));
);

if ($body) {
$request->withBody($this->applySentBodyParsing($body));
}

return $this->executeRequest($request);
}

public function patch(string $path, string|array $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
public function patch(string $path, string|array|null $body = null, array|RequestQuery $params = [], ?string $contentType = null): mixed
{
$this->contentType($contentType ?? static::JSON_CONTENT_TYPE);

Expand All @@ -134,13 +142,17 @@ public function patch(string $path, string|array $body = null, array|RequestQuer
return $this->executeRequest($request);
}

public function delete(string $path, array|RequestQuery $params = []): mixed
public function delete(string $path, string|array|null $body = null, array|RequestQuery $params = []): mixed
{
$request = $this->requestFactory->createRequest(
'DELETE',
$this->baseUrl . $path . $this->queryToString($params)
);

if ($body) {
$request->withBody($this->applySentBodyParsing($body));
}

return $this->executeRequest($request);
}

Expand Down Expand Up @@ -184,9 +196,13 @@ private function applySentBodyParsing(string|array $body): StreamInterface
static::JSON_CONTENT_TYPE => json_encode($body),
// TODO:
// static::STREAM_CONTENT_TYPE =>
default => $body,
default => is_array($body) ? json_encode($body) : $body,
};

if (! $parsedBody) {
throw new \Exception('Body serialization format error, sent falsy or empty value.');
}

return $this->streamFactory->createStream($parsedBody);
}

Expand Down Expand Up @@ -220,7 +236,7 @@ private function parseResponse(ResponseInterface $response): mixed

$bodyContent = $this->applyResponseParsing($response);

if ($response->getStatusCode() >= 300) {
if ((is_string($bodyContent) || is_array($bodyContent)) && $response->getStatusCode() >= 300) {
throw DockerApiException::fromResponse($response, $bodyContent);
}

Expand Down

0 comments on commit bb6ae82

Please sign in to comment.