Tuesday, 4 December 2012

Drupal - Drupal API for Web Services to be Exposed in Different Server Formats

Usage of Services Module in Drupal 7
  1.   Create a unified Drupal API for web services to be exposed in a variety of different server formats.
    1. BENCODE
    2. ICAL
    3. JSON
    4. PHP
    5. RSS
    6. XCAL
    7. XML
  2.   Provide a service browser to be able to test methods.
  3.   Allow distribution of API keys for developer access.
Integrate Services Module in Drupa 7
  1. Download Services Module from "http://drupal.org/project/services".
  2. Place the module in sites/all/modules folder of your drupal instance
  3. Enable the Services Module and it dependencies from admin/module
  4. Add a Service from "admin/structure/services" Page by clicking on "Add" link.
    1. In the Add Service Page, based on the added "Machine-readable name of the endpoint" field only we will configure the Resource Methods and Resource Formatters for the created Service.
    2. In the Add Service Page, based on the added "Path to endpoint" field only we will access the formatters.
  5. From admin/structure/services/list/[Your Service Machine Readable Name]/resourceslink enable the Methods which we would like to use for REST Operations [Formatting]
  6. From admin/structure/services/list/[Your Service Machine Readable Name]/server  link, we have to specify the Response Formatters for REST
  1. In my case, i would like to make all "nodes" should use JSON exposed Format.
    1. Created a Service with the below details.
      1. Machine-readable name of the endpoint - rest_services
      2. Path to endpoint - restservices
    2. In admin/structure/services/list/rest_services/resources, i have enabled NODE resource and corresponding comments as my suggested Resource Methods.
    3. In admin/structure/services/list/rest_services/server, i have enabled JSON as a resource formatters.
    4. Saved all Necessary Configurations.
  2. To test the above settings, i have executed the below URL for my "First Content" node to Generate JSON.
    1. restservices/node/1.json   [In the Give URL Structure is like, http://<ourserver>/[Path to endpoint]/node/<node-id>.json where <node-id> is 1]
O/P of the 1.json is

{"vid":"1","uid":"1","title":"First Content","log":"","status":"1","comment":"1","promote":"0","sticky":"0","nid":"1","type":"page","language":"und","created":"1354606436","changed":"1354606436","tnid":"0","translate":"0","revision_timestamp":"1354606436","revision_uid":"1","body":{"und":[{"value":"First Content","summary":"","format":"filtered_html","safe_value":"<p>First Content</p>\n","safe_summary":""}]},"rdf_mapping":{"rdftype":["foaf:Document"],"title":{"predicates":["dc:title"]},"created":{"predicates":["dc:date","dc:created"],"datatype":"xsd:dateTime","callback":"date_iso8601"},"changed":{"predicates":["dc:modified"],"datatype":"xsd:dateTime","callback":"date_iso8601"},"body":{"predicates":["content:encoded"]},"uid":{"predicates":["sioc:has_creator"],"type":"rel"},"name":{"predicates":["foaf:name"]},"comment_count":{"predicates":["sioc:num_replies"],"datatype":"xsd:integer"},"last_activity":{"predicates":["sioc:last_activity_date"],"datatype":"xsd:dateTime","callback":"date_iso8601"}},"cid":"0","last_comment_timestamp":"1354606436","last_comment_name":null,"last_comment_uid":"1","comment_count":"0","name":"admin","picture":"0","data":"b:0;","path":"http://localhost/drupal7/node/1"}

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.