1.0.7 • Published 8 years ago

vdata v1.0.7

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

vdata

VDATA protocol is the best protocol for calling or responding a HTTP/HTTPS service.

Features

  • Defines the data protocol for HTTP/HTTPS based service Remote Process Call.
  • Defines common error codes and the range of user customized error codes.
  • Defines common status codes.
  • RESTfull api supported.
  • Cross-Origin Resource Sharing supported.
  • Send version request by HTTP_ACCEPT from client.
  • Respond in-service version of service from server.

Data Protocol/Format

{
	"name"			: "",		//	string,	name of service
	"url"			: "",		//	string,	address of service
	"version"		: "1.0",	//	string,	version of service
	"errorid"		: 0,		//	numeric,	error id
	"errordesc"		: "",		//	string,	desciption of error
	"parents"		: {},		//	array,	parent nodes
	"vdata"			: {}		//	user customized data. it might be a string, number, array or an object
}

Comnon error codes

error defineserror coderemark
ERROR_SUCCESS0successfully
ERROR_UNKNOWN-100000unknown errors
ERROR_ACCESS_DENIED-100001access denied
ERROR_PARAMETER-100002error in parameters
ERROR_PERMISSION-100003error in permission
ERROR_EXPIRED-100004error in expired
ERROR_NOT_LOGGEDIN-100005error in not logged in
ERROR_FAILED_LOGGEDIN-100006error in failed logged in
ERROR_CREATE_INSTANCE-100010error in creating instance
ERROR_EXCEPTION-100011error in exception
ERROR_DB_SELECT-100050error in selecting database
ERROR_DB_UPDATE-100051error in updating database
ERROR_DB_INSERT-100052error in inserting database
ERROR_DB_DELETE-100053error in deleting database
ERROR_DB_DROP-100054error in dropping database
ERROR_DB_TRANSACTION-100060error in transaction
ERROR_DB_TABLE_NAME-100065error in table name
ERROR_REQUEST_VIA_IP-100100bad request via ip request
ERROR_MO_NOT_ENOUGH_COINS-100200not enough coins
ERROR_MO_HIRE_OVERDUE-100201the hiring date is overdue
ERROR_MO_TRANSACTION_TYPE-100202error consume type
ERROR_NETWORK-100300error network
ERROR_JSON-100301error json
ERROR_JSON_ERRORID-100302error json.errorid
ERROR_JSON_ERRORDESC-100303error json.errordesc
ERROR_JSON_VDATA-100304error json.vdata

the range of user customized error codes

error defineserror coderemark
ERROR_USER_START10000start of user customized error id
ERROR_USER_END99999end of user customized error id

Cross-Origin Resource Sharing

use dekuan\vdata\CResponse;


$cResponse	= CResponse::GetInstance();

//
//	requests from *.domain1.com will be allowed
//
$cResponse->SetCorsDomains( [ '.domain1.com' ] );

//	...

Send Request to Server

1, Send GET request and receive VData response

use dekuan\vdata\CConst;
use dekuan\vdata\CRequest;


$cRequest	= CRequest::GetInstance();
$arrResponse	= [];
$nCall		= $cRequest->Get
(
	[
		'url'		=> 'http://api-account.yunkuan.org/api/login',
		'data'		=>
		[
			'u_name'	=> 'username',
			'u_pwd'		=> 'password',
			'u_keep'	=> 1
		],
		'version'	=> '1.0',
		'timeout'	=> 30,		//	timeout in seconds
		'cookie'	=> [],		//	array or string are both okay.
		'headers'	=> [],
	],
	$arrResponse
);
if ( CConst::ERROR_SUCCESS == $nCall &&
	$cRequest->IsValidVData( $arrResponse ) )
{
	//	arrResponse
	//		'errorid'	: error id
	//		'errordesc'	: error desc
	//		'vdata'		: virtual data
	//		'version'	: version of service
	//		'json'		: original json array
	print_r( $arrResponse );
}

2, Send POST request and receive VData response

use dekuan\vdata\CConst;
use dekuan\vdata\CRequest;


$cRequest	= CRequest::GetInstance();
$arrResponse	= [];
$nCall		= $cRequest->Post
(
	[
		'url'		=> 'http://api-account.yunkuan.org/api/login',
		'data'		=>
		[
			'u_name'	=> 'username',
			'u_pwd'		=> 'password',
			'u_keep'	=> 1
		],
		'version'	=> '1.0',
		'timeout'	=> 30,		//	timeout in seconds
		'cookie'	=> [],		//	array or string are both okay.
		'headers'	=> [],
	],
	$arrResponse
);
if ( CConst::ERROR_SUCCESS == $nCall &&
	$cRequest->IsValidVData( $arrResponse ) )
{
	//	arrResponse
	//		'errorid'	: error id
	//		'errordesc'	: error desc
	//		'vdata'		: virtual data
	//		'version'	: version of service
	//		'json'		: original json array
	print_r( $arrResponse );
}

3, Send GET/POST request and receive Raw response

use dekuan\vdata\CConst;
use dekuan\vdata\CRequest;


$cRequest	= CRequest::GetInstance();
$arrResponse	= [];
$nCall		= $cRequest->HttpRaw
(
	[
		'method'	=> 'POST',	//	'GET' / 'POST'
		'url'		=> 'http://api-account.yunkuan.org/api/login',
		'data'		=>
		[
			'u_name'	=> 'username',
			'u_pwd'		=> 'password',
			'u_keep'	=> 1
		],
		'version'	=> '1.0',
		'timeout'	=> 30,		//	timeout in seconds
		'cookie'	=> [],		//	array or string are both okay.
		'headers'	=> [],
	],
	$arrResponse
);
if ( CConst::ERROR_SUCCESS == $nCall &&
	$cRequest->IsValidRawResponse( $arrResponse ) )
{
	//	arrResponse	- Array
	//		'data'		: http data
	//		'status'	: status code
	//		'headers'	: response headers
	print_r( $arrResponse );
}

Respond to Client

1, Respond by json encoded string

use dekuan\vdata\CResponse;


$cResponse	= CResponse::GetInstance();

$cResponse->SetServiceName( 'Test of responding array VData' );
$cResponse->SetServiceUrl( 'http://www.ladep.cn/' );
$arrVData	= $cResponse->GetVDataString
(
	0,
	"error desc",
	[ "info" => "User customized info" ],
	$cResponse->GetDefaultVersion()
);

echo( $arrVData );

2, Respond by json array

use dekuan\vdata\CResponse;


$cResponse	= CResponse::GetInstance();

$cResponse->SetServiceName( 'Test of responding array VData' );
$cResponse->SetServiceUrl( 'http://www.ladep.cn/' );
$arrVData	= $cResponse->GetVDataArray
(
	0,
	"error desc",
	[ "info" => "User customized info" ],
	$cResponse->GetDefaultVersion()
);

print_r( $arrVData );

3, Respond by Laravel response instance

use dekuan\vdata\CResponse;


$cResponse	= CResponse::GetInstance();

$cResponse->SetServiceName( 'Test of responding array VData' );
$cResponse->SetServiceUrl( 'http://www.ladep.cn/' );
$nCall	= $cResponse->Send
(
	0,
	"error desc",
	[ "info" => "User customized info" ],
	$cResponse->GetDefaultVersion()
);

Testing

mocha tests/js/index.test.js