34
34
use GuzzleHttp \Exception \RequestException ;
35
35
use GuzzleHttp \HandlerStack ;
36
36
use Psr \Cache \CacheItemPoolInterface ;
37
+ use Psr \Cache \InvalidArgumentException ;
37
38
use Psr \Http \Message \ResponseInterface ;
38
39
use Psr \Log \LoggerInterface ;
39
40
@@ -66,6 +67,13 @@ class Client {
66
67
*/
67
68
protected array $ middlewares = [];
68
69
70
+ /**
71
+ * header to Web API request
72
+ *
73
+ * @var array
74
+ */
75
+ protected array $ wepApiHeaders = [];
76
+
69
77
/**
70
78
* Client constructor.
71
79
*
@@ -129,8 +137,11 @@ public function getHttpClient(): HttpClient {
129
137
/**
130
138
* Retrieves OData Service Metadata.
131
139
*
140
+ * @return Metadata
132
141
* @throws AuthenticationException
142
+ * @throws GuzzleException
133
143
* @throws TransportException
144
+ * @throws InvalidArgumentException
134
145
*/
135
146
public function getMetadata (): Metadata {
136
147
if ( $ this ->metadata instanceof Metadata ) {
@@ -178,6 +189,34 @@ public function getMetadata(): Metadata {
178
189
return $ this ->metadata ;
179
190
}
180
191
192
+ /**
193
+ * Get header for Web Api Request
194
+ *
195
+ * @param array|null $headers
196
+ * @param string|null $method
197
+ *
198
+ * @return array
199
+ */
200
+ private function getRequestHeaders ( ?array $ headers = [], ?string $ method = 'GET ' ): array {
201
+ if ( in_array ( $ method , [ 'POST ' , 'PATCH ' ] ) ) {
202
+ $ headers = array_merge ( [ 'Content-Type ' => 'application/json ' ], $ headers );
203
+ }
204
+
205
+ //Odata/Client/Setting more important than the headers in the request
206
+ if ( $ this ->settings ->callerObjectId || $ this ->settings ->callerID ) {
207
+ $ this ->unsetWepApiHeaderByName ( 'CallerObjectId ' );
208
+ $ this ->unsetWepApiHeaderByName ( 'MSCRMCallerID ' );
209
+ }
210
+
211
+ if ( $ this ->settings ->callerObjectId !== null ) {
212
+ $ headers = array_merge ( [ 'CallerObjectId ' => $ this ->settings ->callerObjectId ], $ headers );
213
+ } elseif ( $ this ->settings ->callerID !== null ) {
214
+ $ headers = array_merge ( [ 'MSCRMCallerID ' => $ this ->settings ->callerID ], $ headers );
215
+ }
216
+
217
+ return $ headers ;
218
+ }
219
+
181
220
/**
182
221
* @param string $method
183
222
* @param string $url
@@ -190,13 +229,7 @@ public function getMetadata(): Metadata {
190
229
* @throws TransportException
191
230
*/
192
231
private function doRequest ( string $ method , string $ url , $ data = null , array $ headers = [] ): ResponseInterface {
193
- if ( in_array ( $ method , [ 'POST ' , 'PATCH ' ] ) ) {
194
- $ headers = array_merge ( [ 'Content-Type ' => 'application/json ' ], $ headers );
195
- }
196
-
197
- if ( $ this ->settings ->callerID !== null ) {
198
- $ headers = array_merge ( [ 'MSCRMCallerID ' => '$this->settings->callerID ' ], $ headers );
199
- }
232
+ $ headers = $ this ->getRequestHeaders ( $ headers , $ method );
200
233
201
234
try {
202
235
$ payload = [
@@ -577,7 +610,7 @@ public function associate(
577
610
string $ fromEntityId ,
578
611
string $ navProperty ,
579
612
string $ toEntityCollection ,
580
- string $ toEntityId
613
+ string $ toEntityId,
581
614
): void {
582
615
$ url = sprintf ( '%s%s(%s)/%s/$ref ' , $ this ->settings ->getEndpointURI (), $ fromEntityCollection , $ fromEntityId , $ navProperty );
583
616
$ data = [ Annotation::ODATA_ID => sprintf ( '%s%s(%s) ' , $ this ->settings ->getEndpointURI (), $ toEntityCollection , $ toEntityId ) ];
@@ -600,9 +633,10 @@ public function disassociate(
600
633
string $ fromEntityId ,
601
634
string $ navProperty ,
602
635
string $ toEntityCollection ,
603
- string $ toEntityId
636
+ string $ toEntityId,
604
637
): void {
605
- $ url = sprintf ( '%s%s(%s)/%s/$ref?$id=%s%s(%s) ' , $ this ->settings ->getEndpointURI (), $ fromEntityCollection , $ fromEntityId , $ navProperty , $ this ->settings ->getEndpointURI (), $ toEntityCollection , $ toEntityId );
638
+ $ url = sprintf ( '%s%s(%s)/%s/$ref?$id=%s%s(%s) ' , $ this ->settings ->getEndpointURI (), $ fromEntityCollection , $ fromEntityId , $ navProperty , $ this ->settings ->getEndpointURI (),
639
+ $ toEntityCollection , $ toEntityId );
606
640
$ this ->doRequest ( 'DELETE ' , $ url );
607
641
}
608
642
@@ -795,4 +829,20 @@ protected static function getEntityId( ResponseInterface $response ): ?string {
795
829
return $ id ;
796
830
}
797
831
832
+ public function getWepApiHeaderByName ( $ headerName ): array {
833
+ return $ this ?->wepApiHeaders[ $ headerName ];
834
+ }
835
+
836
+ public function unsetWepApiHeaderByName ( $ headerName ): void {
837
+ unset( $ this ->wepApiHeaders [ $ headerName ] );
838
+ }
839
+
840
+ public function setWepApiHeaderByName ( $ headerName , $ headerValue ): void {
841
+ $ this ->wepApiHeaders [ $ headerName ] = $ headerValue ;
842
+ }
843
+
844
+ public function getWepApiHeaders (): array {
845
+ return $ this ->wepApiHeaders ;
846
+ }
847
+
798
848
}
0 commit comments