gist/src/Gist/Api/Client.php

85 lines
1.8 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
/**
* URI of creation
*
* @const string
*/
2015-07-19 18:19:43 +02:00
const CREATE = '/en/api/create';
2016-11-13 00:44:23 +01:00
/**
* URI of updating
*
* @const string
*/
2015-11-07 22:27:41 +01:00
const UPDATE = '/en/api/update/{gist}';
2015-07-19 18:19:43 +02:00
2016-11-13 00:44:23 +01:00
/**
* Creates a gist
*
* @param string $title The title
* @param string $type The type
* @param string $content The content
* @return array
*/
2015-07-19 18:19:43 +02:00
public function create($title, $type, $content)
{
$response = $this->post(
self::CREATE,
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)
2015-11-07 22:13:08 +01:00
$response = $this->post(
2015-11-07 22:27:41 +01:00
str_replace('{gist}', $gist, 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 [];
}
2015-07-19 18:19:43 +02:00
}