Skip to content

Commit c818089

Browse files
committed
Added API endpoint to retrieve matomo swagger file
1 parent 2a89ff8 commit c818089

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

API.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,50 @@
1010
namespace Piwik\Plugins\OpenApiDocs;
1111

1212
use Piwik\Piwik;
13+
use Piwik\Plugin\Manager;
1314
use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator;
1415

1516
/**
1617
* API for plugin OpenApiDocs
1718
*
19+
* Exposes endpoints to fetch pre-generated OpenAPI specs or generate plugin-specific
20+
* OpenAPI docs on demand.
21+
*
1822
* @method static \Piwik\Plugins\OpenApiDocs\API getInstance()
1923
*/
2024
class API extends \Piwik\Plugin\API
2125
{
26+
/**
27+
* Get the pre-generated single OpenAPI spec file if it exists. This endpoint only reads
28+
* the generated JSON file and does not trigger spec generation.
29+
*
30+
* /index.php?module=API&method=OpenApiDocs.getMatomoOpenApiSpec
31+
*
32+
* @return array<string, mixed> The decoded OpenAPI specification payload.
33+
* @throws \Exception If the file is missing, unreadable, or contains invalid JSON.
34+
*/
35+
public function getMatomoOpenApiSpec(): array
36+
{
37+
$currentPluginDir = Manager::getInstance()::getPluginDirectory('OpenApiDocs');
38+
$filePath = $currentPluginDir . OpenApiDocs::GENERATED_SPECS_PATH . 'matomo_openapi_spec_v' . OpenApiDocs::DEFAULT_SPEC_VERSION . '.json';
39+
40+
if (!is_file($filePath) || !is_readable($filePath)) {
41+
throw new \Exception('OpenAPI spec file was not found. Generate it first via openapidocs:generate-spec-file.');
42+
}
43+
44+
$specContents = file_get_contents($filePath);
45+
if ($specContents === false) {
46+
throw new \Exception('OpenAPI spec file could not be read.');
47+
}
48+
49+
$decodedSpec = json_decode($specContents, true);
50+
if (!is_array($decodedSpec) || json_last_error() !== JSON_ERROR_NONE) {
51+
throw new \Exception('OpenAPI spec file contains invalid JSON.');
52+
}
53+
54+
return $decodedSpec;
55+
}
56+
2257
/**
2358
* Get the generated API documentation data for the specified plugin.
2459
*

0 commit comments

Comments
 (0)