Skip to content

Commit

Permalink
added separate makeRequest method
Browse files Browse the repository at this point in the history
  • Loading branch information
rcerljenko committed Dec 5, 2023
1 parent 5fb5974 commit 2a91971
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/OneSignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,35 @@ public function sendRequest(RequestInterface $request): array
$content['_status_code'] = $response->getStatusCode();
}

return $content;
}

public function makeRequest(RequestInterface $request): array
{
$response = $this->httpClient->sendRequest($request);

$contentType = $response->getHeader('Content-Type')[0] ?? 'application/json';

if (!preg_match('/\bjson\b/i', $contentType)) {
throw new JsonException("Response content-type is '$contentType' while a JSON-compatible one was expected.");
}

$content = $response->getBody()->__toString();

try {
$content = json_decode($content, true, 512, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonException($e->getMessage(), $e->getCode(), $e);
}

if (!is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned.', gettype($content)));
}

if (!isset($content['_status_code'])) {
$content['_status_code'] = $response->getStatusCode();
}

if ($content['_status_code'] >= 400) {
throw new UnsuccessfulResponse($request, $response);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Segments.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function list(ListSegments $listSegmentsDto): ListSegmentsResponse
$request = $this->createRequest('GET', '/apps/'.$appId.'/segments?'.http_build_query($listSegmentsDto->toArray()));
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");

return ListSegmentsResponse::makeFromResponse($this->client->sendRequest($request));
return ListSegmentsResponse::makeFromResponse($this->client->makeRequest($request));
}

/**
Expand All @@ -46,7 +46,7 @@ public function create(CreateSegment $createSegmentDto): CreateSegmentResponse
$request = $request->withHeader('Content-Type', 'application/json');
$request = $request->withBody($this->createStream($createSegmentDto->toArray()));

return CreateSegmentResponse::makeFromResponse($this->client->sendRequest($request));
return CreateSegmentResponse::makeFromResponse($this->client->makeRequest($request));
}

/**
Expand All @@ -63,6 +63,6 @@ public function delete(string $id): DeleteSegmentResponse
$request = $this->createRequest('DELETE', '/apps/'.$appId.'/segments/'.$id);
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");

return DeleteSegmentResponse::makeFromResponse($this->client->sendRequest($request));
return DeleteSegmentResponse::makeFromResponse($this->client->makeRequest($request));
}
}

0 comments on commit 2a91971

Please sign in to comment.