1 | <?php |
||
27 | class GuzzleClient extends BaseClient implements ClientInterface |
||
28 | { |
||
29 | /** |
||
30 | * Request method. |
||
31 | * |
||
32 | * Generic format for HTTP client request method. |
||
33 | * |
||
34 | * @param string $requestMethod GET/POST/PUT/DELETE |
||
35 | * @param string $path full path to REST resource without domain, ex. 'accounts/XXXXXXXX/contacts' |
||
36 | * @param array $params optional params for GET/POST request |
||
37 | * @param string $accessToken Access Token for Wrike access |
||
38 | * |
||
39 | * @see \Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum |
||
40 | * @see \Zibios\WrikePhpLibrary\Enum\Api\RequestPathFormatEnum |
||
41 | * |
||
42 | * @throws \Throwable |
||
43 | * @throws ApiException |
||
44 | * |
||
45 | * @return ResponseInterface |
||
46 | */ |
||
47 | 12 | public function executeRequestForParams( |
|
61 | |||
62 | /** |
||
63 | * Main method for calculating request params. |
||
64 | * |
||
65 | * @param string $requestMethod |
||
66 | * @param array $params |
||
67 | * @param string $accessToken |
||
68 | * |
||
69 | * @throws \InvalidArgumentException |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 11 | protected function calculateOptionsForParams(string $requestMethod, array $params, string $accessToken): array |
|
74 | { |
||
75 | 11 | $options = $this->prepareBaseOptions($accessToken); |
|
76 | 10 | if (0 === \count($params)) { |
|
77 | 5 | return $options; |
|
78 | } |
||
79 | |||
80 | 5 | switch ($requestMethod) { |
|
81 | case RequestMethodEnum::GET: |
||
82 | case RequestMethodEnum::DELETE: |
||
83 | 2 | $options['query'] = $params; |
|
84 | 2 | break; |
|
85 | case RequestMethodEnum::PUT: |
||
86 | case RequestMethodEnum::POST: |
||
87 | 2 | $options['form_params'] = $params; |
|
88 | 2 | break; |
|
89 | case RequestMethodEnum::UPLOAD: |
||
90 | 1 | if (array_key_exists('resource', $params) && array_key_exists('name', $params)) { |
|
91 | 1 | $options['headers']['X-File-Name'] = $params['name']; |
|
92 | 1 | $options['multipart'] = [ |
|
93 | [ |
||
94 | 1 | 'contents' => $params['resource'], |
|
95 | 1 | 'name' => $params['name'], |
|
96 | ], |
||
97 | ]; |
||
98 | } |
||
99 | 1 | break; |
|
100 | default: |
||
101 | throw new \InvalidArgumentException(sprintf('Request method "%s" not allowed!', $requestMethod)); |
||
102 | } |
||
103 | |||
104 | 5 | return $options; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $accessToken |
||
109 | * |
||
110 | * @throws \InvalidArgumentException |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | 11 | protected function prepareBaseOptions(string $accessToken): array |
|
123 | } |
||
124 |