I'm trying to import the json export of a web service description in code. I have the export saved as an inc file in a /webservices folder. Here is my code.

function loudservices_default_wsclient_service() {
  // Check for all web service files in webserivces directory
  $webservicefiles = file_scan_directory(drupal_get_path('module', 'loudservices') . '/webservices', '/.*\.inc$/');
  
  // Add webservice to list of webservices
  foreach ($webservicefiles as $filepath => $file) {
  $jsonservice = file_get_contents($filepath);
  $service = json_decode($jsonservice);
    if (isset($service)) {
      $services[$service->name] = $service;
    }
  }
  
  // At the end, return array of default services.
  return $services;
}

This doesn't work for me. Does anyone have any idea how I can import the json export as a web service description via code?

Comments

JayShoe’s picture

Issue summary: View changes

updated

JayShoe’s picture

Do you think that in order to do what I need to do, we need could or should integrate ctools with wsclient?

PatchRanger’s picture

hook_default_wsclient_service implementation should contain the instances of WSClientServiceDescription, which are in fact entities: see http://drupalcode.org/project/wsclient.git/blob/refs/heads/7.x-1.x:/wscl... .
So in order to define default implementation using export of this service, you should use entity_import: see http://drupalcode.org/project/entity.git/blob/refs/heads/7.x-1.x:/entity... . Something like this (I didn't try it, but I guess it should work):

function loudservices_default_wsclient_service() {
  // Check for all web service files in webserivces directory.
  $webservicefiles = file_scan_directory(drupal_get_path('module', 'loudservices') . '/webservices', '/.*\.inc$/');
 
  // Add webservice to list of webservices.
  foreach ($webservicefiles as $filepath => $file) {
    $jsonservice = file_get_contents($filepath);
    $service = entity_import('wsclient_service', $jsonservice);
    if (!empty($service)) {
      $services[$service->name] = $service;
    }
  }
 
  // At the end, return array of default services.
  return $services;
}

One more thing to note: I think there could be even better way - such as extend default EntityDefaultFeaturesController for wsclient_service entity type, which controls every interaction with export-import functionality. I guess Rules implementation could be a good example: it manages to save exports of rules into files postfixed by rules_defaults.inc and auto-import them if found as implemented here: http://drupalcode.org/project/rules.git/blob/refs/heads/7.x-2.x:/rules.f... . In my view we could go the same way.

PatchRanger’s picture

Issue summary: View changes

I understand the question better. Here is try two.