@@ -56,7 +56,7 @@ |
||
56 | 56 | * @param array $body |
57 | 57 | * @param array $query |
58 | 58 | * @param array $headers |
59 | - * @return resource|string |
|
59 | + * @return string |
|
60 | 60 | * @throws \InvalidArgumentException |
61 | 61 | */ |
62 | 62 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
@@ -26,72 +26,72 @@ |
||
26 | 26 | use OCP\Remote\IInstance; |
27 | 27 | |
28 | 28 | class ApiBase { |
29 | - /** @var IInstance */ |
|
30 | - private $instance; |
|
31 | - /** @var ICredentials */ |
|
32 | - private $credentials; |
|
33 | - /** @var IClientService */ |
|
34 | - private $clientService; |
|
29 | + /** @var IInstance */ |
|
30 | + private $instance; |
|
31 | + /** @var ICredentials */ |
|
32 | + private $credentials; |
|
33 | + /** @var IClientService */ |
|
34 | + private $clientService; |
|
35 | 35 | |
36 | - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
37 | - $this->instance = $instance; |
|
38 | - $this->credentials = $credentials; |
|
39 | - $this->clientService = $clientService; |
|
40 | - } |
|
36 | + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
37 | + $this->instance = $instance; |
|
38 | + $this->credentials = $credentials; |
|
39 | + $this->clientService = $clientService; |
|
40 | + } |
|
41 | 41 | |
42 | - protected function getHttpClient() { |
|
43 | - return $this->clientService->newClient(); |
|
44 | - } |
|
42 | + protected function getHttpClient() { |
|
43 | + return $this->clientService->newClient(); |
|
44 | + } |
|
45 | 45 | |
46 | - protected function addDefaultHeaders(array $headers) { |
|
47 | - return array_merge([ |
|
48 | - 'OCS-APIREQUEST' => 'true', |
|
49 | - 'Accept' => 'application/json' |
|
50 | - ], $headers); |
|
51 | - } |
|
46 | + protected function addDefaultHeaders(array $headers) { |
|
47 | + return array_merge([ |
|
48 | + 'OCS-APIREQUEST' => 'true', |
|
49 | + 'Accept' => 'application/json' |
|
50 | + ], $headers); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $method |
|
55 | - * @param string $url |
|
56 | - * @param array $body |
|
57 | - * @param array $query |
|
58 | - * @param array $headers |
|
59 | - * @return resource|string |
|
60 | - * @throws \InvalidArgumentException |
|
61 | - */ |
|
62 | - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
63 | - $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
64 | - $options = [ |
|
65 | - 'query' => $query, |
|
66 | - 'headers' => $this->addDefaultHeaders($headers), |
|
67 | - 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
68 | - ]; |
|
69 | - if ($body) { |
|
70 | - $options['body'] = $body; |
|
71 | - } |
|
53 | + /** |
|
54 | + * @param string $method |
|
55 | + * @param string $url |
|
56 | + * @param array $body |
|
57 | + * @param array $query |
|
58 | + * @param array $headers |
|
59 | + * @return resource|string |
|
60 | + * @throws \InvalidArgumentException |
|
61 | + */ |
|
62 | + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
63 | + $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
64 | + $options = [ |
|
65 | + 'query' => $query, |
|
66 | + 'headers' => $this->addDefaultHeaders($headers), |
|
67 | + 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
68 | + ]; |
|
69 | + if ($body) { |
|
70 | + $options['body'] = $body; |
|
71 | + } |
|
72 | 72 | |
73 | - $client = $this->getHttpClient(); |
|
73 | + $client = $this->getHttpClient(); |
|
74 | 74 | |
75 | - switch ($method) { |
|
76 | - case 'get': |
|
77 | - $response = $client->get($fullUrl, $options); |
|
78 | - break; |
|
79 | - case 'post': |
|
80 | - $response = $client->post($fullUrl, $options); |
|
81 | - break; |
|
82 | - case 'put': |
|
83 | - $response = $client->put($fullUrl, $options); |
|
84 | - break; |
|
85 | - case 'delete': |
|
86 | - $response = $client->delete($fullUrl, $options); |
|
87 | - break; |
|
88 | - case 'options': |
|
89 | - $response = $client->options($fullUrl, $options); |
|
90 | - break; |
|
91 | - default: |
|
92 | - throw new \InvalidArgumentException('Invalid method ' . $method); |
|
93 | - } |
|
75 | + switch ($method) { |
|
76 | + case 'get': |
|
77 | + $response = $client->get($fullUrl, $options); |
|
78 | + break; |
|
79 | + case 'post': |
|
80 | + $response = $client->post($fullUrl, $options); |
|
81 | + break; |
|
82 | + case 'put': |
|
83 | + $response = $client->put($fullUrl, $options); |
|
84 | + break; |
|
85 | + case 'delete': |
|
86 | + $response = $client->delete($fullUrl, $options); |
|
87 | + break; |
|
88 | + case 'options': |
|
89 | + $response = $client->options($fullUrl, $options); |
|
90 | + break; |
|
91 | + default: |
|
92 | + throw new \InvalidArgumentException('Invalid method ' . $method); |
|
93 | + } |
|
94 | 94 | |
95 | - return $response->getBody(); |
|
96 | - } |
|
95 | + return $response->getBody(); |
|
96 | + } |
|
97 | 97 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @throws \InvalidArgumentException |
61 | 61 | */ |
62 | 62 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
63 | - $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
63 | + $fullUrl = trim($this->instance->getFullUrl(), '/').'/'.$url; |
|
64 | 64 | $options = [ |
65 | 65 | 'query' => $query, |
66 | 66 | 'headers' => $this->addDefaultHeaders($headers), |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | $response = $client->options($fullUrl, $options); |
90 | 90 | break; |
91 | 91 | default: |
92 | - throw new \InvalidArgumentException('Invalid method ' . $method); |
|
92 | + throw new \InvalidArgumentException('Invalid method '.$method); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | return $response->getBody(); |
@@ -134,7 +134,7 @@ |
||
134 | 134 | |
135 | 135 | /** |
136 | 136 | * @param string $url |
137 | - * @return bool|string |
|
137 | + * @return string |
|
138 | 138 | */ |
139 | 139 | private function downloadStatus($url) { |
140 | 140 | try { |
@@ -30,118 +30,118 @@ |
||
30 | 30 | * Provides some basic info about a remote Nextcloud instance |
31 | 31 | */ |
32 | 32 | class Instance implements IInstance { |
33 | - /** @var string */ |
|
34 | - private $url; |
|
33 | + /** @var string */ |
|
34 | + private $url; |
|
35 | 35 | |
36 | - /** @var ICache */ |
|
37 | - private $cache; |
|
36 | + /** @var ICache */ |
|
37 | + private $cache; |
|
38 | 38 | |
39 | - /** @var IClientService */ |
|
40 | - private $clientService; |
|
39 | + /** @var IClientService */ |
|
40 | + private $clientService; |
|
41 | 41 | |
42 | - /** @var array|null */ |
|
43 | - private $status; |
|
42 | + /** @var array|null */ |
|
43 | + private $status; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $url |
|
47 | - * @param ICache $cache |
|
48 | - * @param IClientService $clientService |
|
49 | - */ |
|
50 | - public function __construct($url, ICache $cache, IClientService $clientService) { |
|
51 | - $url = str_replace('https://', '', $url); |
|
52 | - $this->url = str_replace('http://', '', $url); |
|
53 | - $this->cache = $cache; |
|
54 | - $this->clientService = $clientService; |
|
55 | - } |
|
45 | + /** |
|
46 | + * @param string $url |
|
47 | + * @param ICache $cache |
|
48 | + * @param IClientService $clientService |
|
49 | + */ |
|
50 | + public function __construct($url, ICache $cache, IClientService $clientService) { |
|
51 | + $url = str_replace('https://', '', $url); |
|
52 | + $this->url = str_replace('http://', '', $url); |
|
53 | + $this->cache = $cache; |
|
54 | + $this->clientService = $clientService; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return string The url of the remote server without protocol |
|
59 | - */ |
|
60 | - public function getUrl() { |
|
61 | - return $this->url; |
|
62 | - } |
|
57 | + /** |
|
58 | + * @return string The url of the remote server without protocol |
|
59 | + */ |
|
60 | + public function getUrl() { |
|
61 | + return $this->url; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string The of of the remote server with protocol |
|
66 | - */ |
|
67 | - public function getFullUrl() { |
|
68 | - return $this->getProtocol() . '://' . $this->getUrl(); |
|
69 | - } |
|
64 | + /** |
|
65 | + * @return string The of of the remote server with protocol |
|
66 | + */ |
|
67 | + public function getFullUrl() { |
|
68 | + return $this->getProtocol() . '://' . $this->getUrl(); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return string The full version string in '13.1.2.3' format |
|
73 | - */ |
|
74 | - public function getVersion() { |
|
75 | - $status = $this->getStatus(); |
|
76 | - return $status['version']; |
|
77 | - } |
|
71 | + /** |
|
72 | + * @return string The full version string in '13.1.2.3' format |
|
73 | + */ |
|
74 | + public function getVersion() { |
|
75 | + $status = $this->getStatus(); |
|
76 | + return $status['version']; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return string 'http' or 'https' |
|
81 | - */ |
|
82 | - public function getProtocol() { |
|
83 | - $status = $this->getStatus(); |
|
84 | - return $status['protocol']; |
|
85 | - } |
|
79 | + /** |
|
80 | + * @return string 'http' or 'https' |
|
81 | + */ |
|
82 | + public function getProtocol() { |
|
83 | + $status = $this->getStatus(); |
|
84 | + return $status['protocol']; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Check that the remote server is installed and not in maintenance mode |
|
89 | - * |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function isActive() { |
|
93 | - $status = $this->getStatus(); |
|
94 | - return $status['installed'] && !$status['maintenance']; |
|
95 | - } |
|
87 | + /** |
|
88 | + * Check that the remote server is installed and not in maintenance mode |
|
89 | + * |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function isActive() { |
|
93 | + $status = $this->getStatus(); |
|
94 | + return $status['installed'] && !$status['maintenance']; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return array |
|
99 | - * @throws NotFoundException |
|
100 | - * @throws \Exception |
|
101 | - */ |
|
102 | - private function getStatus() { |
|
103 | - if ($this->status) { |
|
104 | - return $this->status; |
|
105 | - } |
|
106 | - $key = 'remote/' . $this->url . '/status'; |
|
107 | - $httpsKey = 'remote/' . $this->url . '/https'; |
|
108 | - $status = $this->cache->get($key); |
|
109 | - if (!$status) { |
|
110 | - $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
111 | - $protocol = 'https'; |
|
112 | - if (!$response) { |
|
113 | - if ($status = $this->cache->get($httpsKey)) { |
|
114 | - throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); |
|
115 | - } |
|
116 | - $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
117 | - $protocol = 'http'; |
|
118 | - } else { |
|
119 | - $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); |
|
120 | - } |
|
121 | - $status = json_decode($response, true); |
|
122 | - if ($status) { |
|
123 | - $status['protocol'] = $protocol; |
|
124 | - } |
|
125 | - if ($status) { |
|
126 | - $this->cache->set($key, $status, 5 * 60); |
|
127 | - $this->status = $status; |
|
128 | - } else { |
|
129 | - throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
130 | - } |
|
131 | - } |
|
132 | - return $status; |
|
133 | - } |
|
97 | + /** |
|
98 | + * @return array |
|
99 | + * @throws NotFoundException |
|
100 | + * @throws \Exception |
|
101 | + */ |
|
102 | + private function getStatus() { |
|
103 | + if ($this->status) { |
|
104 | + return $this->status; |
|
105 | + } |
|
106 | + $key = 'remote/' . $this->url . '/status'; |
|
107 | + $httpsKey = 'remote/' . $this->url . '/https'; |
|
108 | + $status = $this->cache->get($key); |
|
109 | + if (!$status) { |
|
110 | + $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
111 | + $protocol = 'https'; |
|
112 | + if (!$response) { |
|
113 | + if ($status = $this->cache->get($httpsKey)) { |
|
114 | + throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); |
|
115 | + } |
|
116 | + $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
117 | + $protocol = 'http'; |
|
118 | + } else { |
|
119 | + $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); |
|
120 | + } |
|
121 | + $status = json_decode($response, true); |
|
122 | + if ($status) { |
|
123 | + $status['protocol'] = $protocol; |
|
124 | + } |
|
125 | + if ($status) { |
|
126 | + $this->cache->set($key, $status, 5 * 60); |
|
127 | + $this->status = $status; |
|
128 | + } else { |
|
129 | + throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
130 | + } |
|
131 | + } |
|
132 | + return $status; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @param string $url |
|
137 | - * @return bool|string |
|
138 | - */ |
|
139 | - private function downloadStatus($url) { |
|
140 | - try { |
|
141 | - $request = $this->clientService->newClient()->get($url); |
|
142 | - return $request->getBody(); |
|
143 | - } catch (\Exception $e) { |
|
144 | - return false; |
|
145 | - } |
|
146 | - } |
|
135 | + /** |
|
136 | + * @param string $url |
|
137 | + * @return bool|string |
|
138 | + */ |
|
139 | + private function downloadStatus($url) { |
|
140 | + try { |
|
141 | + $request = $this->clientService->newClient()->get($url); |
|
142 | + return $request->getBody(); |
|
143 | + } catch (\Exception $e) { |
|
144 | + return false; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | * @return string The of of the remote server with protocol |
66 | 66 | */ |
67 | 67 | public function getFullUrl() { |
68 | - return $this->getProtocol() . '://' . $this->getUrl(); |
|
68 | + return $this->getProtocol().'://'.$this->getUrl(); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -103,17 +103,17 @@ discard block |
||
103 | 103 | if ($this->status) { |
104 | 104 | return $this->status; |
105 | 105 | } |
106 | - $key = 'remote/' . $this->url . '/status'; |
|
107 | - $httpsKey = 'remote/' . $this->url . '/https'; |
|
106 | + $key = 'remote/'.$this->url.'/status'; |
|
107 | + $httpsKey = 'remote/'.$this->url.'/https'; |
|
108 | 108 | $status = $this->cache->get($key); |
109 | 109 | if (!$status) { |
110 | - $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
110 | + $response = $this->downloadStatus('https://'.$this->getUrl().'/status.php'); |
|
111 | 111 | $protocol = 'https'; |
112 | 112 | if (!$response) { |
113 | 113 | if ($status = $this->cache->get($httpsKey)) { |
114 | - throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); |
|
114 | + throw new \Exception('refusing to connect to remote instance('.$this->url.') over http that was previously accessible over https'); |
|
115 | 115 | } |
116 | - $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
116 | + $response = $this->downloadStatus('http://'.$this->getUrl().'/status.php'); |
|
117 | 117 | $protocol = 'http'; |
118 | 118 | } else { |
119 | 119 | $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $this->cache->set($key, $status, 5 * 60); |
127 | 127 | $this->status = $status; |
128 | 128 | } else { |
129 | - throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
129 | + throw new NotFoundException('Remote server not found at address '.$this->url); |
|
130 | 130 | } |
131 | 131 | } |
132 | 132 | return $status; |
@@ -25,11 +25,11 @@ |
||
25 | 25 | * @since 13.0.0 |
26 | 26 | */ |
27 | 27 | interface IInstanceFactory { |
28 | - /** |
|
29 | - * @param string $url |
|
30 | - * @return IInstance |
|
31 | - * |
|
32 | - * @since 13.0.0 |
|
33 | - */ |
|
34 | - public function getInstance($url); |
|
28 | + /** |
|
29 | + * @param string $url |
|
30 | + * @return IInstance |
|
31 | + * |
|
32 | + * @since 13.0.0 |
|
33 | + */ |
|
34 | + public function getInstance($url); |
|
35 | 35 | } |
@@ -28,14 +28,14 @@ |
||
28 | 28 | use OCP\Remote\IInstance; |
29 | 29 | |
30 | 30 | class ApiFactory implements IApiFactory { |
31 | - /** @var IClientService */ |
|
32 | - private $clientService; |
|
31 | + /** @var IClientService */ |
|
32 | + private $clientService; |
|
33 | 33 | |
34 | - public function __construct(IClientService $clientService) { |
|
35 | - $this->clientService = $clientService; |
|
36 | - } |
|
34 | + public function __construct(IClientService $clientService) { |
|
35 | + $this->clientService = $clientService; |
|
36 | + } |
|
37 | 37 | |
38 | - public function getApiCollection(IInstance $instance, ICredentials $credentials) { |
|
39 | - return new ApiCollection($instance, $credentials, $this->clientService); |
|
40 | - } |
|
38 | + public function getApiCollection(IInstance $instance, ICredentials $credentials) { |
|
39 | + return new ApiCollection($instance, $credentials, $this->clientService); |
|
40 | + } |
|
41 | 41 | } |
@@ -28,24 +28,24 @@ |
||
28 | 28 | use OCP\Remote\IInstance; |
29 | 29 | |
30 | 30 | class ApiCollection implements IApiCollection { |
31 | - /** @var IInstance */ |
|
32 | - private $instance; |
|
33 | - /** @var ICredentials */ |
|
34 | - private $credentials; |
|
35 | - /** @var IClientService */ |
|
36 | - private $clientService; |
|
31 | + /** @var IInstance */ |
|
32 | + private $instance; |
|
33 | + /** @var ICredentials */ |
|
34 | + private $credentials; |
|
35 | + /** @var IClientService */ |
|
36 | + private $clientService; |
|
37 | 37 | |
38 | - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
39 | - $this->instance = $instance; |
|
40 | - $this->credentials = $credentials; |
|
41 | - $this->clientService = $clientService; |
|
42 | - } |
|
38 | + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
39 | + $this->instance = $instance; |
|
40 | + $this->credentials = $credentials; |
|
41 | + $this->clientService = $clientService; |
|
42 | + } |
|
43 | 43 | |
44 | - public function getCapabilitiesApi() { |
|
45 | - return new OCS($this->instance, $this->credentials, $this->clientService); |
|
46 | - } |
|
44 | + public function getCapabilitiesApi() { |
|
45 | + return new OCS($this->instance, $this->credentials, $this->clientService); |
|
46 | + } |
|
47 | 47 | |
48 | - public function getUserApi() { |
|
49 | - return new OCS($this->instance, $this->credentials, $this->clientService); |
|
50 | - } |
|
48 | + public function getUserApi() { |
|
49 | + return new OCS($this->instance, $this->credentials, $this->clientService); |
|
50 | + } |
|
51 | 51 | } |
@@ -27,17 +27,17 @@ |
||
27 | 27 | use OCP\Remote\IInstanceFactory; |
28 | 28 | |
29 | 29 | class InstanceFactory implements IInstanceFactory { |
30 | - /** @var ICache */ |
|
31 | - private $cache; |
|
32 | - /** @var IClientService */ |
|
33 | - private $clientService; |
|
30 | + /** @var ICache */ |
|
31 | + private $cache; |
|
32 | + /** @var IClientService */ |
|
33 | + private $clientService; |
|
34 | 34 | |
35 | - public function __construct(ICache $cache, IClientService $clientService) { |
|
36 | - $this->cache = $cache; |
|
37 | - $this->clientService = $clientService; |
|
38 | - } |
|
35 | + public function __construct(ICache $cache, IClientService $clientService) { |
|
36 | + $this->cache = $cache; |
|
37 | + $this->clientService = $clientService; |
|
38 | + } |
|
39 | 39 | |
40 | - public function getInstance($url) { |
|
41 | - return new Instance($url, $this->cache, $this->clientService); |
|
42 | - } |
|
40 | + public function getInstance($url) { |
|
41 | + return new Instance($url, $this->cache, $this->clientService); |
|
42 | + } |
|
43 | 43 | } |
@@ -30,70 +30,70 @@ |
||
30 | 30 | use OCP\Remote\Api\IUserApi; |
31 | 31 | |
32 | 32 | class OCS extends ApiBase implements ICapabilitiesApi, IUserApi { |
33 | - /** |
|
34 | - * @param string $method |
|
35 | - * @param string $url |
|
36 | - * @param array $body |
|
37 | - * @param array $query |
|
38 | - * @param array $headers |
|
39 | - * @return array |
|
40 | - * @throws ForbiddenException |
|
41 | - * @throws NotFoundException |
|
42 | - * @throws \Exception |
|
43 | - */ |
|
44 | - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
45 | - try { |
|
46 | - $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
47 | - } catch (ClientException $e) { |
|
48 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
49 | - throw new NotFoundException(); |
|
50 | - } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { |
|
51 | - throw new ForbiddenException(); |
|
52 | - } else { |
|
53 | - throw $e; |
|
54 | - } |
|
55 | - } |
|
56 | - if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { |
|
57 | - throw new \Exception('Invalid ocs response'); |
|
58 | - } |
|
59 | - if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) { |
|
60 | - throw new ForbiddenException(); |
|
61 | - } |
|
62 | - if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) { |
|
63 | - throw new NotFoundException(); |
|
64 | - } |
|
65 | - if ($response['ocs']['meta']['status'] !== 'ok') { |
|
66 | - throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
67 | - } |
|
33 | + /** |
|
34 | + * @param string $method |
|
35 | + * @param string $url |
|
36 | + * @param array $body |
|
37 | + * @param array $query |
|
38 | + * @param array $headers |
|
39 | + * @return array |
|
40 | + * @throws ForbiddenException |
|
41 | + * @throws NotFoundException |
|
42 | + * @throws \Exception |
|
43 | + */ |
|
44 | + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
45 | + try { |
|
46 | + $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
47 | + } catch (ClientException $e) { |
|
48 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
49 | + throw new NotFoundException(); |
|
50 | + } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { |
|
51 | + throw new ForbiddenException(); |
|
52 | + } else { |
|
53 | + throw $e; |
|
54 | + } |
|
55 | + } |
|
56 | + if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { |
|
57 | + throw new \Exception('Invalid ocs response'); |
|
58 | + } |
|
59 | + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) { |
|
60 | + throw new ForbiddenException(); |
|
61 | + } |
|
62 | + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) { |
|
63 | + throw new NotFoundException(); |
|
64 | + } |
|
65 | + if ($response['ocs']['meta']['status'] !== 'ok') { |
|
66 | + throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
67 | + } |
|
68 | 68 | |
69 | - return $response['ocs']['data']; |
|
70 | - } |
|
69 | + return $response['ocs']['data']; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param array $data |
|
74 | - * @param string $type |
|
75 | - * @param string[] $keys |
|
76 | - * @throws \Exception |
|
77 | - */ |
|
78 | - private function checkResponseArray(array $data, $type, array $keys) { |
|
79 | - foreach ($keys as $key) { |
|
80 | - if (!array_key_exists($key, $data)) { |
|
81 | - throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); |
|
82 | - } |
|
83 | - } |
|
84 | - } |
|
72 | + /** |
|
73 | + * @param array $data |
|
74 | + * @param string $type |
|
75 | + * @param string[] $keys |
|
76 | + * @throws \Exception |
|
77 | + */ |
|
78 | + private function checkResponseArray(array $data, $type, array $keys) { |
|
79 | + foreach ($keys as $key) { |
|
80 | + if (!array_key_exists($key, $data)) { |
|
81 | + throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); |
|
82 | + } |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - public function getUser($userId) { |
|
87 | - $result = $this->request('get', 'cloud/users/' . $userId); |
|
88 | - $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); |
|
89 | - return new User($result); |
|
90 | - } |
|
86 | + public function getUser($userId) { |
|
87 | + $result = $this->request('get', 'cloud/users/' . $userId); |
|
88 | + $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); |
|
89 | + return new User($result); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return array The capabilities in the form of [$appId => [$capability => $value]] |
|
94 | - */ |
|
95 | - public function getCapabilities() { |
|
96 | - $result = $this->request('get', 'cloud/capabilities'); |
|
97 | - return $result['capabilities']; |
|
98 | - } |
|
92 | + /** |
|
93 | + * @return array The capabilities in the form of [$appId => [$capability => $value]] |
|
94 | + */ |
|
95 | + public function getCapabilities() { |
|
96 | + $result = $this->request('get', 'cloud/capabilities'); |
|
97 | + return $result['capabilities']; |
|
98 | + } |
|
99 | 99 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
45 | 45 | try { |
46 | - $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
46 | + $response = json_decode(parent::request($method, 'ocs/v2.php/'.$url, $body, $query, $headers), true); |
|
47 | 47 | } catch (ClientException $e) { |
48 | 48 | if ($e->getResponse()->getStatusCode() === 404) { |
49 | 49 | throw new NotFoundException(); |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | throw new NotFoundException(); |
64 | 64 | } |
65 | 65 | if ($response['ocs']['meta']['status'] !== 'ok') { |
66 | - throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
66 | + throw new \Exception('Unknown ocs error '.$response['ocs']['meta']['message']); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | return $response['ocs']['data']; |
@@ -78,13 +78,13 @@ discard block |
||
78 | 78 | private function checkResponseArray(array $data, $type, array $keys) { |
79 | 79 | foreach ($keys as $key) { |
80 | 80 | if (!array_key_exists($key, $data)) { |
81 | - throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); |
|
81 | + throw new \Exception('Invalid '.$type.' response, expected field '.$key.' not found'); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | } |
85 | 85 | |
86 | 86 | public function getUser($userId) { |
87 | - $result = $this->request('get', 'cloud/users/' . $userId); |
|
87 | + $result = $this->request('get', 'cloud/users/'.$userId); |
|
88 | 88 | $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); |
89 | 89 | return new User($result); |
90 | 90 | } |
@@ -25,114 +25,114 @@ |
||
25 | 25 | use OCP\Remote\IUser; |
26 | 26 | |
27 | 27 | class User implements IUser { |
28 | - const EXPECTED_KEYS = [ |
|
29 | - 'id', |
|
30 | - 'email', |
|
31 | - 'displayname', |
|
32 | - 'phone', |
|
33 | - 'address', |
|
34 | - 'website', |
|
35 | - 'groups', |
|
36 | - 'language', |
|
37 | - 'quota' |
|
38 | - ]; |
|
39 | - |
|
40 | - /** @var array */ |
|
41 | - private $data; |
|
42 | - |
|
43 | - public function __construct(array $data) { |
|
44 | - $this->data = $data; |
|
45 | - } |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function getUserId() { |
|
52 | - return $this->data['id']; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function getEmail() { |
|
59 | - return $this->data['email']; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function getDisplayName() { |
|
66 | - return $this->data['displayname']; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function getPhone() { |
|
73 | - return $this->data['phone']; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function getAddress() { |
|
80 | - return $this->data['address']; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function getWebsite() { |
|
87 | - return $this->data['website']; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @return string |
|
92 | - */ |
|
93 | - public function getTwitter() { |
|
94 | - return isset($this->data['twitter']) ? $this->data['twitter'] : ''; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return string[] |
|
99 | - */ |
|
100 | - public function getGroups() { |
|
101 | - return $this->data['groups']; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function getLanguage() { |
|
108 | - return $this->data['language']; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @return int |
|
113 | - */ |
|
114 | - public function getUsedSpace() { |
|
115 | - return $this->data['quota']['used']; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function getFreeSpace() { |
|
122 | - return $this->data['quota']['free']; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return int |
|
127 | - */ |
|
128 | - public function getTotalSpace() { |
|
129 | - return $this->data['quota']['total']; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @return int |
|
134 | - */ |
|
135 | - public function getQuota() { |
|
136 | - return $this->data['quota']['quota']; |
|
137 | - } |
|
28 | + const EXPECTED_KEYS = [ |
|
29 | + 'id', |
|
30 | + 'email', |
|
31 | + 'displayname', |
|
32 | + 'phone', |
|
33 | + 'address', |
|
34 | + 'website', |
|
35 | + 'groups', |
|
36 | + 'language', |
|
37 | + 'quota' |
|
38 | + ]; |
|
39 | + |
|
40 | + /** @var array */ |
|
41 | + private $data; |
|
42 | + |
|
43 | + public function __construct(array $data) { |
|
44 | + $this->data = $data; |
|
45 | + } |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function getUserId() { |
|
52 | + return $this->data['id']; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function getEmail() { |
|
59 | + return $this->data['email']; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function getDisplayName() { |
|
66 | + return $this->data['displayname']; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function getPhone() { |
|
73 | + return $this->data['phone']; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function getAddress() { |
|
80 | + return $this->data['address']; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function getWebsite() { |
|
87 | + return $this->data['website']; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @return string |
|
92 | + */ |
|
93 | + public function getTwitter() { |
|
94 | + return isset($this->data['twitter']) ? $this->data['twitter'] : ''; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return string[] |
|
99 | + */ |
|
100 | + public function getGroups() { |
|
101 | + return $this->data['groups']; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function getLanguage() { |
|
108 | + return $this->data['language']; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @return int |
|
113 | + */ |
|
114 | + public function getUsedSpace() { |
|
115 | + return $this->data['quota']['used']; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function getFreeSpace() { |
|
122 | + return $this->data['quota']['free']; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return int |
|
127 | + */ |
|
128 | + public function getTotalSpace() { |
|
129 | + return $this->data['quota']['total']; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @return int |
|
134 | + */ |
|
135 | + public function getQuota() { |
|
136 | + return $this->data['quota']['quota']; |
|
137 | + } |
|
138 | 138 | } |