Consume odata/web api d365fo and parsing json content
// Two types of content type url will have usually ,
//1. normal URL encocoding(means adding parameters in url link)
//2. multiform-data(values needs to specify on webforms to accesss data )
//Check RetailCommonWebAPI and do find references how they are consuming odata API
class ALMMenaiHRMSIntegrationService
{
str accessToken,responseJsONData;
RecordInsertList recIns;
public void getAuthToken()
{
RetailCommonWebAPI webApi;
str rawResponse;
RetailWebResponse response;
URL loginURL;
str contentType;
Map docFields = new Map(Types::String, Types::Class);
webApi = RetailCommonWebAPI::construct();
loginURL = "https://hrms.almajed4oud.com/menaitech/index.php/api/login/";
Map strFields = new Map(Types::String, Types::String);
strFields.insert("UserName", "ERPintegrationAdmins");
strFields.insert("PassWord", "yed*mZC7LA66");
strFields.insert("CompanyCode", "Almajed");
strFields.insert("BranchCode", "HQ");
response = webApi.makeFormPost(loginURL , strFields,docFields);
if(response.parmHttpStatus() != 200)
throw error("Couldnt authenticate");
rawResponse = response.parmData();
// Need to re-write the below code to get the token according our response
int len = strLen(rawResponse);
int tokenstartPos;
tokenstartPos = strScan(rawResponse,"token",1,len);
int tokenBrackStart = strFind(rawResponse,"[",tokenstartPos,len);
int tokenBrackEnd =strFind(rawResponse,"]",tokenstartPos,len);
accessToken = subStr(rawResponse,tokenBrackStart+2,tokenBrackEnd-tokenBrackStart-3);
}
public void getData()
{
RetailCommonWebAPI webApi;
str rawResponse;
RetailWebResponse response;
Map responseData;
URL loginURL;
str contentType;
MapEnumerator mapEnum;
Map docFields = new Map(Types::String, Types::Class);
webApi = RetailCommonWebAPI::construct();
loginURL = "http://hrms.almajed4oud.com/menaitech/index.php/api/getview";
Map strFields = new Map(Types::String, Types::String);
strFields.insert("company_code", "Almajed");
strFields.insert("branch_code", "HQ");
strFields.insert("View_Name","PayRoll_API_Integration");
strFields.insert("X", "PayRoll_API_Integration");//dummy data
response = webApi.makeFormPost(loginURL,strFields,docFields,@'Authorization: Bearer '+accessToken);
if(response.parmHttpStatus() != 200)
throw error("Couldnt authenticate");
rawResponse = response.parmData();
// responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse);
int len = strLen(rawResponse);
int tokenstartPos;
tokenstartPos = strScan(rawResponse,"Data",1,len);
int tokenBrackStart = strFind(rawResponse,"[",tokenstartPos,len);
int tokenBrackEnd =strFind(rawResponse,"]",tokenstartPos,len);
responseJsONData = subStr(rawResponse,tokenBrackStart,tokenBrackEnd-tokenBrackStart+1);
this.deserialize(responseJsONData);
}
public void runProcess()
{
ALMMenaiHRMSIntegrationServiceStagingTable deleteStaging;
ttsbegin;
delete_from deleteStaging;
ttscommit;
this.getAuthToken();
this.getData();
}
// To serialize json data to values
public void deserialize(str responseData)
{
str json;
List values = new List(Types::String);
ListEnumerator value;
json = responseData;
// Deserializing Json
values = FormJsonSerializer::deserializeCollection(classnum(List), json, Types::Class, classStr(ALMJSONParserContract));
value = values.getEnumerator();
recIns = new RecordInsertList(tableNum(ALMMenaiHRMSIntegrationServiceStagingTable));
while(value.moveNext())
{
ALMJSONParserContract JSONContractCurrent = value.current();
this.insertIntoStaging(JSONContractCurrent);
}
recIns.insertDatabase();
}
public void insertIntoStaging(ALMJSONParserContract _contract)
{
ALMMenaiHRMSIntegrationServiceStagingTable stagingTable;
stagingTable.Company_Code = _contract.parmCompany_Code();
stagingTable.Employee_ID = _contract.parmEmployee_ID();
stagingTable.Branch_Code = _contract.parmBranch_Code();
stagingTable.Employee_ID = _contract.parmEmployee_ID();
stagingTable.Employee_Name = _contract.parmEmployee_Name();
stagingTable.Birth_Date = _contract.parmBirth_Date();
stagingTable.Date_Of_Joining = _contract.parmDate_Of_Joining();
stagingTable.Position = _contract.parmPosition();
stagingTable.Position_Prefix = _contract.parmPosition_Prefix();
stagingTable.Site_Name = _contract.parmSite_Name();
stagingTable.Site_Prefix = _contract.parmSite_Prefix();
stagingTable.Department = _contract.parmDepartment();
stagingTable.Department_Prefix = _contract.parmDepartment_Prefix();
stagingTable.Section = _contract.parmSection();
stagingTable.Section_Prefix = _contract.parmSection_Prefix();
stagingTable.Category_Two_Name = _contract.parmCategory_Two_Name();
stagingTable.Category_Two_Prefix = _contract.parmCategory_Two_Prefix();
stagingTable.Project = _contract.parmProject();
stagingTable.Project_Prefix = _contract.parmProject_Prefix();
stagingTable.Basic_Salary = _contract.parmBasic_Salary();
stagingTable.Housing_Allowance = _contract.parmHousing_Allowance();
stagingTable.Transportation_Allowance = _contract.parmTransportation_Allowance();
stagingTable.Nature_of_work_allowance = _contract.parmNature_of_work_allowance();
stagingTable.Mobile_Allowance = _contract.parmMobile_Allowance();
stagingTable.Supervision_Allowance = _contract.parmSupervision_Allowance();
stagingTable.Fuel_Allowance = _contract.parmFuel_Allowance();
stagingTable.Fixed_Additional_Allowance = _contract.parmFixed_Additional_Allowance();
stagingTable.Acting_Allowance = _contract.parmActing_Allowance();
stagingTable.Additional_Basic = _contract.parmAdditional_Basic();
stagingTable.Additional_Housing = _contract.parmAdditional_Housing();
stagingTable.Additional_Transportation = _contract.parmAdditional_Transportation();
stagingTable.Car_Allowance = _contract.parmCar_Allowance();
stagingTable.Other_Allowance = _contract.parmOther_Allowance();
stagingTable.Driving_Allowance = _contract.parmDriving_Allowance();
stagingTable.Service_Allowance = _contract.parmService_Allowance();
stagingTable.Management_Allowance = _contract.parmManagement_Allowance();
stagingTable.Fixed_Transportation_Allowance = _contract.parmFixed_Transportation_Allowance();
stagingTable.Termination_Date = _contract.parmTermination_Date();
recIns.add(stagingTable);
}
}
Comments
Post a Comment