-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from gregcube/api
Adds an API to MOT
- Loading branch information
Showing
15 changed files
with
655 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# API | ||
|
||
## Overview | ||
|
||
This API provides read-only access to a collection of models. It is open to the public and does not require an API key for access. | ||
|
||
### Response Format | ||
|
||
- All responses are in **JSON** format. | ||
|
||
### Rate Limiting | ||
|
||
- **Limit**: 10,000 requests per hour per IP address. | ||
- Exceeding this limit will result in temporary rate-limiting. | ||
- Rate-limited responses include the following structure: | ||
```json | ||
{ | ||
"error": { | ||
"code": 429, | ||
"message": "Rate limit exceeded. Try again later." | ||
} | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Endpoints | ||
|
||
### **GET /api/v1/models** | ||
|
||
Lists all models with pagination. | ||
|
||
#### Query Parameters: | ||
- `page` (optional): The page number to retrieve (default: `1`). Pages are 1-indexed. | ||
- `limit` (optional): The number of models per page (default: `100`). | ||
|
||
#### Example Request: | ||
```http | ||
GET /api/v1/models?page=2&limit=50 | ||
``` | ||
|
||
#### Example Response: | ||
```json | ||
{ | ||
"pager": { | ||
"total_items": 235, | ||
"total_pages": 5, | ||
"current_page": 2 | ||
}, | ||
"models": [...] | ||
} | ||
``` | ||
|
||
--- | ||
|
||
### **GET /api/v1/model/{model}** | ||
|
||
Retrieves details of a specific model. | ||
|
||
#### Path Parameters: | ||
- `model`: The ID of the model to retrieve. Model IDs can be found using the `/api/v1/models` endpoint. | ||
|
||
#### Example Request: | ||
```http | ||
GET /api/v1/model/1130 | ||
``` | ||
|
||
#### Example Response: | ||
```json | ||
{ | ||
"id": "1130", | ||
"framework": { | ||
"name": "Model Openness Framework", | ||
"version": "1.0", | ||
"date": "2024-12-15" | ||
}, | ||
"release": {...}, | ||
"classification": { | ||
"class": 1, | ||
"label": "Class I - Open Science", | ||
"progress": { | ||
"1": 100, | ||
"2": 100, | ||
"3": 100 | ||
} | ||
} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
langcode: en | ||
status: true | ||
dependencies: | ||
module: | ||
- mof | ||
- serialization | ||
- user | ||
id: model_v1 | ||
plugin_id: model_v1 | ||
granularity: resource | ||
configuration: | ||
methods: | ||
- GET | ||
formats: | ||
- json | ||
authentication: | ||
- cookie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
name: 'Model Openness Framework' | ||
type: module | ||
description: '@todo Add description.' | ||
package: '@todo Add package' | ||
description: 'Implements the model openness framework in Drupal' | ||
package: 'Model Openness Tool' | ||
core_version_requirement: ^10 | ||
dependencies: | ||
- drupal:options | ||
- drupal:rest | ||
- social_auth_github:social_auth_github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Implements hook_update_N() | ||
* | ||
* Install dependencies for MOT API. | ||
*/ | ||
function mof_update_10201() { | ||
// Enable REST and config management module. | ||
\Drupal::service('module_installer')->install([ | ||
'rest', | ||
'config', | ||
'dynamic_page_cache', | ||
'page_cache' | ||
]); | ||
|
||
// Enable MOT REST plugin. | ||
\Drupal::service('config.installer')->installDefaultConfig('module', 'mof'); | ||
|
||
// Add permission to use REST API for anonymous and authenticated users. | ||
foreach (['anonymous', 'authenticated'] as $role_name) { | ||
$role = \Drupal\user\Entity\Role::load($role_name); | ||
if ($role) { | ||
$role->grantPermission('restful get model_v1'); | ||
$role->save(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
web/modules/mof/src/EventSubscriber/ApiExceptionSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\mof\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
/** | ||
* Subscribe to exception events and return JSON responses for API errors. | ||
*/ | ||
class ApiExceptionSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
return [KernelEvents::EXCEPTION => 'onException']; | ||
} | ||
|
||
/** | ||
* Handles the exception event. | ||
* | ||
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event | ||
* The exception event. | ||
*/ | ||
public function onException(ExceptionEvent $event) { | ||
$request = $event->getRequest(); | ||
|
||
// Only handle exceptions for API hits. | ||
if (strpos($request->getPathInfo(), '/api') === 0) { | ||
$exception = $event->getThrowable(); | ||
|
||
$json = [ | ||
'error' => [ | ||
'code' => $exception->getStatusCode(), | ||
'message' => $exception->getMessage(), | ||
]]; | ||
|
||
$response = new JsonResponse($json, $exception->getStatusCode()); | ||
$event->setResponse($response); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\mof; | ||
|
||
|
Oops, something went wrong.