feat: Add basic client and first basic endpoint

Adds the client with first basic authentication and
the first endpoint `repositories` with first method.
This commit is contained in:
Michael Gerdemann
2020-01-17 08:38:49 +01:00
parent d4bb8a6108
commit 5aaff48c99
4 changed files with 201 additions and 0 deletions

112
Classes/Client.php Normal file
View File

@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea;
use Avency\Gitea\Endpoint\EndpointInterface;
use Avency\Gitea\Endpoint\Repositories;
use Exception;
use Psr\Http\Message\ResponseInterface;
/**
* Gitea Client
*/
class Client
{
const AUTH_ACCESS_TOKEN = 'access_token';
const AUTH_TOKEN = 'token';
const AUTH_BASIC_AUTH = 'basic_auth';
/**
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* @var array
*/
protected $config;
/**
* @param $baseUri
* @param $authentication
* @throws Exception
*/
public function __construct($baseUri, $authentication)
{
$this->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;
}
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint;
use Avency\Gitea\Client;
/**
* Endpoint interface
*/
interface EndpointInterface
{
}

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint;
use Avency\Gitea\Client;
/**
* Repositories endpoint
*/
class Repositories implements EndpointInterface
{
const BASE_URI = 'api/v1/repos';
/**
* @var Client
*/
protected $client;
/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->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);
}
}

View File

@ -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)