Skip to content

Commit 66c1a21

Browse files
authored
Merge pull request #45 from chadicus/master
Add send and startSend to ClientInterface
2 parents fbab163 + ccbc569 commit 66c1a21

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/Client.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ public function delete(string $resource, string $id = null, array $data = null)
282282
return $this->end($this->startDelete($resource, $id, $data));
283283
}
284284

285+
/**
286+
* Performs a request to the given URI and returns the response.
287+
*
288+
* @param string $method The HTTP method of the request to send.
289+
* @param string $uri A relative api URI to which the POST request will be made.
290+
* @param array $data Array of data to be sent as the POST body.
291+
*
292+
* @return Response
293+
*/
294+
public function send(string $method, string $uri, array $data = null) : Response
295+
{
296+
return $this->end($this->startSend($method, $uri, $data));
297+
}
298+
299+
/**
300+
* Starts a request to the given URI.
301+
*
302+
* @param string $method The HTTP method of the request to send.
303+
* @param string $uri A relative api URI to which the POST request will be made.
304+
* @param array $data Array of data to be sent as the POST body.
305+
*
306+
* @return string opaque handle to be given to endDelete()
307+
*/
308+
public function startSend(string $method, string $uri, array $data = null) : string
309+
{
310+
$url = "{$this->baseUrl}/{$uri}";
311+
$json = $data !== null ? json_encode($data) : null;
312+
return $this->start($url, $method, $json, ['Content-Type' => 'application/json']);
313+
}
314+
285315
/**
286316
* Get response of start*() method
287317
*

src/ClientInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ public function startDelete(string $resource, string $id = null, array $data = n
9191
*/
9292
public function delete(string $resource, string $id = null, array $data = null) : Response;
9393

94+
/**
95+
* Performs a request to the given URI and returns the response.
96+
*
97+
* @param string $method The HTTP method of the request to send.
98+
* @param string $uri A relative api URI to which the POST request will be made.
99+
* @param array $data Array of data to be sent as the POST body.
100+
*
101+
* @return Response
102+
*/
103+
public function send(string $method, string $uri, array $data = null) : Response;
104+
105+
/**
106+
* Starts a request to the given URI.
107+
*
108+
* @param string $method The HTTP method of the request to send.
109+
* @param string $uri A relative api URI to which the POST request will be made.
110+
* @param array $data Array of data to be sent as the POST body.
111+
*
112+
* @return string opaque handle to be given to endDelete()
113+
*/
114+
public function startSend(string $method, string $uri, array $data = null) : string;
115+
94116
/**
95117
* Get response of start*() method
96118
*

tests/ClientTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,38 @@ function (RequestInterface $request) {
607607
$this->assertSame(['Content-Type' => ['application/json']], $response->getResponseHeaders());
608608
}
609609

610+
/**
611+
* @test
612+
* @covers ::send
613+
* @covers ::startSend
614+
*/
615+
public function send()
616+
{
617+
$test = $this;
618+
$adapter = new FakeAdapter(
619+
function (RequestInterface $request) use ($test) {
620+
if (substr($request->getUri(), -5) === 'token') {
621+
return new Psr7Response(
622+
200,
623+
['Content-Type' => ['application/json']],
624+
json_encode(['access_token' => 'a token', 'expires_in' => 1])
625+
);
626+
}
627+
628+
$test->assertSame('PATCH', $request->getMethod());
629+
$test->assertSame('baseUrl/v1/item-logs/123/log-error', (string)$request->getUri());
630+
$test->assertSame('{"message":"the message"}', (string)$request->getBody());
631+
return new Psr7Response(204);
632+
}
633+
);
634+
635+
$client = new Client($adapter, $this->getAuthentication(), 'baseUrl/v1');
636+
637+
$response = $client->send('PATCH', 'item-logs/123/log-error', ['message' => 'the message']);
638+
$this->assertSame(204, $response->getHttpCode());
639+
$this->assertSame([], $response->getResponse());
640+
}
641+
610642
/**
611643
* Verify behavior of startDelete when no id is given.
612644
*

0 commit comments

Comments
 (0)