@@ -27,12 +27,18 @@ class Client implements Http
27
27
private ClientInterface $ http ;
28
28
private RequestFactoryInterface $ requestFactory ;
29
29
private StreamFactoryInterface $ streamFactory ;
30
- /** @var array<string,string> */
30
+ /**
31
+ * @var array<string, string|string[]>
32
+ */
31
33
private array $ headers ;
34
+ /**
35
+ * @var non-empty-string
36
+ */
32
37
private string $ baseUrl ;
33
38
private Json $ json ;
34
39
35
40
/**
41
+ * @param non-empty-string $url
36
42
* @param array<int, string> $clientAgents
37
43
*/
38
44
public function __construct (
@@ -72,7 +78,7 @@ public function get(string $path, array $query = [])
72
78
}
73
79
74
80
/**
75
- * @param mixed |null $body
81
+ * @param non-empty-string |null $contentType
76
82
*
77
83
* @throws ApiException
78
84
* @throws ClientExceptionInterface
@@ -81,57 +87,48 @@ public function get(string $path, array $query = [])
81
87
*/
82
88
public function post (string $ path , $ body = null , array $ query = [], ?string $ contentType = null )
83
89
{
84
- if (!\is_null ($ contentType )) {
85
- $ this ->headers ['Content-type ' ] = $ contentType ;
86
- } else {
87
- $ this ->headers ['Content-type ' ] = 'application/json ' ;
90
+ if (null === $ contentType ) {
88
91
$ body = $ this ->json ->serialize ($ body );
89
92
}
90
93
$ request = $ this ->requestFactory ->createRequest (
91
94
'POST ' ,
92
95
$ this ->baseUrl .$ path .$ this ->buildQueryString ($ query )
93
96
)->withBody ($ this ->streamFactory ->createStream ($ body ));
94
97
95
- return $ this ->execute ($ request );
98
+ return $ this ->execute ($ request, [ ' Content-type ' => $ contentType ?? ' application/json ' ] );
96
99
}
97
100
101
+ /**
102
+ * @param non-empty-string|null $contentType
103
+ *
104
+ * @throws ApiException
105
+ * @throws ClientExceptionInterface
106
+ * @throws CommunicationException
107
+ * @throws JsonEncodingException
108
+ */
98
109
public function put (string $ path , $ body = null , array $ query = [], ?string $ contentType = null )
99
110
{
100
- if (!\is_null ($ contentType )) {
101
- $ this ->headers ['Content-type ' ] = $ contentType ;
102
- } else {
103
- $ this ->headers ['Content-type ' ] = 'application/json ' ;
111
+ if (null === $ contentType ) {
104
112
$ body = $ this ->json ->serialize ($ body );
105
113
}
106
114
$ request = $ this ->requestFactory ->createRequest (
107
115
'PUT ' ,
108
116
$ this ->baseUrl .$ path .$ this ->buildQueryString ($ query )
109
117
)->withBody ($ this ->streamFactory ->createStream ($ body ));
110
118
111
- return $ this ->execute ($ request );
119
+ return $ this ->execute ($ request, [ ' Content-type ' => $ contentType ?? ' application/json ' ] );
112
120
}
113
121
114
- /**
115
- * @param mixed|null $body
116
- *
117
- * @throws ApiException
118
- * @throws JsonEncodingException
119
- */
120
122
public function patch (string $ path , $ body = null , array $ query = [])
121
123
{
122
- $ this ->headers ['Content-type ' ] = 'application/json ' ;
123
124
$ request = $ this ->requestFactory ->createRequest (
124
125
'PATCH ' ,
125
126
$ this ->baseUrl .$ path .$ this ->buildQueryString ($ query )
126
127
)->withBody ($ this ->streamFactory ->createStream ($ this ->json ->serialize ($ body )));
127
128
128
- return $ this ->execute ($ request );
129
+ return $ this ->execute ($ request, [ ' Content-type ' => ' application/json ' ] );
129
130
}
130
131
131
- /**
132
- * @throws ClientExceptionInterface
133
- * @throws ApiException
134
- */
135
132
public function delete (string $ path , array $ query = [])
136
133
{
137
134
$ request = $ this ->requestFactory ->createRequest (
@@ -143,13 +140,15 @@ public function delete(string $path, array $query = [])
143
140
}
144
141
145
142
/**
143
+ * @param array<string, string|string[]> $headers
144
+ *
146
145
* @throws ApiException
147
146
* @throws ClientExceptionInterface
148
147
* @throws CommunicationException
149
148
*/
150
- private function execute (RequestInterface $ request )
149
+ private function execute (RequestInterface $ request, array $ headers = [] )
151
150
{
152
- foreach ($ this ->headers as $ header => $ value ) {
151
+ foreach (array_merge ( $ this ->headers , $ headers ) as $ header => $ value ) {
153
152
$ request = $ request ->withAddedHeader ($ header , $ value );
154
153
}
155
154
0 commit comments