gist/src/Gist/Api/Client.php

185 lines
3.6 KiB
PHP
Raw Normal View History

2015-07-19 18:19:43 +02:00
<?php
namespace Gist\Api;
use GuzzleHttp\Client as BaseClient;
/**
2016-11-13 00:44:23 +01:00
* Class Client.
*
2015-07-19 18:19:43 +02:00
* @author Simon Vieille <simon@deblan.fr>
*/
class Client extends BaseClient
{
2016-11-13 00:44:23 +01:00
/**
2017-06-25 19:13:27 +02:00
* URI of creation.
*
2016-11-13 00:44:23 +01:00
* @const string
*/
2015-07-19 18:19:43 +02:00
const CREATE = '/en/api/create';
2016-11-13 00:44:23 +01:00
/**
2017-06-25 19:13:27 +02:00
* URI of update.
2016-11-13 00:44:23 +01:00
*
* @const string
*/
2015-11-07 22:27:41 +01:00
const UPDATE = '/en/api/update/{gist}';
2015-07-19 18:19:43 +02:00
2017-08-23 19:32:56 +02:00
/**
* URI of delete.
*
* @const string
*/
const DELETE = '/en/api/delete/{gist}';
2017-08-23 17:20:23 +02:00
/**
* URI of list.
*
* @const string
*/
const LIST = '/en/api/list';
/**
2017-08-23 19:32:56 +02:00
* The API key.
2017-08-23 17:20:23 +02:00
*
* @var string|null
*/
2017-08-23 19:32:56 +02:00
protected $apiKey;
2017-08-23 17:20:23 +02:00
2016-11-13 00:44:23 +01:00
/**
2017-06-25 19:13:27 +02:00
* Creates a gist.
2016-11-13 00:44:23 +01:00
*
2017-08-23 19:32:56 +02:00
* @param string $title The title
* @param string $type The type
2016-11-13 00:44:23 +01:00
* @param string $content The content
2017-06-25 19:13:27 +02:00
*
2016-11-13 00:44:23 +01:00
* @return array
*/
2015-07-19 18:19:43 +02:00
public function create($title, $type, $content)
{
$response = $this->post(
2017-08-23 19:32:56 +02:00
$this->mergeApiKey(self::CREATE),
2015-07-19 18:19:43 +02:00
array(
'form_params' => array(
'form' => array(
'title' => $title,
'type' => $type,
'content' => $content,
),
),
)
);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
2015-07-19 18:23:26 +02:00
return [];
2015-07-19 18:19:43 +02:00
}
2016-11-13 00:44:23 +01:00
/**
2017-08-23 19:32:56 +02:00
* Clones and update a gist.
2016-11-13 00:44:23 +01:00
*
2017-08-23 19:32:56 +02:00
* @param string $gist Gist's ID
2016-11-13 00:44:23 +01:00
* @param string $content The content
*
* @return array
*/
2015-11-07 22:27:41 +01:00
public function update($gist, $content)
2016-11-17 00:13:09 +01:00
{
2015-11-07 22:13:08 +01:00
$response = $this->post(
str_replace('{gist}', $gist, $this->mergeApiKey(self::UPDATE)),
2015-11-07 22:13:08 +01:00
array(
'form_params' => array(
'form' => array(
'content' => $content,
),
),
)
);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
2017-08-23 17:20:23 +02:00
/**
2017-08-23 19:32:56 +02:00
* Deletes a gist.
2017-08-23 17:20:23 +02:00
*
* @param string $gist Gist's ID
2017-08-23 19:32:56 +02:00
*
* @return array
*/
public function delete($gist)
{
$response = $this->post(str_replace('{gist}', $gist, $this->mergeApiKey(self::DELETE)));
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
/**
* Lists the user's gists.
*
* @param string $gist Gist's ID
2017-08-23 17:20:23 +02:00
* @param string $content The content
*
* @return array
*/
public function list()
{
2017-08-23 19:32:56 +02:00
$response = $this->get($this->mergeApiKey(self::LIST));
2017-08-23 17:20:23 +02:00
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
/*
2017-08-23 19:32:56 +02:00
* Merges the API key with the given url.
2017-08-23 17:20:23 +02:00
*
* @param string $url
*
* @return string
*/
2017-08-23 19:32:56 +02:00
public function mergeApiKey($url)
2017-08-23 17:20:23 +02:00
{
2017-08-23 19:32:56 +02:00
if (empty($this->apiKey)) {
2017-08-23 17:20:23 +02:00
return $url;
}
2017-08-23 19:32:56 +02:00
return rtrim($url, '/').'/'.$this->apiKey;
2017-08-23 17:20:23 +02:00
}
/*
2017-08-23 19:32:56 +02:00
* Set the value of "apiKey".
2017-08-23 17:20:23 +02:00
*
2017-08-23 19:32:56 +02:00
* @param string|null $apiKey
2017-08-23 17:20:23 +02:00
*
* @return Client
*/
2017-08-23 19:32:56 +02:00
public function setApiKey($apiKey)
2017-08-23 17:20:23 +02:00
{
2017-08-23 19:32:56 +02:00
$this->apiKey = $apiKey;
2017-08-23 17:20:23 +02:00
return $this;
}
/*
2017-08-23 19:32:56 +02:00
* Get the value of "apiKey".
2017-08-23 17:20:23 +02:00
*
* @return string|null
*/
2017-08-23 19:32:56 +02:00
public function getApiKey()
2017-08-23 17:20:23 +02:00
{
2017-08-23 19:32:56 +02:00
return $this->apiKey;
2017-08-23 17:20:23 +02:00
}
2015-07-19 18:19:43 +02:00
}