From 739f2122b3a8fb74c0d2a369635e729d5bf84f0d Mon Sep 17 00:00:00 2001 From: Kiril Kirkov Date: Sun, 24 Nov 2024 19:36:15 +0200 Subject: [PATCH] init --- .gitignore | 1 + composer.json | 16 ++++++++++++ src/BaseApiClient.php | 44 +++++++++++++++++++++++++++++++++ src/ConversionStatusChecker.php | 12 +++++++++ src/Converter.php | 12 +++++++++ src/ConverterFileDownloader.php | 30 ++++++++++++++++++++++ 6 files changed, 115 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/BaseApiClient.php create mode 100644 src/ConversionStatusChecker.php create mode 100644 src/Converter.php create mode 100644 src/ConverterFileDownloader.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49ce3c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1aa0a9d --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "tigerra/convert", + "description": "High-Performance File Conversion API for Developers & Businesses.", + "type": "library", + "autoload": { + "psr-4": { + "Tigerra\\": "src/" + } + }, + "authors": [ + { + "name": "Tigerra" + } + ], + "require": {} +} diff --git a/src/BaseApiClient.php b/src/BaseApiClient.php new file mode 100644 index 0000000..969c22a --- /dev/null +++ b/src/BaseApiClient.php @@ -0,0 +1,44 @@ +authToken = $authToken; + } + + protected function sendRequest($method, $endpoint, $params = [], $filePath = null) + { + $url = $this->apiUrl . $endpoint; + $headers = [ + "Authorization: Bearer {$this->authToken}" + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + if ($method === 'POST' && $filePath) { + $file = new \CURLFile($filePath); + $params['file'] = $file; + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + } + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpCode >= 400) { + throw new \Exception("HTTP Error: {$httpCode} - Response: {$response}"); + } + + return json_decode($response, true); + } +} \ No newline at end of file diff --git a/src/ConversionStatusChecker.php b/src/ConversionStatusChecker.php new file mode 100644 index 0000000..59e3e2d --- /dev/null +++ b/src/ConversionStatusChecker.php @@ -0,0 +1,12 @@ +sendRequest('GET', $endpoint); + } +} \ No newline at end of file diff --git a/src/Converter.php b/src/Converter.php new file mode 100644 index 0000000..0aae177 --- /dev/null +++ b/src/Converter.php @@ -0,0 +1,12 @@ +sendRequest('POST', $endpoint, [], $filePath); + } +} \ No newline at end of file diff --git a/src/ConverterFileDownloader.php b/src/ConverterFileDownloader.php new file mode 100644 index 0000000..8189d04 --- /dev/null +++ b/src/ConverterFileDownloader.php @@ -0,0 +1,30 @@ +authToken}" + ]; + + $file = fopen($outputPath, 'w'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_FILE, $file); + + $success = curl_exec($ch); + curl_close($ch); + fclose($file); + + if (!$success) { + throw new \Exception("Failed to download file."); + } + + return $outputPath; + } +} \ No newline at end of file