123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- /**
- * Implements hook_init()
- */
- function tripal_ws_init() {
- global $base_url;
- $version = 'v0.1';
- $api_url = $base_url . '/ws/' . $version;
- // Following the WC3 Hydra documentation, we want to add LINK to the header
- // of the site that indicates where the API documentation can be found.
- // This allows a hydra-enabled client to discover the API and use it.
- $attributes = array(
- 'rel' => 'http://www.w3.org/ns/hydra/core#apiDocumentation',
- 'href' => $api_url . '/ws-doc/',
- );
- drupal_add_html_head_link($attributes, $header = FALSE);
- }
- /**
- * Implements hook_menu().
- * Defines all menu items needed by Tripal Core
- *
- * @ingroup tripal_ws
- */
- function tripal_ws_menu() {
- // Web Services API callbacks.
- $items['ws'] = array(
- 'title' => 'Tripal Web Services API',
- 'page callback' => 'tripal_ws_services',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK,
- );
- // Tripal Web Services setting groups
- $items['admin/tripal/ws'] = array(
- 'title' => 'Web Services',
- 'description' => t("Exchange data between Tripal sites using the web services."),
- 'weight' => 20,
- 'page callback' => 'system_admin_menu_block_page',
- 'access arguments' => array('administer tripal'),
- 'file' => 'system.admin.inc',
- 'file path' => drupal_get_path('module', 'system'),
- );
- $items['admin/tripal/ws/tripal_sites'] = array(
- 'title' => 'Other Tripal Sites',
- 'description' => t('Provides information about other Tripal sites.
- This allows data exchange and communication betwen Tripal
- enabled sites through the web services.'),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_ws_tripal_sites_form'),
- 'access arguments' => array('administer tripal'),
- 'type' => MENU_NORMAL_ITEM,
- 'weight' => 0,
- 'file' => 'includes/tripal_ws.admin.inc',
- 'file path' => drupal_get_path('module', 'tripal_ws'),
- );
- return $items;
- }
- /**
- * The callback function for all RESTful web services.
- *
- */
- function tripal_ws_services() {
- $ws_path = func_get_args();
- $params = $_GET;
- unset($params['q']);
- // The web services should never be cached.
- drupal_page_is_cacheable(FALSE);
- // Using the provided version number, determine which web services
- // verion to call.
- $version = array_shift($ws_path);
- if ($version and preg_match('/v\d+\.\d+/', $version)) {
- $api_url = 'ws/' . $version;
- // Add the file with the appropriate web services.
- module_load_include('inc', 'tripal_ws', 'includes/tripal_ws.rest_' . $version);
- $version = preg_replace('/\./', '_', $version);
- $function = 'tripal_ws_services_' . $version;
- $response = array();
- if (function_exists($function)) {
- $response = $function($api_url, $ws_path, $params);
- }
- }
- else {
- // TODO: What do we do if no version is provided?
- }
- drupal_add_http_header('Content-Type', 'application/ld+json');
- print drupal_json_encode($response);
- }
|