Skip to content

Commit

Permalink
vtwsclib version 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
nilay-automatesmb committed Jun 6, 2019
1 parent 36ffba7 commit 0e2ca1a
Show file tree
Hide file tree
Showing 88 changed files with 15,938 additions and 20 deletions.
266 changes: 266 additions & 0 deletions Vtiger/WSClient.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/*+***********************************************************************************
* The contents of this file are subject to the Vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: Vtiger CRM Open Source
* The Initial Developer of the Original Code is Vtiger.
* Portions created by Vtiger are Copyright (C) www.vtiger.com
* All Rights Reserved.
*************************************************************************************/

package {

import flash.display.MovieClip;
import JSON;
import flash.utils.Dictionary;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.geom.Matrix3D;

import com.adobe.crypto.MD5;

public class WSClient extends MovieClip {
private var _serviceBase:String = 'webservice.php';
private var _serviceURL:String;

private var _sessionId:String;
private var _serviceToken:String;
private var _serverTime:String;
private var _expireTime:String;

private var _userId:String;
private var _vtigerVersion:String;

private var _lastError:String = "";


public function WSClient(url:String) {
if(url.substring(url.length-1) != '/') {
_serviceURL = url+"/"+_serviceBase;
} else {
_serviceURL = url+_serviceBase;
}
}

private function _doGetRequest(params:URLVariables, callBack:Function) {
var request:URLRequest = new URLRequest(_serviceURL);

request.method = URLRequestMethod.GET;
request.data = params;

var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, callBack);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, _ioErrorHandler);

try {
urlLoader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}
}

private function _doPostRequest(params:Object, callBack:Function):Object {
var request:URLRequest = new URLRequest(_serviceURL);

request.method = URLRequestMethod.POST;
request.data = params;

var urlLoader:URLLoader = new URLLoader();

urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, callBack);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, _ioErrorHandler);

try {
urlLoader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}

return true;
}

private function _ioErrorHandler(event:Event) {
trace(event.target.data);
}

public function doLogin(username:String, accessKey:String, callBack:Function=null):Boolean {

var map:URLVariables = new URLVariables();
map.operation = "getchallenge";
map.username = username;

var response:Object = _doGetRequest(map, function(event:Event) {
var response:Object = JSON.parse(event.target.data);

if(_hasError(response)) {
trace(response["error"]);
return false;
}

_serverTime = response["result"]["serverTime"];
_expireTime = response["result"]["expireTime"];
_serviceToken = response["result"]["token"];

var params:URLVariables = new URLVariables();
params.operation = "login";
params.username = username;
params.accessKey = MD5.hash(_serviceToken+accessKey);

_doPostRequest(params, function(event:Event){
var loginResponse:Object = JSON.parse(event.target.data);

var error:Boolean = false;
if(_hasError(loginResponse)) {
error = true;
}

_sessionId = loginResponse["result"]["sessionName"];
_userId = loginResponse["result"]["userId"];
_vtigerVersion = loginResponse["result"]["vtigerVersion"];

callBack(error);
});

});

return true;
}

private function __checkLogin() {
if(_userId) return true;
return false;
}

private function _hasError(response:Object):Boolean {
if(response["success"] == false && response.hasOwnProperty("error")) {
_lastError = response["error"]["message"];
return true;
}
return false;
}

private function _handleCallBack(callBack:Function, event:Event) {
trace(event.target.data);
var response:Object = JSON.parse(event.target.data);
var error = false;
if(_hasError(response)) {
error = true;
}
callBack(response["result"], error);
}



public function getUser() {
return _userId;
}

public function getLastError():String {
return _lastError;
}

public function doQuery(query:String, callBack:Function) {

if(query.indexOf(';') == -1) query += ';';

var params:URLVariables = new URLVariables();
params.operation = "query";
params.sessionName = _sessionId;
params.query = query;

_doGetRequest(params, function(event:Event) {
_handleCallBack(callBack, event);
});
}

public function getListTypes(callBack:Function) {
var params:URLVariables = new URLVariables();
params.operation = "listtypes";
params.sessionName = _sessionId;

_doGetRequest(params, function(event:Event) {
_handleCallBack(callBack, event);
});
}

public function doDescribe(module:String, callBack:Function) {
var params:URLVariables = new URLVariables();
params.operation = "describe";
params.sessionName = _sessionId;
params.elementType = module;

_doGetRequest(params, function(event:Event) {
_handleCallBack(callBack, event);
});
}


public function doRetrieve(record:String, callBack:Function) {
var params:URLVariables = new URLVariables();
params.operation = "retrieve";
params.sessionName = _sessionId;
params.id = record;

_doGetRequest(params, function(event:Event) {
_handleCallBack(callBack, event);
});
}

