function tripal_ws_get_services
3.x tripal_ws.module | tripal_ws_get_services() |
The callback function for all RESTful web services.
Related topics
1 string reference to 'tripal_ws_get_services'
- tripal_ws_menu in tripal_ws/
tripal_ws.module - Implements hook_menu(). Defines all menu items needed by Tripal Core
File
- tripal_ws/
tripal_ws.module, line 131 - The Tripal Web Service Module
Code
function tripal_ws_get_services() {
global $base_url;
$service_path = $base_url . '/web-services';
// This should go out as ld+json
drupal_add_http_header('Content-Type', 'application/ld+json');
// Add a link header for the vocabulary service so that clients
// know where to find the docs.
tripal_load_include_web_service_class('TripalDocService_v0_1');
$service = new TripalDocService_v0_1($service_path);
$vocab = tripal_get_vocabulary_details('hydra');
drupal_add_http_header('Link', '<' . $service->getServicePath() . '>; rel="' . $vocab['sw_url'] . 'apiDocumentation"');
drupal_add_http_header('Cache-Control', "no-cache");
try {
$ws_path = func_get_args();
$args = $_GET;
unset($args['q']);
// The web services should never be cached.
drupal_page_is_cacheable(FALSE);
// The Tripal web services bath will be:
// [base_path]/web-services/[service name]/v[major_version].[minor_version]
$matches = array();
$service = '';
$major_version = '';
$minor_version = '';
$list_services = FALSE;
// If there is no path then we should list all of the services available.
if (empty($ws_path)) {
tripal_ws_list_services();
return;
}
// A service path will have the service name in $ws_path[0] and the
// version in $ws_path[1]. If we check that the version is correctly
// formatted then we can look for the service class and invoke it.
else if (preg_match("/^v(\d+)\.(\d+)$/", $ws_path[1], $matches)) {
$service_type = $ws_path[0];
$major_version = $matches[1];
$minor_version = $matches[2];
$service_version = 'v' . $major_version . '.' . $minor_version;
}
// If the URL doesn't match then return not found.
else {
throw new Exception("Unsupported service URL: '" . $ws_path[1] . "'");
}
// Get the service that matches the service_name
$service = NULL;
$services = tripal_get_web_services();
foreach ($services as $service_class) {
tripal_load_include_web_service_class($service_class);
if ($service_class::$type == $service_type) {
$service = new $service_class($service_path);
if ($service->getVersion() == $service_version) {
break;
}
$service = NULL;
}
}
// If a service was not provided then return an error.
if (!$service) {
throw new Exception('The service type, "' . $service_type . '", is not available');
}
// Adjust the path to remove the service type and the version.
$adj_path = $ws_path;
array_shift($adj_path);
array_shift($adj_path);
// Now call the service to handle the request.
$service->setPath($adj_path);
$service->setParams($args);
$service->handleRequest();
$response = $service->getResponse();
print drupal_json_encode($response);
}
catch (Exception $e) {
$service = new TripalWebService($service_path);
$service->setError($e->getMessage());
$response = $service->getResponse();
print drupal_json_encode($response);
}
}