Poster Service

  • 24 September 2017
  • GeeX

Drupal7 | jDrupal 

REST Service Implementation für Drupalgap(jDrupal) 

Drupalgap REST Service Beispiel (Services API 3.0)

poster.info:

name = poster
description = Awri Mobile Poster Modul 
package = custom
core = 7.x
dependencies[]=services
dependencies[] = rest_server	
project = "custom"
datestamp = "1398963366"

 

poster.module.inc

 function poster_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => 'poster.services.inc'
    );
  }
}

poster.resource.inc

//Parameterübergabe in PHP
//$/data[0]=array("uid"=>"3");
//$data[1]=array{"message"=>"Ich habe ne Frage");
function post_anonymous($data) {
	watchdog("poster", '<pre>' . print_r( $data, true) . '</pre>');		
	return  $data[0]['uid'].' -> '.$data[1]['message'];
}

poster.services.inc:

function poster_services_resources()
{
$resources = array(
		'poster_resources' => array(
				'actions' => array(

//->Add endpoint								
						'post_anonymous' => array(
								'help' => t('Der Endpunkt nimmmt eine anonyme Rechtsfrage an'),
								'file' => array(
										'type' => 'inc',
										'module' => 'poster',
										'name' => 'poster.resource',
								),
								'callback' => 'post_anonymous',
								'args' => array(
										array(
												'name'         => 'uid',
												'type'         => 'string',
												'description'  => t('The uid to send.'),
												'source'       => 'data',
												'optional'     => FALSE,
										),
										array(
												'name'         => 'message',
												'type'         => 'string',
												'description'  => t('The message to post.'),
												'source'       => 'data',
												'optional'     => FALSE,
										)
								), //collback args array

									'access callback' => '_drupalgap_resource_access',
									'access callback file' => array(
											'type' => 'inc',
											'module' => 'drupalgap',
											'name' => 'drupalgap.resource',
									),
									'access arguments' => array('access content'),
									'access arguments append' => TRUE,
							)//post_anoymous endpoint

//->Add endpoint
								
					),//actions
			),//poster_resources

	);//resurces
	return $resources;
}

Service Client (jDrupal)

1. Cient Aufruf mit JDrupal

var my_params = [ {
		'uid' : '3'
	}, {
		'message' : 'Ich habe ne Frage'
	} ];
	Drupal.services.call({
		method : 'POST',
		path : 'poster_resources/post_anonymous.json',
		service : 'poster',
		resource : 'post_anonymous',
		data : JSON.stringify(my_params),
		success : function(result) {
			console.log(result);
			var response = result;
			var msg = 'Poster direct call: ' + response + ' !';
			drupalgap_alert(msg);
		}
	});

2. Cient Aufruf mit JDrupal

/ Aufruf über eine Funktion muss selbst in einer funktion sein
	var my_params = [ {
		'uid' : '100000611529195'
	}, {
		'message' : 'HLLLLAOO'
	} ];
	awri_postit({
		success : function(result) {
			var user_count = result[0];
			var msg = 'Poster function: ' + user_count + ' !'
			drupalgap_alert(msg);
		}
	}, my_params);
}

function awri_postit(options, my_params) {
	try {
		console.log(my_params);
		options.method = 'POST';
		options.path = 'poster_resources/post_anonymous.json';
		options.service = 'poster';
		options.resource = 'post_anonymous';
		options.data = JSON.stringify(my_params);
		Drupal.services.call(options);
	} catch (error) {
		console.log('poster_resources/post_anonymous - ' + error);
	}
}

 

Drupal Service Debug

In den Service Funktionen die Daten in den Watchdog schreiben:

drush watchdog("poster", '' . print_r( $data, true) . '');

Daten in Echtzeit ansehen:

drush watchdog-show --tail --sleep-delay=2