|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ArgoCD\Api; |
| 4 | + |
| 5 | +use ArgoCD\Client; |
| 6 | +use ArgoCD\HttpClient\Message\ResponseMediator; |
| 7 | +use Psr\Http\Message\ResponseInterface; |
| 8 | + |
| 9 | +abstract class AbstractApi |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The client instance. |
| 13 | + * |
| 14 | + * @var Client |
| 15 | + */ |
| 16 | + protected $client; // Changed visibility to protected to allow access in child classes if needed |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new API instance. |
| 20 | + * |
| 21 | + * @param Client $client |
| 22 | + * |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function __construct(Client $client) |
| 26 | + { |
| 27 | + $this->client = $client; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the client instance. |
| 32 | + * |
| 33 | + * @return Client |
| 34 | + */ |
| 35 | + protected function getClient(): Client |
| 36 | + { |
| 37 | + return $this->client; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return $this |
| 42 | + */ |
| 43 | + public function configure() |
| 44 | + { |
| 45 | + // Kept as a no-op or simple method returning $this |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Send a GET request with query parameters. |
| 51 | + * |
| 52 | + * @param string $path Request path. |
| 53 | + * @param array $parameters GET parameters. |
| 54 | + * @param array $requestHeaders Request Headers. |
| 55 | + * |
| 56 | + * @return array|string |
| 57 | + */ |
| 58 | + protected function get(string $path, array $parameters = [], array $requestHeaders = []) |
| 59 | + { |
| 60 | + // Removed $perPage logic |
| 61 | + // Removed 'ref' parameter logic as it's GitHub specific |
| 62 | + |
| 63 | + if (count($parameters) > 0) { |
| 64 | + $path .= '?'.http_build_query($parameters, '', '&', PHP_QUERY_RFC3986); |
| 65 | + } |
| 66 | + |
| 67 | + $response = $this->client->getHttpClient()->get($path, $requestHeaders); |
| 68 | + |
| 69 | + return ResponseMediator::getContent($response); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Send a HEAD request with query parameters. |
| 74 | + * |
| 75 | + * @param string $path Request path. |
| 76 | + * @param array $parameters HEAD parameters. |
| 77 | + * @param array $requestHeaders Request headers. |
| 78 | + * |
| 79 | + * @return ResponseInterface |
| 80 | + */ |
| 81 | + protected function head(string $path, array $parameters = [], array $requestHeaders = []): ResponseInterface |
| 82 | + { |
| 83 | + // Removed 'ref' parameter logic |
| 84 | + $queryString = ''; |
| 85 | + if (count($parameters) > 0) { |
| 86 | + $queryString = '?'.http_build_query($parameters, '', '&', PHP_QUERY_RFC3986); |
| 87 | + } |
| 88 | + return $this->client->getHttpClient()->head($path.$queryString, $requestHeaders); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Send a POST request with JSON-encoded parameters. |
| 93 | + * |
| 94 | + * @param string $path Request path. |
| 95 | + * @param array $parameters POST parameters to be JSON encoded. |
| 96 | + * @param array $requestHeaders Request headers. |
| 97 | + * |
| 98 | + * @return array|string |
| 99 | + */ |
| 100 | + protected function post(string $path, array $parameters = [], array $requestHeaders = []) |
| 101 | + { |
| 102 | + return $this->postRaw( |
| 103 | + $path, |
| 104 | + $this->createJsonBody($parameters), |
| 105 | + $requestHeaders |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Send a POST request with raw data. |
| 111 | + * |
| 112 | + * @param string $path Request path. |
| 113 | + * @param string|null $body Request body. |
| 114 | + * @param array $requestHeaders Request headers. |
| 115 | + * |
| 116 | + * @return array|string |
| 117 | + */ |
| 118 | + protected function postRaw(string $path, $body, array $requestHeaders = []) |
| 119 | + { |
| 120 | + $response = $this->client->getHttpClient()->post( |
| 121 | + $path, |
| 122 | + $requestHeaders, |
| 123 | + $body |
| 124 | + ); |
| 125 | + |
| 126 | + return ResponseMediator::getContent($response); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Send a PATCH request with JSON-encoded parameters. |
| 131 | + * |
| 132 | + * @param string $path Request path. |
| 133 | + * @param array $parameters POST parameters to be JSON encoded. |
| 134 | + * @param array $requestHeaders Request headers. |
| 135 | + * |
| 136 | + * @return array|string |
| 137 | + */ |
| 138 | + protected function patch(string $path, array $parameters = [], array $requestHeaders = []) |
| 139 | + { |
| 140 | + $response = $this->client->getHttpClient()->patch( |
| 141 | + $path, |
| 142 | + $requestHeaders, |
| 143 | + $this->createJsonBody($parameters) |
| 144 | + ); |
| 145 | + |
| 146 | + return ResponseMediator::getContent($response); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Send a PUT request with JSON-encoded parameters. |
| 151 | + * |
| 152 | + * @param string $path Request path. |
| 153 | + * @param array $parameters POST parameters to be JSON encoded. |
| 154 | + * @param array $requestHeaders Request headers. |
| 155 | + * |
| 156 | + * @return array|string |
| 157 | + */ |
| 158 | + protected function put(string $path, array $parameters = [], array $requestHeaders = []) |
| 159 | + { |
| 160 | + $response = $this->client->getHttpClient()->put( |
| 161 | + $path, |
| 162 | + $requestHeaders, |
| 163 | + $this->createJsonBody($parameters) |
| 164 | + ); |
| 165 | + |
| 166 | + return ResponseMediator::getContent($response); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Send a DELETE request with JSON-encoded parameters. |
| 171 | + * |
| 172 | + * @param string $path Request path. |
| 173 | + * @param array $parameters POST parameters to be JSON encoded. |
| 174 | + * @param array $requestHeaders Request headers. |
| 175 | + * |
| 176 | + * @return array|string |
| 177 | + */ |
| 178 | + protected function delete(string $path, array $parameters = [], array $requestHeaders = []) |
| 179 | + { |
| 180 | + // ArgoCD DELETE requests might not always have a body. |
| 181 | + // If parameters are provided, assume they are for the body. |
| 182 | + // If not, send null as the body. |
| 183 | + $body = null; |
| 184 | + if (count($parameters) > 0) { |
| 185 | + $body = $this->createJsonBody($parameters); |
| 186 | + } |
| 187 | + |
| 188 | + $response = $this->client->getHttpClient()->delete( |
| 189 | + $path, |
| 190 | + $requestHeaders, |
| 191 | + $body |
| 192 | + ); |
| 193 | + |
| 194 | + return ResponseMediator::getContent($response); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Create a JSON encoded version of an array of parameters. |
| 199 | + * |
| 200 | + * @param array $parameters Request parameters |
| 201 | + * |
| 202 | + * @return string|null |
| 203 | + */ |
| 204 | + protected function createJsonBody(array $parameters): ?string |
| 205 | + { |
| 206 | + // Ensure empty array results in null, not "[]" for some ArgoCD endpoints if they expect no body. |
| 207 | + // However, for POST/PUT/PATCH, an empty JSON object "{}" might be valid. |
| 208 | + // The original behavior is to return null for empty arrays, which is generally fine. |
| 209 | + return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0); |
| 210 | + } |
| 211 | +} |
0 commit comments