From b7813147a6afbce8e72141ae5bed2d1614803a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Bacik?= Date: Thu, 18 Apr 2024 17:18:46 +0200 Subject: [PATCH] Update requests --- src/SDK/Client.php | 17 ++++++++++------- src/SDK/Client/RequestFactory.php | 15 +++++++++++++-- tests/SDK/ClientTest.php | 8 +++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/SDK/Client.php b/src/SDK/Client.php index 9d77aa6..aa170eb 100644 --- a/src/SDK/Client.php +++ b/src/SDK/Client.php @@ -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); @@ -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()), }; diff --git a/src/SDK/Client/RequestFactory.php b/src/SDK/Client/RequestFactory.php index 46d6144..98560ed 100644 --- a/src/SDK/Client/RequestFactory.php +++ b/src/SDK/Client/RequestFactory.php @@ -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( @@ -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, ]; diff --git a/tests/SDK/ClientTest.php b/tests/SDK/ClientTest.php index efba030..e93d38c 100644 --- a/tests/SDK/ClientTest.php +++ b/tests/SDK/ClientTest.php @@ -43,6 +43,7 @@ public function setUp(): void ), new DefinitionMapper(), ]), + null, ); } @@ -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); @@ -117,6 +118,11 @@ private function createResponse(array $payload): ResponseInterface ->method('getBody') ->willReturn($body); + $response + ->expects($this->once()) + ->method('getStatusCode') + ->willReturn($statusCode); + return $response; } }