7
7
use Closure ;
8
8
use JustSteveKing \UriBuilder \Uri ;
9
9
use Psr \Http \Message \ResponseInterface ;
10
- use Psr \Http \Message \StreamInterface ;
11
- use WrkFlow \ApiSdkBuilder \Actions \SendRequestAction ;
12
10
use WrkFlow \ApiSdkBuilder \Contracts \ApiFactoryContract ;
13
11
use WrkFlow \ApiSdkBuilder \Endpoints \AbstractEndpoint ;
14
12
use WrkFlow \ApiSdkBuilder \Environments \AbstractEnvironment ;
17
15
use WrkFlow \ApiSdkBuilder \Exceptions \ServerFailedException ;
18
16
use WrkFlow \ApiSdkBuilder \Interfaces \ApiInterface ;
19
17
use WrkFlow \ApiSdkBuilder \Interfaces \EnvironmentOverrideEndpointsInterface ;
20
- use WrkFlow \ApiSdkBuilder \Interfaces \OptionsInterface ;
21
- use WrkFlow \ApiSdkBuilder \Responses \AbstractResponse ;
22
18
23
19
abstract class AbstractApi implements ApiInterface
24
20
{
@@ -29,25 +25,17 @@ abstract class AbstractApi implements ApiInterface
29
25
*/
30
26
private array $ cachedEndpoints = [];
31
27
32
- private ?SendRequestAction $ sendRequestAction = null ;
33
-
34
28
/**
35
29
* @var array<class-string, class-string<AbstractEndpoint>>
36
30
*/
37
31
private readonly array $ overrideEndpoints ;
38
32
39
- /**
40
- * @param array<class-string, class-string<AbstractEndpoint>> $overrideEndpoints
41
- */
33
+
42
34
public function __construct (
43
35
private readonly AbstractEnvironment $ environment ,
44
- private readonly ApiFactoryContract $ factory ,
45
- array $ overrideEndpoints = []
36
+ private readonly ApiFactoryContract $ factory
46
37
) {
47
- $ this ->overrideEndpoints = array_merge (
48
- $ overrideEndpoints ,
49
- $ environment instanceof EnvironmentOverrideEndpointsInterface ? $ environment ->endpoints () : []
50
- );
38
+ $ this ->overrideEndpoints = $ environment instanceof EnvironmentOverrideEndpointsInterface ? $ environment ->endpoints () : [];
51
39
}
52
40
53
41
final public function environment (): AbstractEnvironment
@@ -67,124 +55,6 @@ final public function uri(): Uri
67
55
return $ this ->environment ->uri ();
68
56
}
69
57
70
- final public function get (
71
- string $ responseClass ,
72
- Uri $ uri ,
73
- array $ headers = [],
74
- ?int $ expectedResponseStatusCode = null ,
75
- Closure $ shouldIgnoreLoggersOnError = null ,
76
- ): AbstractResponse {
77
- $ request = $ this ->factory ()
78
- ->request ()
79
- ->createRequest ('GET ' , $ uri ->toString ());
80
-
81
- return $ this ->sendRequestAction ()
82
- ->execute (
83
- api: $ this ,
84
- request: $ request ,
85
- responseClass: $ responseClass ,
86
- headers: $ headers ,
87
- expectedResponseStatusCode: $ expectedResponseStatusCode ,
88
- shouldIgnoreLoggersOnError: $ shouldIgnoreLoggersOnError ,
89
- );
90
- }
91
-
92
- final public function post (
93
- string $ responseClass ,
94
- Uri $ uri ,
95
- OptionsInterface |StreamInterface |string $ body = null ,
96
- array $ headers = [],
97
- ?int $ expectedResponseStatusCode = null ,
98
- Closure $ shouldIgnoreLoggersOnError = null ,
99
- ): AbstractResponse {
100
- $ request = $ this ->factory ()
101
- ->request ()
102
- ->createRequest ('POST ' , $ uri ->toString ());
103
-
104
- return $ this ->sendRequestAction ()
105
- ->execute (
106
- api: $ this ,
107
- request: $ request ,
108
- responseClass: $ responseClass ,
109
- body: $ body ,
110
- headers: $ headers ,
111
- expectedResponseStatusCode: $ expectedResponseStatusCode ,
112
- shouldIgnoreLoggersOnError: $ shouldIgnoreLoggersOnError ,
113
- );
114
- }
115
-
116
- final public function put (
117
- string $ responseClass ,
118
- Uri $ uri ,
119
- OptionsInterface |StreamInterface |string $ body = null ,
120
- array $ headers = [],
121
- ?int $ expectedResponseStatusCode = null ,
122
- Closure $ shouldIgnoreLoggersOnError = null ,
123
- ): AbstractResponse {
124
- $ request = $ this ->factory ()
125
- ->request ()
126
- ->createRequest ('PUT ' , $ uri ->toString ());
127
-
128
- return $ this ->sendRequestAction ()
129
- ->execute (
130
- api: $ this ,
131
- request: $ request ,
132
- responseClass: $ responseClass ,
133
- body: $ body ,
134
- headers: $ headers ,
135
- expectedResponseStatusCode: $ expectedResponseStatusCode ,
136
- shouldIgnoreLoggersOnError: $ shouldIgnoreLoggersOnError ,
137
- );
138
- }
139
-
140
- final public function delete (
141
- string $ responseClass ,
142
- Uri $ uri ,
143
- OptionsInterface |StreamInterface |string $ body = null ,
144
- array $ headers = [],
145
- ?int $ expectedResponseStatusCode = null ,
146
- Closure $ shouldIgnoreLoggersOnError = null ,
147
- ): AbstractResponse {
148
- $ request = $ this ->factory ()
149
- ->request ()
150
- ->createRequest ('DELETE ' , $ uri ->toString ());
151
-
152
- return $ this ->sendRequestAction ()
153
- ->execute (
154
- api: $ this ,
155
- request: $ request ,
156
- responseClass: $ responseClass ,
157
- body: $ body ,
158
- headers: $ headers ,
159
- expectedResponseStatusCode: $ expectedResponseStatusCode ,
160
- shouldIgnoreLoggersOnError: $ shouldIgnoreLoggersOnError ,
161
- );
162
- }
163
-
164
- public function fake (
165
- ResponseInterface $ response ,
166
- string $ responseClass ,
167
- Uri $ uri ,
168
- OptionsInterface |StreamInterface |string $ body = null ,
169
- array $ headers = [],
170
- ?int $ expectedResponseStatusCode = null ,
171
- Closure $ shouldIgnoreLoggersOnError = null ,
172
- ): AbstractResponse {
173
- return $ this ->sendRequestAction ()
174
- ->execute (
175
- api: $ this ,
176
- request: $ this ->factory ()
177
- ->request ()
178
- ->createRequest ('FAKE ' , $ uri ->toString ()),
179
- responseClass: $ responseClass ,
180
- body: $ body ,
181
- headers: $ headers ,
182
- expectedResponseStatusCode: $ expectedResponseStatusCode ,
183
- fakedResponse: $ response ,
184
- shouldIgnoreLoggersOnError: $ shouldIgnoreLoggersOnError ,
185
- );
186
- }
187
-
188
58
public function createFailedResponseException (int $ statusCode , ResponseInterface $ response ): ResponseException
189
59
{
190
60
if ($ statusCode >= 400 && $ statusCode < 500 ) {
@@ -194,6 +64,11 @@ public function createFailedResponseException(int $statusCode, ResponseInterface
194
64
return new ServerFailedException ($ response );
195
65
}
196
66
67
+ final public function shouldIgnoreLoggersOnException (): ?Closure
68
+ {
69
+ return null ;
70
+ }
71
+
197
72
/**
198
73
* @template T of AbstractEndpoint
199
74
*
@@ -247,15 +122,4 @@ private function getOverrideEndpointClassIfCan(string $endpoint): string
247
122
248
123
return $ endpoint ;
249
124
}
250
-
251
- private function sendRequestAction (): SendRequestAction
252
- {
253
- if ($ this ->sendRequestAction instanceof SendRequestAction === false ) {
254
- $ this ->sendRequestAction = $ this ->factory ()
255
- ->container ()
256
- ->make (SendRequestAction::class);
257
- }
258
-
259
- return $ this ->sendRequestAction ;
260
- }
261
125
}
0 commit comments