Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tom committed Feb 19, 2021
2 parents 091c4d0 + 2727ed3 commit 7abd5fd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function create($data)
$response = $this->httpClient->post($this->getPath(), [
'body' => json_encode($data),
'headers' => $this->getHeaders(),
'connect_timeout' => 1
]);
} catch (BadResponseException $e) {
throw new EntityCreationException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -67,6 +68,7 @@ public function update($id, $data)
$response = $this->httpClient->patch($this->getPathWithId($id), [
'body' => json_encode($data),
'headers' => $this->getHeaders(true),
'connect_timeout' => 1
]);
} catch (BadResponseException $e) {
throw new EntityCreationException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -87,7 +89,8 @@ public function find($id, $filters = [])
try {
$response = $this->httpClient->get($this->getPathWithId($id), [
'headers' => $this->getHeaders(),
'query' => $filters
'query' => $filters,
'connect_timeout' => 1
]);
} catch (BadResponseException $e) {
throw new EntityFetchException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -109,7 +112,8 @@ public function findAll($filters = [], $pageSize = 10, $page = 1)
try {
$response = $this->httpClient->get($this->getPath(), [
'headers' => $this->getHeaders(),
'query' => array_merge(compact('pageSize', 'page'), $filters)
'query' => array_merge(compact('pageSize', 'page'), $filters),
'connect_timeout' => 1
]);
} catch (BadResponseException $e) {
throw new EntityFetchException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -119,6 +123,26 @@ public function findAll($filters = [], $pageSize = 10, $page = 1)
->getContents(), true);
}

/**
* @param string $path
*
* @return array
* @throws EntityFetchException
*/
public function getByGivenPath(string $path): array
{
try {
$response = $this->httpClient->get($path, [
'headers' => $this->getHeaders(),
'connect_timeout' => 1
]);
} catch (BadResponseException $e) {
throw new EntityFetchException($e->getMessage(), $e->getCode(), $e);
}

return json_decode($response->getBody()->getContents(), true);
}

/**
* @param bool $patch
* @return array
Expand All @@ -127,7 +151,8 @@ protected function getHeaders($patch = false): array
{
$headers = [
'x-api-key' => $this->credentials['api_key'],
'content-type' => 'application/json'
'content-type' => 'application/json',
'connection' => 'close'
];

if ($patch) {
Expand Down
31 changes: 31 additions & 0 deletions src/RelationClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LiveStyled;

use LogicException;

abstract class RelationClient extends Client
{
/**
* @param $id
* @param $data
*
* @return void
*/
public function update($id, $data)
{
throw new LogicException('Relation entry cannot be updated');
}

/**
* @param $id
* @param array $filters
*
* @return mixed
*/
public function find($id, $filters = [])
{
throw new LogicException('Relation entry cannot be found by id');
}

}
4 changes: 2 additions & 2 deletions src/UserManagement/AudienceDeviceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace LiveStyled\UserManagement;

use LiveStyled\Client;
use LiveStyled\CrudClient;
use LiveStyled\RelationClient;

class AudienceDeviceClient extends Client implements CrudClient
class AudienceDeviceClient extends RelationClient implements CrudClient
{
protected function getPath()
{
Expand Down

0 comments on commit 7abd5fd

Please sign in to comment.