135 lines
5.2 KiB
PHP
135 lines
5.2 KiB
PHP
<?php
|
|
class TalpaApi {
|
|
private $baseUrl;
|
|
private $token;
|
|
private $mockMode;
|
|
public $lastResponse = null;
|
|
|
|
public function __construct() {
|
|
$this->baseUrl = $_ENV['TALPA_API_BASE'] ?? '';
|
|
$this->token = $_ENV['TALPA_TOKEN'] ?? '';
|
|
$this->mockMode = filter_var($_ENV['TALPA_MOCK_MODE'] ?? false, FILTER_VALIDATE_BOOLEAN);
|
|
}
|
|
|
|
private function log($message, $data = null) {
|
|
$logFile = __DIR__ . '/api_log.txt';
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$content = "[$timestamp] $message" . ($data ? " | Data: " . json_encode($data) : "") . PHP_EOL;
|
|
file_put_contents($logFile, $content, FILE_APPEND);
|
|
}
|
|
|
|
private function request($method, $endpoint, $data = null) {
|
|
$this->log("REQUEST: $method $endpoint", $data);
|
|
|
|
if ($this->mockMode) {
|
|
$response = $this->getMockResponse($endpoint, $data);
|
|
$this->log("MOCK RESPONSE: ", $response);
|
|
$this->lastResponse = $response;
|
|
return $response;
|
|
}
|
|
|
|
$curl = curl_init();
|
|
$url = $this->baseUrl . $endpoint;
|
|
|
|
$options = [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => $method,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: application/json',
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $this->token
|
|
],
|
|
];
|
|
if ($data) {
|
|
$options[CURLOPT_POSTFIELDS] = json_encode($data);
|
|
}
|
|
|
|
curl_setopt_array($curl, $options);
|
|
$result = curl_exec($curl);
|
|
|
|
if ($result === false) {
|
|
$error = curl_error($curl);
|
|
$this->log("CURL ERROR: $error");
|
|
}
|
|
|
|
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
curl_close($curl);
|
|
|
|
$response = json_decode($result, true);
|
|
$this->log("RESPONSE (HTTP $httpCode): ", $response);
|
|
$this->lastResponse = $response;
|
|
return $response;
|
|
}
|
|
|
|
public function createEpisode($title, $duration, $seasonId) {
|
|
return $this->request('POST', '/content/v1/episodes', [
|
|
"title" => $title,
|
|
"duration" => $duration,
|
|
"seasonId" => $seasonId,
|
|
"department" => "Homeshopping"
|
|
]);
|
|
}
|
|
|
|
public function createMediaAsset($contentId) {
|
|
return $this->request('POST', '/mam/v1/mediaAssets?template=ProgrammaHDzonderOTzonderODS', [
|
|
"contentId" => $contentId,
|
|
"frameRate" => "25fps"
|
|
]);
|
|
}
|
|
|
|
public function getMediaAssetDetails($assetId) {
|
|
return $this->request('GET', '/mam/v1/mediaAssets/'.$assetId.'?full=false');
|
|
}
|
|
|
|
public function createTransmission($data) {
|
|
return $this->request('POST', '/linearSchedule/v1/transmissions', [
|
|
"channel" => $data['channel'],
|
|
"template" => $data['template'],
|
|
"scheduleVersion" => "Homeshopping",
|
|
"startDate" => $data['startDate'],
|
|
"startTime" => $data['startTime'],
|
|
"duration" => $data['duration'],
|
|
"contentId" => $data['contentId'],
|
|
"live" => false
|
|
]);
|
|
}
|
|
|
|
public function updateTransmission($transmissionId, $data) {
|
|
return $this->request('PUT', '/linearSchedule/v1/transmissions/' . $transmissionId, [
|
|
"startDate" => $data['startDate'],
|
|
"startTime" => $data['startTime'],
|
|
"duration" => $data['duration']
|
|
]);
|
|
}
|
|
|
|
public function updateEpisode($contentId, $title, $duration) {
|
|
return $this->request('PUT', '/content/v1/episodes/' . $contentId, [
|
|
"title" => $title,
|
|
"duration" => $duration,
|
|
"department" => "Homeshopping"
|
|
]);
|
|
}
|
|
|
|
public function deleteEpisode($contentId) {
|
|
return $this->request('DELETE', '/content/v1/episodes/' . $contentId);
|
|
}
|
|
|
|
private function getMockResponse($endpoint, $data) {
|
|
usleep(200000);
|
|
if (strpos($endpoint, '/content/v1/episodes') !== false && strpos($endpoint, 'DELETE') === false && !isset($data['title'])) return ["id" => "MOCK_CONT_" . time()];
|
|
if (strpos($endpoint, '/content/v1/episodes') !== false && isset($data['title'])) return ["statusCode" => "200", "message" => "Episode updated"];
|
|
if (strpos($endpoint, '/content/v1/episodes') !== false) return ["statusCode" => "200", "message" => "Episode deleted"];
|
|
if (strpos($endpoint, '/mam/v1/mediaAssets') !== false && !isset($data)) return ["mediaAssetLabel" => "TEL_MOCK_" . rand(100, 999)];
|
|
if (strpos($endpoint, '/mam/v1/mediaAssets') !== false) return ["id" => "MOCK_ASSET_" . time()];
|
|
if (strpos($endpoint, '/linearSchedule/v1/transmissions') !== false && strpos($endpoint, 'PUT') === false) return ["statusCode" => "201", "id" => "MOCK_TX_" . time()];
|
|
if (strpos($endpoint, '/linearSchedule/v1/transmissions') !== false) return ["statusCode" => "200", "message" => "Transmission updated"];
|
|
return [];
|
|
}
|
|
}
|