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