From f69d5da1f507a1be7cce6ec9f7f7074169d95d88 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 29 Apr 2025 22:18:09 +0200 Subject: [PATCH] Move snips class to separate file so it can be reused --- php/bin/snips | 66 +-------------------------------------------------- php/snips.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 65 deletions(-) create mode 100644 php/snips.php diff --git a/php/bin/snips b/php/bin/snips index 8d5df57..cd33935 100755 --- a/php/bin/snips +++ b/php/bin/snips @@ -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); diff --git a/php/snips.php b/php/snips.php new file mode 100644 index 0000000..20d1888 --- /dev/null +++ b/php/snips.php @@ -0,0 +1,66 @@ +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; + } +} \ No newline at end of file