feat: Add user endpoint

This commit is contained in:
Michael Gerdemann
2020-01-19 15:53:23 +01:00
parent 6e6b0b6a29
commit 5c2450116b
7 changed files with 421 additions and 27 deletions

View File

@ -9,6 +9,7 @@ use Avency\Gitea\Endpoint\EndpointInterface;
use Avency\Gitea\Endpoint\Miscellaneous;
use Avency\Gitea\Endpoint\Organizations;
use Avency\Gitea\Endpoint\Repositories;
use Avency\Gitea\Endpoint\User;
use Exception;
use Psr\Http\Message\ResponseInterface;
@ -19,6 +20,7 @@ use Psr\Http\Message\ResponseInterface;
* @method Miscellaneous miscellaneous()
* @method Organizations organizations()
* @method Repositories repositories()
* @method User user()
*/
class Client
{

37
Classes/Endpoint/User.php Normal file
View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint;
use Avency\Gitea\Client;
use Avency\Gitea\Endpoint\User\RepositoriesTrait;
use Avency\Gitea\Endpoint\User\UserTrait;
use Avency\Gitea\Endpoint\User\FollowersTrait;
use Avency\Gitea\Endpoint\User\KeysTrait;
/**
* User endpoint
*/
class User extends AbstractEndpoint implements EndpointInterface
{
use FollowersTrait;
use KeysTrait;
use RepositoriesTrait;
use UserTrait;
const BASE_URI = '/user';
/**
* @var Client
*/
protected $client;
/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint\User;
use Avency\Gitea\Client;
/**
* Users Followers Trait
*/
trait FollowersTrait
{
/**
* @return array
*/
public function getFollowers(): array
{
$response = $this->client->request(self::BASE_URI . '/followers');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @return array
*/
public function getFollowing(): array
{
$response = $this->client->request(self::BASE_URI . '/following');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param string $username
* @return bool
*/
public function checkFollowing(string $username): bool
{
$this->client->request(self::BASE_URI . '/following/' . $username);
return true;
}
/**
* @param string $username
* @return bool
*/
public function addFollowing(string $username): bool
{
$this->client->request(self::BASE_URI . '/following/' . $username, 'PUT');
return true;
}
/**
* @param string $username
* @return bool
*/
public function deleteFollowing(string $username): bool
{
$this->client->request(self::BASE_URI . '/following/' . $username, 'DELETE');
return true;
}
}

View File

@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint\User;
use Avency\Gitea\Client;
/**
* Users Keys Trait
*/
trait KeysTrait
{
/**
* @return array
*/
public function getGPGKeys(): array
{
$response = $this->client->request(self::BASE_URI . '/gpg_keys');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param string $key
* @return array
*/
public function addGPGKey(string $key): array
{
$options['json'] = [
'armored_public_key' => $key
];
$response = $this->client->request(self::BASE_URI . '/gpg_keys', 'POST', $options);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param int $id
* @return array
*/
public function getGPGKey(int $id): array
{
$response = $this->client->request(self::BASE_URI . '/gpg_keys/' . $id);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param int $id
* @return bool
*/
public function deleteGPGKey(int $id): bool
{
$this->client->request(self::BASE_URI . '/gpg_keys/' . $id, 'DELETE');
return true;
}
/**
* @return array
*/
public function getKeys(): array
{
$response = $this->client->request(self::BASE_URI . '/keys');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param string $title
* @param string $key
* @param bool|null $readOnly
* @return array
*/
public function addKey(string $title, string $key, bool $readOnly = null): array
{
$options['json'] = [
'key' => $key,
'read_only' => $readOnly,
'title' => $title,
];
$options['json'] = $this->removeNullValues($options['json']);
$response = $this->client->request(self::BASE_URI . '/keys', 'POST', $options);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param int $id
* @return array
*/
public function getKey(int $id): array
{
$response = $this->client->request(self::BASE_URI . '/keys/' . $id);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param int $id
* @return bool
*/
public function deleteKey(int $id): bool
{
$this->client->request(self::BASE_URI . '/keys/' . $id, 'DELETE');
return true;
}
}

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint\User;
use Avency\Gitea\Client;
/**
* Users Repositories Trait
*/
trait RepositoriesTrait
{
/**
* @return array
*/
public function getRepositories(): array
{
$response = $this->client->request(self::BASE_URI . '/repos');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @return array
*/
public function getStarredRepositories(): array
{
$response = $this->client->request(self::BASE_URI . '/starred');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param string $owner
* @param string $repositoryName
* @return bool
*/
public function checkStarredRepository(string $owner, string $repositoryName): bool
{
$this->client->request(self::BASE_URI . '/starred/' . $owner . '/' . $repositoryName);
return true;
}
/**
* @param string $owner
* @param string $repositoryName
* @return bool
*/
public function addStarredRepository(string $owner, string $repositoryName): bool
{
$this->client->request(self::BASE_URI . '/starred/' . $owner . '/' . $repositoryName, 'PUT');
return true;
}
/**
* @param string $owner
* @param string $repositoryName
* @return bool
*/
public function deleteStarredRepository(string $owner, string $repositoryName): bool
{
$this->client->request(self::BASE_URI . '/starred/' . $owner . '/' . $repositoryName, 'DELETE');
return true;
}
/**
* @return array
*/
public function getSubscriptions(): array
{
$response = $this->client->request(self::BASE_URI . '/subscriptions');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
}

View File

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Avency\Gitea\Endpoint\User;
use Avency\Gitea\Client;
/**
* Users User Trait
*/
trait UserTrait
{
/**
* @return array
*/
public function get(): array
{
$response = $this->client->request(self::BASE_URI);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @return array
*/
public function getEmails(): array
{
$response = $this->client->request(self::BASE_URI . '/emails');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param array $emails
* @return array
*/
public function addEmails(array $emails): array
{
$options['json'] = [
'emails' => $emails
];
$response = $this->client->request(self::BASE_URI . '/emails', 'POST', $options);
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @param array $emails
* @return bool
*/
public function deleteEmails(array $emails): bool
{
$options['json'] = [
'emails' => $emails
];
$this->client->request(self::BASE_URI . '/emails', 'DELETE', $options);
return true;
}
/**
* @return array
*/
public function getStopwatches(): array
{
$response = $this->client->request(self::BASE_URI . '/stopwatches');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @return array
*/
public function getTeams(): array
{
$response = $this->client->request(self::BASE_URI . '/teams');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
/**
* @return array
*/
public function getTimes(): array
{
$response = $this->client->request(self::BASE_URI . '/times');
return \GuzzleHttp\json_decode($response->getBody(), true);
}
}

View File

@ -278,34 +278,39 @@ Status | Method | Endpoint | Comment
Status | Method | Endpoint | Comment
--- | --- | --- | ---
✅ | GET | /user |
✅ | GET | /user/emails |
✅ | POST | /user/emails |
✅ | DELETE | /user/emails |
✅ | GET | /user/followers |
✅ | GET | /user/following |
✅ | GET | /user/following/{username} |
✅ | PUT | /user/following/{username} |
✅ | DELETE | /user/following/{username} |
✅ | GET | /user/gpg_keys |
✅ | POST | /user/gpg_keys |
✅ | GET | /user/gpg_keys/{id} |
✅ | DELETE | /user/gpg_keys/{id} |
✅ | GET | /user/keys |
✅ | POST | /user/keys |
✅ | GET | /user/keys/{id} |
✅ | DELETE | /user/keys/{id} |
✅ | GET | /user/repos |
✅ | POST | /user/repos |
✅ | GET | /user/starred |
✅ | GET | /user/starred/{owner}/{repo} |
✅ | PUT | /user/starred/{owner}/{repo} |
✅ | DELETE | /user/starred/{owner}/{repo} |
✅ | GET | /user/stopwatches |
✅ | GET | /user/subscriptions |
✅ | GET | /user/teams |
✅ | GET | /user/times |
#### Users
Status | Method | Endpoint | Comment
--- | --- | --- | ---
❌ | GET | /repos/{owner}/{repo}/times/{user} |
❌ | GET | /user |
❌ | GET | /user/emails |
❌ | POST | /user/emails |
❌ | DELETE | /user/emails |
❌ | GET | /user/followers |
❌ | GET | /user/following |
❌ | GET | /user/following/{username} |
❌ | PUT | /user/following/{username} |
❌ | DELETE | /user/following/{username} |
❌ | GET | /user/gpg_keys |
❌ | POST | /user/gpg_keys |
❌ | GET | /user/gpg_keys/{id} |
❌ | DELETE | /user/gpg_keys/{id} |
❌ | GET | /user/keys |
❌ | POST | /user/keys |
❌ | GET | /user/keys/{id} |
❌ | DELETE | /user/keys/{id} |
❌ | GET | /user/repos |
❌ | POST | /user/repos |
❌ | GET | /user/starred |
❌ | GET | /user/starred/{owner}/{repo} |
❌ | PUT | /user/starred/{owner}/{repo} |
❌ | DELETE | /user/starred/{owner}/{repo} |
❌ | GET | /user/stopwatches |
❌ | GET | /user/subscriptions |
❌ | GET | /user/teams |
❌ | GET | /user/times |
❌ | GET | /users/search |
❌ | GET | /users/{follower}/following/{followee} |
❌ | GET | /users/{username} |