Files
meetletacbfree/classes/registration/api_client.php
T

114 lines
3.6 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
/**
* HTTP client for MeetLet registration API.
*
* @package mod_meetletacbfree
* @copyright 2026 MeetLet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_meetletacbfree\registration;
defined('MOODLE_INTERNAL') || die();
/**
* Talks to MeetLet /api/v1 licensing endpoints (free-tier registration).
*/
class api_client {
/** @var string */
protected $baseurl;
public function __construct(?string $baseurl = null) {
$url = $baseurl ?? manager::api_base_url();
$this->baseurl = rtrim(trim($url), '/');
}
public function activate(array $body): array {
return $this->request('/api/v1/activations', $body);
}
public function validate(array $body): array {
return $this->request('/api/v1/validations', $body);
}
public function refresh(array $body): array {
return $this->request('/api/v1/authorizations/refresh', $body);
}
public function deactivate(string $installationid, array $body): array {
$path = '/api/v1/activations/' . rawurlencode($installationid) . '/deactivate';
return $this->request($path, $body);
}
/**
* @return array data from API envelope
*/
protected function request(string $path, array $body): array {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$url = $this->baseurl . $path;
$json = json_encode($body, JSON_UNESCAPED_SLASHES);
if ($json === false) {
throw new registration_exception(error_mapper::LOCAL_NETWORK_ERROR, 'Could not encode request JSON');
}
$curl = new \curl(['ignoresecurity' => true]);
$raw = $curl->post($url, $json, [
'CURLOPT_HTTPHEADER' => [
'Content-Type: application/json',
'Accept: application/json',
],
'CURLOPT_TIMEOUT' => 30,
]);
$info = $curl->get_info();
$httpcode = (int)($info['http_code'] ?? 0);
if ($curl->get_errno()) {
throw new registration_exception(
error_mapper::LOCAL_NETWORK_ERROR,
$curl->error . ' (url=' . $url . ')'
);
}
$raw = is_string($raw) ? trim($raw) : '';
if ($raw === '') {
throw new registration_exception(
error_mapper::LOCAL_NETWORK_ERROR,
'Empty response HTTP ' . $httpcode . ' from ' . $url
);
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
$snippet = \core_text::substr(preg_replace('/\s+/', ' ', $raw), 0, 180);
throw new registration_exception(
error_mapper::LOCAL_NETWORK_ERROR,
'Invalid JSON (HTTP ' . $httpcode . '): ' . $snippet
);
}
$requestid = isset($decoded['request_id']) ? (string)$decoded['request_id'] : null;
if (empty($decoded['success'])) {
$err = is_array($decoded['error'] ?? null) ? $decoded['error'] : [];
$code = (string)($err['code'] ?? 'UNKNOWN');
$message = (string)($err['message'] ?? 'Registration request failed');
$details = isset($err['details']) && is_array($err['details']) ? $err['details'] : null;
throw new registration_exception($code, $message, $details, $requestid);
}
$data = $decoded['data'] ?? [];
if (!is_array($data)) {
$data = [];
}
if ($requestid !== null && $requestid !== '') {
$data['_request_id'] = $requestid;
}
return $data;
}
}