Skip to content

Commit 94d29bd

Browse files
committed
Send response data through pipelines
1 parent 34e2b40 commit 94d29bd

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"illuminate/support": "^5.8"
99
},
1010
"suggest": {
11-
"illuminate/database": "Allows to send request with data from model"
11+
"illuminate/database": "Allows to send request with data from model",
12+
"illuminate/pipeline": "Allows to send response data through pipelines"
1213
},
1314
"autoload": {
1415
"psr-4": {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
use Exception;
6+
7+
class PipelineNotAvailable extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null) {
10+
parent::__construct($message, $code, $previous);
11+
}
12+
13+
public function __toString() {
14+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
15+
}
16+
17+
}

src/Response.php

+41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Ivan770\HttpClient;
44

5+
use Ivan770\HttpClient\Exceptions\PipelineNotAvailable;
56
use Symfony\Component\HttpClient\Exception\JsonException;
7+
use Illuminate\Pipeline\Pipeline;
68

79
/**
810
* @method int getStatusCode() Get response status code
@@ -14,11 +16,30 @@ class Response
1416
{
1517
protected $baseResponse;
1618

19+
protected $pipeline;
20+
1721
public function __construct($baseResponse)
1822
{
1923
$this->baseResponse = $baseResponse;
2024
}
2125

26+
protected function pipelineAvailable()
27+
{
28+
//TODO: Remove pipeline vendor lock.
29+
if (class_exists(Pipeline::class)) {
30+
return true;
31+
}
32+
throw new PipelineNotAvailable("Pipeline class cannot be found");
33+
}
34+
35+
protected function getPipeline()
36+
{
37+
if ($this->pipelineAvailable() && is_null($this->pipeline)) {
38+
$this->pipeline = new Pipeline();
39+
}
40+
return $this->pipeline;
41+
}
42+
2243
/**
2344
* Create collection from response
2445
*
@@ -44,6 +65,26 @@ public function getContent($throw = true)
4465
}
4566
}
4667

68+
/**
69+
* Pass response content to function
70+
*
71+
* @param \Closure $function Function to call
72+
*/
73+
public function then($function)
74+
{
75+
return $function->call($this, $this->getContent());
76+
}
77+
78+
/**
79+
* Pass response content to pipeline
80+
*
81+
* @return Pipeline
82+
*/
83+
public function pipeline()
84+
{
85+
return $this->getPipeline()->send($this->getContent());
86+
}
87+
4788
public function __call($name, $arguments)
4889
{
4990
return $this->baseResponse->$name(...$arguments);

0 commit comments

Comments
 (0)