Skip to content

Commit

Permalink
Update requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbacik committed Apr 18, 2024
1 parent 6727359 commit b781314
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/SDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ public function getDefinition(string $definitionUuid): Definition
return $this->processRequestAndMapResponse($request, Definition::class);
}

public function createEntity(array $data, string $definitionUuid, string $token): Entity
public function createEntity(Entity $entity, string $token): Entity
{
$payload = [
'data' => $data,
'definition' => '/api/definitions/' . $definitionUuid,
];
$request = $this->requestFactory->createCreateEntityRequest($entity->toArray(), $token);

$request = $this->requestFactory->createCreateEntityRequest($payload, $token);
/** @var Entity */
return $this->processRequestAndMapResponse($request, Entity::class);
}

public function updateEntity(Entity $entity, string $token): Entity
{
$request = $this->requestFactory->createUpdateEntityRequest($entity->id, $entity->toArray(), $token);

/** @var Entity */
return $this->processRequestAndMapResponse($request, Entity::class);
Expand Down Expand Up @@ -105,7 +108,7 @@ private function processRequestAndMapResponse(
try {
$response = $this->httpClient->sendRequest($request);

match($response->getStatusCode()) {
match ($response->getStatusCode()) {
200, 201 => null,
default => throw new RuntimeException('Response status code', $response->getStatusCode()),
};
Expand Down
15 changes: 13 additions & 2 deletions src/SDK/Client/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public function createCreateEntityRequest(array $data, string $token): Request
);
}

public function createUpdateEntityRequest(string $id, array $data, string $token): Request
{
return new Request(
'PATCH',
$this->urlFactory->getEntity($id),
$this->generateHeaders($token, content: 'application/merge-patch+json'),
json_encode($data)
);
}

public function createValidateTokenRequest(string $token): Request
{
return new Request(
Expand All @@ -97,10 +107,11 @@ public function createGetOAuthTokenRequest(string $clientId, string $clientSecre

private function generateHeaders(
?string $token = null,
string $accept = 'application/json'
string $accept = 'application/json',
string $content = 'application/json',
): array {
$headers = [
'Content-Type' => 'application/json',
'Content-Type' => $content,
'Accept' => $accept,
];

Expand Down
8 changes: 7 additions & 1 deletion tests/SDK/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function setUp(): void
),
new DefinitionMapper(),
]),
null,
);
}

Expand Down Expand Up @@ -102,7 +103,7 @@ public function testGetDefinition()
$this->assertEquals('test', $result->slug);
}

private function createResponse(array $payload): ResponseInterface
private function createResponse(array $payload, int $statusCode = 200): ResponseInterface
{
$response = $this->createMock(ResponseInterface::class);
$body = $this->createMock(StreamInterface::class);
Expand All @@ -117,6 +118,11 @@ private function createResponse(array $payload): ResponseInterface
->method('getBody')
->willReturn($body);

$response
->expects($this->once())
->method('getStatusCode')
->willReturn($statusCode);

return $response;
}
}

0 comments on commit b781314

Please sign in to comment.