From 5aaff48c99e5362010a97f0049447640833dd5cb Mon Sep 17 00:00:00 2001 From: Michael Gerdemann Date: Fri, 17 Jan 2020 08:38:49 +0100 Subject: [PATCH] feat: Add basic client and first basic endpoint Adds the client with first basic authentication and the first endpoint `repositories` with first method. --- Classes/Client.php | 112 +++++++++++++++++++++++++ Classes/Endpoint/EndpointInterface.php | 14 ++++ Classes/Endpoint/Repositories.php | 39 +++++++++ README.md | 36 ++++++++ 4 files changed, 201 insertions(+) create mode 100644 Classes/Client.php create mode 100644 Classes/Endpoint/EndpointInterface.php create mode 100644 Classes/Endpoint/Repositories.php diff --git a/Classes/Client.php b/Classes/Client.php new file mode 100644 index 0000000..c0d170c --- /dev/null +++ b/Classes/Client.php @@ -0,0 +1,112 @@ +config = [ + 'base_uri' => $baseUri + ]; + + $this->auth($authentication); + + $this->httpClient = new \GuzzleHttp\Client($this->config); + } + + /** + * @param string $api + * @return EndpointInterface + * @throws Exception + */ + public function api(string $api): EndpointInterface + { + switch ($api) { + case 'repositories': + return new Repositories($this); + } + + throw new Exception('Endpoint not found', 1579246217); + } + + /** + * @param string $uri + * @param string $method + * @param array $options + * @return ResponseInterface + */ + public function request(string $uri = '', string $method = 'GET', array $options = []): ResponseInterface + { + return $this->httpClient->request($method, $uri, $options); + } + + /** + * @param array $authentication + * @throws Exception + */ + protected function auth(array $authentication) + { + if (empty($authentication['type'])) { + throw new Exception('Please add an authentication type.', 1579244392); + } + + switch ($authentication['type']) { + case self::AUTH_ACCESS_TOKEN: + if (empty($authentication['auth'])) { + throw new Exception('Please add the access token.', 1579245994); + } + + $this->config['query']['access_token'] = $authentication['auth']; + break; + + case self::AUTH_BASIC_AUTH: + if (empty($authentication['auth']['username'])) { + throw new Exception('Please add the username.', 1579246033); + } + if (empty($authentication['auth']['password'])) { + throw new Exception('Please add the password.', 1579246035); + } + + $this->config['auth'] = [$authentication['auth']['username'], $authentication['auth']['password']]; + break; + + case self::AUTH_TOKEN: + if (empty($authentication['auth'])) { + throw new Exception('Please add the token.', 1579246003); + } + + $this->config['query']['token'] = $authentication['auth']; + break; + } + } +} diff --git a/Classes/Endpoint/EndpointInterface.php b/Classes/Endpoint/EndpointInterface.php new file mode 100644 index 0000000..28608de --- /dev/null +++ b/Classes/Endpoint/EndpointInterface.php @@ -0,0 +1,14 @@ +client = $client; + } + + /** + * @param $owner + * @param $repositoryName + * @return array + */ + public function get($owner, $repositoryName): array + { + $response = $this->client->request(self::BASE_URI . '/' . $owner . '/' . $repositoryName); + return \GuzzleHttp\json_decode($response->getBody(), true); + } +} diff --git a/README.md b/README.md index 79455e5..8cbde2f 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,42 @@ Install latest version via composer: composer require avency/gitea ``` +### Basic usage + +``` +// This file is generated by Composer +require_once __DIR__ . '/vendor/autoload.php'; + +// - - - - - + +// Create client and authenticate +$giteaClient = new Avency\Gitea\Client( + 'https://gitea.yourdomain.com', + [ + 'type' => Avency\Gitea::AUTH_TOKEN, + 'auth' => 'your-auth-token' + ] +); + +or + +$giteaClient = new Avency\Gitea\Client( + 'https://gitea.yourdomain.com', + [ + 'type' => Avency\Gitea::AUTH_BASIC_AUTH, + 'auth' => [ + 'username' => 'your-username', + 'password' => 'your-password', + ] + ] +); + +// - - - - - + +// Get a single repository +$repository = $giteaClient->api('repositories')->get('owner', 'repoName'); +``` + ## Contributing Please read [CONTRIBUTING.md](https://github.com/avency/gitea//blob/master/CONTRIBUTING.md)