diff --git a/Classes/Client.php b/Classes/Client.php index c0d170c..6a710f7 100644 --- a/Classes/Client.php +++ b/Classes/Client.php @@ -5,12 +5,16 @@ declare(strict_types=1); namespace Avency\Gitea; use Avency\Gitea\Endpoint\EndpointInterface; +use Avency\Gitea\Endpoint\Miscellaneous; use Avency\Gitea\Endpoint\Repositories; use Exception; use Psr\Http\Message\ResponseInterface; /** * Gitea Client + * + * @method Repositories repositories() + * @method Miscellaneous miscellaneous() */ class Client { @@ -45,18 +49,20 @@ class Client } /** - * @param string $api + * @param $method + * @param $args * @return EndpointInterface * @throws Exception */ - public function api(string $api): EndpointInterface + public function __call($method, $args) { - switch ($api) { - case 'repositories': - return new Repositories($this); + $interfaceName = EndpointInterface::class; + $endpointClassName = str_replace('\\EndpointInterface', '\\' . ucfirst($method), $interfaceName); + if (class_exists($endpointClassName)) { + return new $endpointClassName($this); } - throw new Exception('Endpoint not found', 1579246217); + throw new Exception('Endpoint "' . ucfirst($method) . '" not found!', 1579274712); } /** diff --git a/Classes/Endpoint/Miscellaneous.php b/Classes/Endpoint/Miscellaneous.php new file mode 100644 index 0000000..c4c0baf --- /dev/null +++ b/Classes/Endpoint/Miscellaneous.php @@ -0,0 +1,37 @@ +client = $client; + } + + /** + * @return string + */ + public function version(): string + { + $response = $this->client->request(self::BASE_URI . '/version'); + return \GuzzleHttp\json_decode($response->getBody(), true)['version']; + } +} diff --git a/README.md b/README.md index a2580ba..37fca7a 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,10 @@ $giteaClient = new Avency\Gitea\Client( // - - - - - // Get a single repository -$repository = $giteaClient->api('repositories')->get('owner', 'repoName'); +$repository = $giteaClient->repositories()->get('owner', 'repoName'); + +// Get version +$repository = $giteaClient->miscellaneous()->version(); ``` ## Versioning @@ -95,7 +98,7 @@ Status | Method | Endpoint ❌ | POST | /markdown ❌ | POST | /markdown/raw ❌ | GET | /signing-key.gpg -❌ | GET | /version +✅ | GET | /version #### Organization