Skip to content

Commit 10aab51

Browse files
author
Artem Shteltser
committed
✨ Added *update*, *create* and *delete* methods
1 parent 5679857 commit 10aab51

File tree

2 files changed

+81
-19
lines changed

2 files changed

+81
-19
lines changed

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Client
2626
public function __construct($token_id, $base_url = self::BASE_URL)
2727
{
2828
$this->client = new HTTPClient([
29-
'base_uri' => $base_url,
30-
'headers' => [
29+
'base_uri' => $base_url,
30+
'headers' => [
3131
'tokenid' => $token_id,
3232
'User-Agent' => "stelzer/php-powerlink/" . PowerLink::VERSION,
3333
'Accept' => 'application/json'

src/PowerLink.php

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace PowerLink;
44

55
use PowerLink\Exceptions\PowerLinkException;
6-
7-
use GuzzleHttp\Psr7;
8-
use GuzzleHttp\Exception\RequestException;
6+
use GuzzleHttp\Exception\ClientException;
7+
use Psr\Http\Message\ResponseInterface;
98

109
class PowerLink
1110
{
@@ -74,7 +73,7 @@ public function getToken()
7473
* @param Client|null $client
7574
* @return $this
7675
*/
77-
public function setHttpClient(Client $client = null)
76+
public function setHttpClient($client = null)
7877
{
7978
if ($client === null) {
8079
$client = new Client(self::$token);
@@ -93,22 +92,85 @@ public function getHttpClient()
9392
return $this->client;
9493
}
9594

96-
public function query()
95+
/**
96+
* Function to resolve data from response
97+
*/
98+
private function resolveResult(ResponseInterface $response)
99+
{
100+
return json_decode($response->getBody()->getContents());
101+
}
102+
103+
/**
104+
* @param string $method Request Method
105+
* @param string $path Path
106+
* @param array $params Query params
107+
*
108+
* @throws PowerLinkException When respose returned with error
109+
*/
110+
private function request(string $method, string $path, array $params)
97111
{
98112
try {
99-
$response = $this->client->post('/query', [
100-
'json' => [
101-
'objecttype' => 'asdsdasdfgasddf',
102-
'page_size' => 50,
103-
'page_number' => 1,
104-
'fields' => '*',
105-
]
113+
$response = $this->client->request($method, $path, [
114+
'json' => $params
106115
]);
107-
} catch (RequestException $e) {
108-
echo Psr7\Message::toString($e->getRequest());
109-
if ($e->hasResponse()) {
110-
echo Psr7\Message::toString($e->getResponse());
111-
}
116+
117+
$body = $this->resolveResult($response);
118+
} catch (ClientException $exception) {
119+
$response = $exception->getResponse();
120+
$body = $this->resolveResult($response);
121+
$msg = $body->Message;
122+
throw new PowerLinkException($msg);
112123
}
124+
125+
return $body;
126+
}
127+
128+
/**
129+
* Query request
130+
* @param array $params
131+
*
132+
* @return object
133+
*/
134+
public function query(array $params)
135+
{
136+
return $this->request("POST", "query", $params);
137+
}
138+
139+
/**
140+
* Create request
141+
* @param string $object_type
142+
* @param array $params
143+
*
144+
* @return object
145+
*/
146+
public function create(string $object_type, array $params)
147+
{
148+
return $this->request("POST", "record/$object_type", $params);
149+
}
150+
151+
/**
152+
* Update request
153+
* @param string $object_type
154+
* @param int $id
155+
* @param array $params
156+
*
157+
* @return object
158+
*/
159+
public function update(string $object_type, int $id, array $params)
160+
{
161+
return $this->request("PUT", "record/$object_type/$id", $params);
162+
}
163+
164+
/**
165+
* Delete request
166+
* @param string $object_type
167+
* @param int $id
168+
* @param array $params
169+
*
170+
* @return object
171+
*/
172+
public function delete(string $object_type, int $id, array $params)
173+
{
174+
return $this->request("DELETE", "record/$object_type/$id", $params);
113175
}
114176
}

0 commit comments

Comments
 (0)