Browse Source

WS now working with the HydraConsole

Stephen Ficklin 7 years ago
parent
commit
ada4e87e16

+ 1 - 1
tripal_ws/includes/TripalWebService/TripalEntityService_v0_1.inc

@@ -61,7 +61,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
    */
   private function doExpandedField($ctype, $entity_id, $expfield) {
     $service_path = $this->getServicePath() . '/' . urlencode($ctype) . '/' . $entity_id;
-    $this->resource = new TripalWebServiceResource($service_path);
+    $this->resource = new TripalWebServiceResource($service_path, $this->doc_path);
 
     // Get the TripalBundle, TripalTerm and TripalVocab for this type.
     $bundle = tripal_load_bundle_entity(array('label' => $ctype));

+ 72 - 6
tripal_ws/includes/TripalWebService/TripalVocabService_v0_1.inc

@@ -22,11 +22,16 @@ class TripalVocabService_v0_1 extends TripalWebService {
    * @see TripalWebService::handleRequest()
    */
   public function handleRequest() {
+    $this->resource->addContextItem('vocab', $this->getServicePath() . '#');
 
-    $this->resource->addContextItem('vocab', $this->getServicePath());
     $this->resource->addContextItem('ApiDocumentation', 'hydra:ApiDocumentation');
     $this->resource->addContextItem('supportedClass', 'hydra:supportedClass');
     $this->resource->setType('ApiDocumentation');
+    $this->resource->setID('vocab/' . $this->getVersion());
+
+    // We need to list the content types as properties of the EntryPoint.
+    $properties = array();
+
 
     // Iterate through all of the web services and get their documentation
     $services = tripal_get_web_services();
@@ -37,15 +42,77 @@ class TripalVocabService_v0_1 extends TripalWebService {
       foreach ($supported_classes as $supported) {
         $this->resource->addProperty('supportedClass', $supported);
       }
+      if ($service_class::$type != 'vocab') {
+        $event_prop = new TripalWebServiceResource($this->getServicePath());
+        $event_prop->addProperty('hydra:title', $service_class::$type);
+        $event_prop->addProperty('hydra:description', $service_class::$description);
+        $event_prop->addContextItem('required', 'hydra:reqiured');
+        $event_prop->addProperty('required', NULL);
+        $event_prop->addContextItem('readonly', 'hydra:readonly');
+        $event_prop->addProperty('readonly', TRUE);
+        $event_prop->addContextItem('writeonly', 'hydra:writeonly');
+        $event_prop->addProperty('writeonly', FALSE);
+        $event_prop->addContextItem('property', array(
+          "@id" => "hydra:property",
+          "@type" => "@id"
+        ));
+        //$event_prop->setID('');
+        //$event_prop->setType('');
+        $prop = new TripalWebServiceResource($this->getServicePath());
+        $prop->setID('vocab:EntryPoint/' . $service_class::$type);
+        $prop->setType('hydra:Link');
+        $prop->addContextItem('label', 'rdfs:label');
+        $prop->addProperty('label', $service_class::$label);
+        $prop->addContextItem('description', 'rdfs:comment');
+        $prop->addProperty('description', $service_class::$description);
+        $prop->addContextItem('domain', array(
+          "@id" => "rdfs:domain",
+          "@type" => "@id"
+        ));
+        $prop->addProperty('domain', 'vocab:EntryPoint');
+        $prop->addContextItem('range', array(
+          "@id" => "rdfs:range",
+          "@type" => "@id"
+        ));
+        $prop->addProperty('range', 'hydra:Collection');
+
+        $prop_ops = array();
+        $prop_op = new TripalWebServiceResource($this->getServicePath());
+        $prop_op->setID('_:event_collectionretrieve');
+        $prop_op->setType('hydra:Operation');
+        $prop_op->addContextItem('method', 'hydra:method');
+        $prop_op->addProperty('method', 'GET');
+        $prop_op->addContextItem('label', 'rdfs:label');
+        $prop_op->addProperty('label', 'Retrieves all ' . $service_class::$label . ' entities.');
+        $prop_op->addContextItem('description', 'rdfs:comment');
+        $prop_op->addProperty('description', NULL);
+        $prop_op->addContextItem('expects', array(
+          "@id" => "hydra:expects",
+          "@type" => "@id"
+        ));
+        $prop_op->addProperty('expects', NULL);
+        $prop_op->addContextItem('returns', array(
+          "@id" => "hydra:returns",
+          "@type" => "@id"
+        ));
+        $prop_op->addProperty('returns', 'hydra:Collection');
+        $prop_op->addContextItem('statusCodes', 'hydra:statusCodes');
+        $prop_op->addProperty('statusCodes', array());
+        $prop_ops[] = $prop_op;
+
+        $prop->addContextItem('supportedOperation', 'hydra:supportedOperation');
+        $prop->addProperty('supportedOperation', $prop_ops);
+        $event_prop->addProperty('property', $prop);
+        $properties[] = $event_prop;
+      }
     }
 
     // Add in the generic supported classes.
     $entry_class = new TripalWebServiceResource($this->base_path);
-    $entry_class->addContextItem('vocab', $this->getServicePath());
     $entry_class->addContextItem('label', 'rdfs:label');
-    $entry_class->setID('vocab:EntryPoint');
+    $entry_class->setID($this->getServicePath() . '#EntryPoint');
     $entry_class->setType('hydra:Class');
-    $entry_class->addProperty('label', 'Entry Point');
+    $entry_class->addProperty('label', 'EntryPoint');
     $entry_class->addContextItem('description', 'rdfs:comment');
     $entry_class->addProperty('description', 'The main entry point or homepage of the API');
     $entry_class->addContextItem('subClassOf', array(
@@ -83,8 +150,7 @@ class TripalVocabService_v0_1 extends TripalWebService {
 
     $operations[] = $get_op;
     $entry_class->addProperty('supportedOperation', $operations);
-
-
+    $entry_class->addProperty('supportedProperty', $properties);
     $this->resource->addProperty('supportedClass', $entry_class);
   }
 }

+ 8 - 7
tripal_ws/includes/TripalWebServiceResource.inc

@@ -32,21 +32,21 @@ class TripalWebServiceResource {
   /**
    * Implements the constructor.
    *
-   * @param TripalWebService $service
-   *   An instance of a TripalWebService or class that extends it.
+   * @param  $service_path
+   *
    */
   public function __construct($service_path) {
+
     $this->context = array();
     $this->data = array();
     $this->service_path = $service_path;
 
     // First, add the RDFS and Hydra vocabularies to the context.  All Tripal
     // web services should use these.
-    $vocab = tripal_get_vocabulary_details('rdfs');
-    $this->addContextItem('rdfs', $vocab['url']);
+    $this->addContextItem('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+    $this->addContextItem('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
 
-    $vocab = tripal_get_vocabulary_details('hydra');
-    $this->addContextItem('hydra', $vocab['url']);
+    $this->addContextItem('hydra', "http://www.w3.org/ns/hydra/core#");
 
     $vocab = tripal_get_vocabulary_details('dc');
     $this->addContextItem('dc', $vocab['url']);
@@ -55,7 +55,8 @@ class TripalWebServiceResource {
     $this->addContextItem('schema', $vocab['url']);
 
     $vocab = tripal_get_vocabulary_details('local');
-    $this->addContextItem('local', $vocab['url']);
+    $this->addContextItem('local', url($vocab['url'], array('absolute' => TRUE)). '/');
+
 
     $this->data['@id'] = $service_path;
     $this->data['@type'] = '';

+ 13 - 3
tripal_ws/tripal_ws.module

@@ -104,7 +104,17 @@ function tripal_ws_get_services() {
   global $base_url;
   $service_path = $base_url . '/web-services';
 
-  drupal_add_http_header('Content-Type', 'application/json');
+
+  // 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('TripalVocabService_v0_1');
+  $service = new TripalVocabService_v0_1($service_path);
+  $vocab = tripal_get_vocabulary_details('hydra');
+  drupal_add_http_header('Link', '<' . $service->getServicePath() . '>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
+  drupal_add_http_header('Cache-Control', "no-cache");
 
   try {
     $ws_path = func_get_args();
@@ -200,7 +210,7 @@ function tripal_ws_list_services() {
   // Add the vocabulary to the context.
   tripal_load_include_web_service_class('TripalVocabService_v0_1');
   $service = new TripalVocabService_v0_1($base_path);
-  $resource->addContextItem('vocab', $service->getServicePath());
+  $resource->addContextItem('vocab', $service->getServicePath() . '#');
   $resource->addContextItem('EntryPoint', 'vocab:EntryPoint');
   $resource->setType('EntryPoint');
 
@@ -257,7 +267,7 @@ function tripal_ws_services() {
     // TODO: What do we do if no version is provided?
   }
 
-  drupal_add_http_header('Content-Type', 'application/json');
+  drupal_add_http_header('Content-Type', 'application/ld+json');
   print drupal_json_encode($response);
 }