1+ <?php
2+
3+ namespace ReactphpQuerylist ;
4+
5+ use Psr \Http \Message \ResponseInterface ;
6+ use React \Http \Browser ;
7+ use React \Promise \Deferred ;
8+ use React \Promise \PromiseInterface ;
9+ use React \Stream \ReadableStreamInterface ;
10+
11+ class Client
12+ {
13+ /**
14+ * @param string $requestMethod
15+ * @param string $url
16+ * @param array $headers
17+ * @param ReadableStreamInterface|string $body
18+ * @return PromiseInterface
19+ */
20+ public function execute (
21+ string $ requestMethod ,
22+ string $ url ,
23+ array $ headers = [],
24+ $ body = ''
25+ ): PromiseInterface
26+ {
27+ return $ this ->prepareQueryable (
28+ (new Browser ())->request ($ requestMethod , $ url , $ headers , $ body )
29+ );
30+ }
31+
32+ /**
33+ * Construct QueryList object from response body
34+ *
35+ * @param PromiseInterface $promise
36+ * @return PromiseInterface
37+ */
38+ protected function prepareQueryable (PromiseInterface $ promise ): PromiseInterface
39+ {
40+ $ deferred = new Deferred ();
41+
42+ // Construct queryable object when request is success
43+ $ promise ->then (function (ResponseInterface $ response ) use ($ deferred ) {
44+ $ deferred ->resolve (new Queryable ($ response ));
45+ });
46+
47+ // Forward any error to child promise
48+ $ promise ->otherwise (fn ($ reason ) => $ deferred ->reject ($ reason ));
49+
50+ return $ deferred ->promise ();
51+ }
52+
53+ /**
54+ * Send http GET request
55+ *
56+ * @param string $url
57+ * @param array $headers
58+ * @return PromiseInterface
59+ */
60+ public static function get (string $ url , array $ headers = []): PromiseInterface
61+ {
62+ return (new static ())->execute ('GET ' , $ url , $ headers );
63+ }
64+
65+ /**
66+ * Send http POST request
67+ *
68+ * @param string $url
69+ * @param array $headers
70+ * @param ReadableStreamInterface|string $body
71+ * @return PromiseInterface
72+ */
73+ public static function post (string $ url , array $ headers = [], $ body = '' ): PromiseInterface
74+ {
75+ return (new static ())->execute ('POST ' , $ url , $ headers , $ body );
76+ }
77+
78+ /**
79+ * Send http HEAD request
80+ *
81+ * @param string $url
82+ * @param array $headers
83+ * @return PromiseInterface
84+ */
85+ public static function head (string $ url , array $ headers = []): PromiseInterface
86+ {
87+ return (new static ())->execute ('HEAD ' , $ url , $ headers );
88+ }
89+
90+ /**
91+ * Send http PATCH request
92+ *
93+ * @param string $url
94+ * @param array $headers
95+ * @param ReadableStreamInterface|string $body
96+ * @return PromiseInterface
97+ */
98+ public static function patch (string $ url , array $ headers = [], $ body = '' ): PromiseInterface
99+ {
100+ return (new static ())->execute ('PATCH ' , $ url , $ headers , $ body );
101+ }
102+
103+ /**
104+ * Send http PUT request
105+ *
106+ * @param string $url
107+ * @param array $headers
108+ * @param ReadableStreamInterface|string $body
109+ * @return PromiseInterface
110+ */
111+ public static function put (string $ url , array $ headers = [], $ body = '' ): PromiseInterface
112+ {
113+ return (new static ())->execute ('PUT ' , $ url , $ headers , $ body );
114+ }
115+
116+ /**
117+ * Send http DELETE request
118+ *
119+ * @param string $url
120+ * @param array $headers
121+ * @return PromiseInterface
122+ */
123+ public static function delete (string $ url , array $ headers = []): PromiseInterface
124+ {
125+ return (new static ())->execute ('DELETE ' , $ url , $ headers );
126+ }
127+
128+ /**
129+ * Submit post request with json body
130+ *
131+ * @param string $url
132+ * @param array $payload ['key' => 'value']
133+ * @param array $headers
134+ * @return PromiseInterface
135+ */
136+ public static function postJson (string $ url , array $ payload , array $ headers = []): PromiseInterface
137+ {
138+ return self ::post (
139+ $ url ,
140+ array_merge (['Content-Type ' => 'application/json ' ], $ headers ),
141+ json_encode ($ payload )
142+ );
143+ }
144+
145+ /**
146+ * Submit form
147+ *
148+ * @param string $url
149+ * @param array $fields ['name' => 'John Doe']
150+ * @param array $headers
151+ * @return PromiseInterface
152+ */
153+ public static function postForm (string $ url , array $ fields , array $ headers = []): PromiseInterface
154+ {
155+ return self ::post (
156+ $ url ,
157+ array_merge (['Content-Type ' => 'application/x-www-form-urlencoded ' ], $ headers ),
158+ http_build_query ($ fields )
159+ );
160+ }
161+ }
0 commit comments