Move snips class to separate file so it can be reused

This commit is contained in:
Tim 2025-04-29 22:18:09 +02:00
parent 6a6edd4784
commit f69d5da1f5
2 changed files with 67 additions and 65 deletions

View File

@ -3,71 +3,7 @@
require_once __DIR__ . '/../argvParser.php';
require_once __DIR__ . '/../functions.php';
class snips
{
public function __construct(
private readonly string $baseUrl,
private readonly string $apiKey,
) {}
public function getSnip(int $id): string
{
$url = $this->baseUrl . 'snip/' . $id;
$headers = [
'X-AUTH-TOKEN: ' . $this->apiKey,
'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content'];
} else {
throw new Exception('Error fetching snip: ' . json_encode($response));
}
// rewrite to curl
}
public function postSnip(int $id, string $content): string
{
$url = $this->baseUrl . 'snip/' . $id;
$headers = [
'X-AUTH-TOKEN: ' . $this->apiKey,
'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]);
$response = curl_exec($ch);
curl_close($ch);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content'];
} else {
throw new Exception('Error updating snip: ' . json_encode($response));
}
}
public function getResponse(bool|string $response): array
{
$response = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Error decoding JSON response: ' . json_last_error_msg());
}
return $response;
}
}
require_once __DIR__ . '/../snips.php';
$argvParser = new argvParser(['f' => 'File', 'a' => 'Api key'], ['c' => 'Command', 'id' => 'Snip id']);
$parsed = $argvParser->parseArgv($argv);

66
php/snips.php Normal file
View File

@ -0,0 +1,66 @@
<?php
readonly class snips
{
public function __construct(
private string $baseUrl,
private string $apiKey,
) {}
public function getSnip(int $id): string
{
$url = $this->baseUrl . 'snip/' . $id;
$headers = [
'X-AUTH-TOKEN: ' . $this->apiKey,
'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content'];
} else {
throw new Exception('Error fetching snip: ' . json_encode($response));
}
// rewrite to curl
}
public function postSnip(int $id, string $content): string
{
$url = $this->baseUrl . 'snip/' . $id;
$headers = [
'X-AUTH-TOKEN: ' . $this->apiKey,
'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]);
$response = curl_exec($ch);
curl_close($ch);
$response = $this->getResponse($response);
if (isset($response['success']) && $response['success'] === true) {
return $response['data']['content'];
} else {
throw new Exception('Error updating snip: ' . json_encode($response));
}
}
private function getResponse(bool|string $response): array
{
$response = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Error decoding JSON response: ' . json_last_error_msg());
}
return $response;
}
}