mirror of
https://github.com/sitelease/sugar-cube-client.git
synced 2025-11-02 21:12:30 +01:00
Huge number of updates, too tired to list them all
+ Added a bunch of API handler classes that will use the already created models + Created a new Client class that will connect to new API handler classes + Created new collection classes
This commit is contained in:
319
src/Api/AbstractApi.php
Normal file
319
src/Api/AbstractApi.php
Normal file
@ -0,0 +1,319 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api;
|
||||
|
||||
use Gitea\Client;
|
||||
|
||||
/**
|
||||
* Abstract class for Api classes
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*/
|
||||
abstract class AbstractApi
|
||||
{
|
||||
/**
|
||||
* The client
|
||||
*
|
||||
* @var Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* The API authentication token for Gitea
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $authToken;
|
||||
|
||||
/**
|
||||
* The default parameters that should be sent
|
||||
* in ALL requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultParameters = [];
|
||||
|
||||
/**
|
||||
* The default headers that should be sent
|
||||
* in ALL requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultHeaders = [
|
||||
'Accept' => 'application/json'
|
||||
];
|
||||
|
||||
/**
|
||||
* The default parameters that should be sent
|
||||
* in all GET requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $getDefaultParameters = [];
|
||||
|
||||
/**
|
||||
* The default headers that should be sent
|
||||
* in all GET requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $getDefaultHeaders = [];
|
||||
|
||||
/**
|
||||
* The default headers that should be sent
|
||||
* in all POST requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $postDefaultHeaders = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
/**
|
||||
* The default headers that should be sent
|
||||
* in all PUT requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $putDefaultHeaders = [
|
||||
'Content-Type' => 'application/json'
|
||||
];
|
||||
|
||||
/**
|
||||
* The default headers that should be sent
|
||||
* in all DELETE requests to this api route
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $deleteDefaultHeaders = [];
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
*/
|
||||
public function __construct(Client $client, $authToken)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->authToken = $authToken;
|
||||
|
||||
// Set authorization headers and parameters
|
||||
$this->getDefaultParameters["access_token"] = $authToken;
|
||||
$this->postDefaultHeaders["Authorization"] = "token $authToken";
|
||||
$this->putDefaultHeaders["Authorization"] = "token $authToken";
|
||||
$this->deleteDefaultHeaders["Authorization"] = "token $authToken";
|
||||
}
|
||||
|
||||
public function getClient() {
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function getAuthToken() {
|
||||
return $this->authToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default parameters for a particular type of request
|
||||
*
|
||||
* If "all" is passed OR if the $type parameter is left blank
|
||||
* an array of defaults for ALL request types will be returned
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @param string $type The type of request ("all","get","post","put",etc.)
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultParametersForType($type = "all") {
|
||||
if (!$type || $type == "all") {
|
||||
return $this->defaultParameters;
|
||||
} else {
|
||||
$propertyName = $type."DefaultParameters";
|
||||
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
|
||||
return array_merge($this->defaultParameters, $this->$propertyName);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default headers for a particular type of request
|
||||
*
|
||||
* If "all" is passed OR if the $type parameter is left blank
|
||||
* an array of defaults for ALL request types will be returned
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @param string $type The type of request ("all", "get","post","put",etc.)
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultHeadersForType($type = "all") {
|
||||
if (!$type || $type == "all") {
|
||||
return $this->defaultHeaders;
|
||||
} else {
|
||||
$propertyName = $type."DefaultHeaders";
|
||||
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
|
||||
return array_merge($this->defaultParameters, $this->$propertyName);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function configure()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a GET query and returns the response as a PSR-7 response object.
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param array $requestHeaders
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
|
||||
{
|
||||
$path = $this->preparePath($path, $parameters);
|
||||
|
||||
return $this->client->getHttpClient()->get($path, $requestHeaders);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
$defaultParameters = $this->getDefaultParametersForType("get");
|
||||
$defaultHeaders = $this->getDefaultParametersForType("get");
|
||||
|
||||
// Create request options array
|
||||
// and populate it with defaults
|
||||
$requestOptions = [];
|
||||
$requestOptions['query'] = $defaultParameters;
|
||||
$requestOptions['headers'] = $defaultHeaders;
|
||||
if ($debugRequest) {
|
||||
$requestOptions['debug'] = true;
|
||||
}
|
||||
|
||||
if ($parameters) {
|
||||
$requestOptions['query'] = array_merge($defaultParameters, $parameters);
|
||||
}
|
||||
|
||||
if ($requestHeaders) {
|
||||
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
|
||||
}
|
||||
|
||||
return $guzzleClient->request('GET', $path, $requestOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $body
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function post($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
$defaultHeaders = $this->getDefaultHeadersForType("post");
|
||||
|
||||
// Create request options array
|
||||
// and populate it with defaults
|
||||
$requestOptions = [];
|
||||
$requestOptions['headers'] = $defaultHeaders;
|
||||
$requestOptions['body'] = "{}";
|
||||
if ($debugRequest) {
|
||||
$requestOptions['debug'] = true;
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
if (is_object($body)) {
|
||||
$requestOptions['body'] = json_encode($body);
|
||||
}
|
||||
|
||||
if (is_string($body)) {
|
||||
$requestOptions['body'] = $body;
|
||||
}
|
||||
}
|
||||
|
||||
if ($requestHeaders) {
|
||||
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
|
||||
}
|
||||
|
||||
return $guzzleClient->request('POST', $path, $requestOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $body
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function put($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
$defaultHeaders = $this->getDefaultHeadersForType("put");
|
||||
|
||||
// Create request options array
|
||||
// and populate it with defaults
|
||||
$requestOptions = [];
|
||||
$requestOptions['headers'] = $defaultHeaders;
|
||||
$requestOptions['body'] = "{}";
|
||||
if ($debugRequest) {
|
||||
$requestOptions['debug'] = true;
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
if (is_object($body)) {
|
||||
$requestOptions['body'] = json_encode($body);
|
||||
}
|
||||
|
||||
if (is_string($body)) {
|
||||
$requestOptions['body'] = $body;
|
||||
}
|
||||
}
|
||||
|
||||
if ($requestHeaders) {
|
||||
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
|
||||
}
|
||||
|
||||
return $guzzleClient->request('PUT', $path, $requestOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function delete($path, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
$defaultHeaders = $this->getDefaultHeadersForType("delete");
|
||||
|
||||
// Create request options array
|
||||
// and populate it with defaults
|
||||
$requestOptions = [];
|
||||
$requestOptions['headers'] = $defaultHeaders;
|
||||
if ($debugRequest) {
|
||||
$requestOptions['debug'] = true;
|
||||
}
|
||||
|
||||
if ($requestHeaders) {
|
||||
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
|
||||
}
|
||||
|
||||
return $guzzleClient->request('DELETE', $path, $requestOptions);
|
||||
}
|
||||
}
|
||||
46
src/Api/Organizations.php
Normal file
46
src/Api/Organizations.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api;
|
||||
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
use Gitea\Client;
|
||||
use Gitea\Models\Organization;
|
||||
|
||||
use Gitea\Api\AbstractApi;
|
||||
|
||||
class Organizations extends AbstractApi
|
||||
{
|
||||
|
||||
/**
|
||||
* Get an organization using its username and parse
|
||||
* it's information into an organization object
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* $giteaClient->organizations()->getByUsername($orgUsername);
|
||||
* ```
|
||||
*
|
||||
* @param string $username
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @return Organization|Response
|
||||
*/
|
||||
public function getByUsername(string $username)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
try {
|
||||
$response = $this->get("orgs/$username");
|
||||
$statusCode = $response->getStatusCode();
|
||||
$body = $response->getBody();
|
||||
if ($statusCode == 200) {
|
||||
return Organization::fromJson(json_decode($body));
|
||||
} else {
|
||||
return $response;
|
||||
}
|
||||
} catch (ServerException $serverError) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
115
src/Api/Repositories.php
Normal file
115
src/Api/Repositories.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api;
|
||||
|
||||
use Gitea\Client;
|
||||
use Gitea\Collections\ApiItemCollection;
|
||||
use Gitea\Models\Repository;
|
||||
|
||||
use Gitea\Api\AbstractApi;
|
||||
|
||||
class Repositories extends AbstractApi
|
||||
{
|
||||
|
||||
/**
|
||||
* The maximum number of pages to process when
|
||||
* retrieving all repositories
|
||||
*
|
||||
* NOTE: Each page will contain the number of items
|
||||
* set in the $maxPageCount property (50 by default).
|
||||
* So you can get the number of records that this will equate to
|
||||
* by multiplying this number by the $maxPageCount
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $maxPageCount = 25;
|
||||
|
||||
/**
|
||||
* The number of items per page
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $itemsPerPage = 50;
|
||||
|
||||
/**
|
||||
* Return a collection of all the repositories
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$allItems = array();
|
||||
$maxPageCount = $this->getMaxPageCount();
|
||||
// Loop over pages until the $maxPageCount is reached
|
||||
for ($pageNum=1; $pageNum < $maxPageCount; $pageNum++) {
|
||||
$searchItemsCollection = $this->search("", $pageNum);
|
||||
if ($searchItemsCollection && $searchItemsCollection->count() > 0) {
|
||||
$searchItemsArray = $searchItemsCollection->toArray();
|
||||
$allItems = array_merge($allItems, $searchItemsArray);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new ApiItemCollection($allItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search all repositories and return a list of them
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* $giteaClient->repositories()->search($keyword, $page);
|
||||
* ```
|
||||
*
|
||||
* @param string $keyword Keyword to search by
|
||||
* @param integer $page The page of items to return
|
||||
* @param integer $limit Maximum number of items per page
|
||||
* @param array $extraOptions An array of extra options to pass the API reoute
|
||||
* @return void
|
||||
*
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function search(string $keyword = "", int $page = 1, int $limit = null, array $extraOptions = array())
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$limit = $limit ?? $this->getItemsPerPage();
|
||||
|
||||
$repositoryCollection = new ApiItemCollection();
|
||||
try {
|
||||
$response = $this->get("repos/search",[
|
||||
"page" => $page,
|
||||
"limit" => $limit
|
||||
]);
|
||||
$statusCode = $response->getStatusCode();
|
||||
$body = (string) $response->getBody();
|
||||
if ($statusCode == 200) {
|
||||
$jsonObj = json_decode($body, true);
|
||||
$jsonOk = $jsonObj["ok"];
|
||||
$jsonItemList = $jsonObj["data"];
|
||||
|
||||
if ($jsonOk && count($jsonItemList) > 0) {
|
||||
foreach ($jsonItemList as $jsonItem) {
|
||||
$encodedItem = json_encode($jsonItem);
|
||||
$itemObject = Repository::fromJson(json_decode($encodedItem));
|
||||
$repositoryCollection->addItem($itemObject, $itemObject->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return $repositoryCollection;
|
||||
|
||||
} catch (ServerException $serverError) {
|
||||
return $repositoryCollection;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMaxPageCount() {
|
||||
return $this->maxPageCount;
|
||||
}
|
||||
|
||||
public function getItemsPerPage() {
|
||||
return $this->itemsPerPage;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user