mirror of
https://github.com/avency/Gitea.git
synced 2025-10-29 18:52:33 +01:00
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:
112
Classes/Client.php
Normal file
112
Classes/Client.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Classes/Endpoint/EndpointInterface.php
Normal file
14
Classes/Endpoint/EndpointInterface.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Avency\Gitea\Endpoint;
|
||||||
|
|
||||||
|
use Avency\Gitea\Client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint interface
|
||||||
|
*/
|
||||||
|
interface EndpointInterface
|
||||||
|
{
|
||||||
|
}
|
||||||
39
Classes/Endpoint/Repositories.php
Normal file
39
Classes/Endpoint/Repositories.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
36
README.md
36
README.md
@ -20,6 +20,42 @@ Install latest version via composer:
|
|||||||
composer require avency/gitea
|
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
|
## Contributing
|
||||||
|
|
||||||
Please read [CONTRIBUTING.md](https://github.com/avency/gitea//blob/master/CONTRIBUTING.md)
|
Please read [CONTRIBUTING.md](https://github.com/avency/gitea//blob/master/CONTRIBUTING.md)
|
||||||
|
|||||||
Reference in New Issue
Block a user