gist/src/Gist/Api/Client.php

160 lines
3.1 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 17:20:23 +02:00
/**
* URI of list.
*
* @const string
*/
const LIST = '/en/api/list';
/**
* The API token.
*
* @var string|null
*/
protected $apiToken;
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
*
* @param string $title The title
* @param string $type The type
* @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 17:20:23 +02:00
$this->mergeToken(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
/**
* Clones and update a gist
*
* @param string $gist Gist's ID
* @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(
2017-08-23 17:20:23 +02:00
str_replace('{gist}', $gist, $this->mergeToken(self::LIST)),
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
/**
* Lists the user's gists.
*
* @param string $gist Gist's ID
* @param string $content The content
*
* @return array
*/
public function list()
{
$response = $this->get($this->mergeToken(self::LIST));
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
/*
* Merges the API token with the given url..
*
* @param string $url
*
* @return string
*/
public function mergeToken($url)
{
if (empty($this->apiToken)) {
return $url;
}
return rtrim($url, '/').'/'.$this->apiToken;
}
/*
* Set the value of "apiToken".
*
* @param string|null $apiToken
*
* @return Client
*/
public function setApiToken($apiToken)
{
$this->apiToken = $apiToken;
return $this;
}
/*
* Get the value of "apiToken".
*
* @return string|null
*/
public function getApiToken()
{
return $this->apiToken;
}
2015-07-19 18:19:43 +02:00
}