public function doCreate(module:String, data:Object, callBack:Function) {

if(!data.hasOwnProperty("assigned_user_id")) {
data["assigned_user_id"] = _userId;
}

var params:URLVariables = new URLVariables();
params.operation = "create";
params.sessionName = _sessionId;
params.elementType = module;
params.element = JSON.stringify(data);

_doPostRequest(params, function(event:Event){
_handleCallBack(callBack, event);
});
}

public function doInvoke(callBack:Function, method:String, params:Object=null, postType:String="POST") {
var requestData:URLVariables = new URLVariables();
requestData.operation = method;
requestData.sessionName = _sessionId;

for(var key:String in params) {
if(params[key] != '') {
requestData[key] = params[key];
}
}

if(postType == "POST") {
_doPostRequest(requestData, function(event:Event) {
_handleCallBack(callBack, event);
});
} else {
_doGetRequest(requestData, function(event:Event){
_handleCallBack(callBack, event);
});
}
}
}
}

//Usage
/*var client:Vtiger_WSClient = new Vtiger_WSClient("PATH_TO_YOUR_VTIGER");
client.doLogin("YOUR_USERNAME","YOUR_ACCESSKEY", function(){
client.getListTypes(function(data:Object, error:Boolean){
if(!error) {
trace(data.toString());
} else {
trace(client.getLastError());
}
});
});*/
8 changes: 8 additions & 0 deletions Vtiger/WSClient.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*+***********************************************************************************
* The contents of this file are subject to the Vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: Vtiger CRM Open Source
* The Initial Developer of the Original Code is Vtiger.
* Portions created by Vtiger are Copyright (C) www.vtiger.com
* All Rights Reserved.
*************************************************************************************/
var Vtiger_WSClient = function(url) {
this._servicebase = 'webservice.php';
// TODO: Format the url before appending servicebase
Expand Down
30 changes: 19 additions & 11 deletions Vtiger/WSClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/*+***********************************************************************************
* The contents of this file are subject to the Vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: Vtiger CRM Open Source
* The Initial Developer of the Original Code is Vtiger.
* Portions created by Vtiger are Copyright (C) www.vtiger.com
* All Rights Reserved.
*************************************************************************************/

require_once('vtwsclib/Vtiger/Net/HTTP_Client.php');
require_once('vtwsclib/Vtiger/WSVersion.php');
Expand Down Expand Up @@ -34,7 +42,7 @@ class Vtiger_WSClient {
/**
* Constructor.
*/
function __construct($url) {
function __construct($url) {
$this->_serviceurl = $this->getWebServiceURL($url);
$this->_client = new Vtiger_HTTP_Client($this->_serviceurl);
}
Expand Down Expand Up @@ -144,7 +152,7 @@ function toJSONString($input) {
function doLogin($username, $vtigerUserAccesskey) {
// Do the challenge before login
if($this->__doChallenge($username) === false) return false;

$postdata = Array(
'operation' => 'login',
'username' => $username,
Expand All @@ -171,7 +179,7 @@ function doQuery($query) {
$this->__checkLogin();

// Make sure the query ends with ;
$query = trim($query);
$query = trim($query);
if(strripos($query, ';') != strlen($query)-1) $query .= ';';

$getdata = Array(
Expand Down Expand Up @@ -212,14 +220,14 @@ function doListTypes() {
$resultdata = $this->_client->doGet($getdata, true);
if($this->hasError($resultdata)) {
return false;
}
}
$modulenames = $resultdata[result][types];

$returnvalue = Array();
foreach($modulenames as $modulename) {
$returnvalue[$modulename] =
$returnvalue[$modulename] =
Array ( 'name' => $modulename );
}
}
return $returnvalue;
}

Expand All @@ -238,7 +246,7 @@ function doDescribe($module) {
$resultdata = $this->_client->doGet($getdata, true);
if($this->hasError($resultdata)) {
return false;
}
}
return $resultdata[result];
}

Expand All @@ -257,7 +265,7 @@ function doRetrieve($record) {
$resultdata = $this->_client->doGet($getdata, true);
if($this->hasError($resultdata)) {
return false;
}
}
return $resultdata[result];
}

Expand All @@ -282,7 +290,7 @@ function doCreate($module, $valuemap) {
$resultdata = $this->_client->doPost($postdata, true);
if($this->hasError($resultdata)) {
return false;
}
}
return $resultdata[result];
}

Expand All @@ -296,7 +304,7 @@ function doCreate($module, $valuemap) {
function doInvoke($method, $params = null, $type = 'POST') {
// Perform re-login if required
$this->__checkLogin();

$senddata = Array(
'operation' => $method,
'sessionName' => $this->_sessionid
Expand All @@ -320,6 +328,6 @@ function doInvoke($method, $params = null, $type = 'POST') {
return false;
}
return $resultdata[result];
}
}
}
?>
Loading

0 comments on commit 0e2ca1a

Please sign in to comment.