Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiril Kirkov authored and Kiril Kirkov committed Nov 24, 2024
0 parents commit 739f212
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
44 changes: 44 additions & 0 deletions src/BaseApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tigerra;

class BaseApiClient
{
protected $apiUrl = "https://convert.tigerra.com";
protected $authToken;

public function __construct($authToken)
{
$this->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);
}
}
12 changes: 12 additions & 0 deletions src/ConversionStatusChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tigerra;

class ConversionStatusChecker extends BaseApiClient
{
public function checkStatus($pid)
{
$endpoint = "/get-status/{$pid}";
return $this->sendRequest('GET', $endpoint);
}
}
12 changes: 12 additions & 0 deletions src/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tigerra;

class Converter extends BaseApiClient
{
public function convert($conversionType, $filePath)
{
$endpoint = "/do-convert/{$conversionType}";
return $this->sendRequest('POST', $endpoint, [], $filePath);
}
}
30 changes: 30 additions & 0 deletions src/ConverterFileDownloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tigerra;


class ConverterFileDownloader extends BaseApiClient
{
public function downloadFile($downloadUrl, $outputPath)
{
$ch = curl_init($downloadUrl);
$headers = [
"Authorization: Bearer {$this->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;
}
}

0 comments on commit 739f212

Please sign in to comment.