API Calls

Class library usage, coding and language questions.
pmaladi
Posts: 10
Joined: Thu Aug 17, 2017 9:42 pm

API Calls

Post by pmaladi » Mon Oct 22, 2018 9:09 pm

this are code examples which we use.. might not be helpful .. i just kept here for my reference.

public void editXML()
{
XmlDocument doc = new XmlDocument();
OCoELibrary OL=new OCoELibrary();
WinSCPLib wnScp=new WinSCPLib();
string path = OCoEValues.RequestXML;
doc.Load(OCoEValues.RequestXML);
//Report.Success(OCoEValues.RequestXML);
XmlNodeList elemList = null;
string attrVal,pegaSHRequired="";
try{
elemList = doc.GetElementsByTagName("cc:entry");
// this is must- plz dont delete it
string tryoneValue = elemList[0].Attributes["name"].Value;
}
catch{
elemList = doc.GetElementsByTagName("entry");
string tryoneValue = elemList[0].Attributes["name"].Value;
}

for (int i = 0; i<= elemList.Count; i++)
{
attrVal = elemList[i].Attributes["name"].Value;
if(attrVal.Contains("Extract"))
{
if(attrVal=="ExtractFile")
{
elemList[i].Attributes["value"].Value= OL.GetFromApplicationPath(OCoEValues.AppName,"InputFolder")+OCoEValues.ExtractFileName;
//Report.Info(elemList[i].Attributes["value"].Value);
pegaSHRequired=OCoEValues.PEGASpecialHandlingFlag;
break;
}
else if(attrVal=="ExtractFileName")
{
//Report.Info("File name : "+OCoEValues.ExtractFileName);
elemList[i].Attributes["value"].Value=OCoEValues.ExtractFileName;
pegaSHRequired=OCoEValues.PEGASpecialHandlingFlag;
break;
}
}
}


/// <summary>
/// to update pega Special handling flag
/// </summary>
try{
for (int j = 0; j<= elemList.Count; j++)
{
attrVal = elemList[j].Attributes["name"].Value;
if(attrVal == "HoldForReviewFlag")
{
if(pegaSHRequired=="Y"||pegaSHRequired=="Yes")
{
elemList[j].Attributes["value"].Value="true";
break;
}
else
{
elemList[j].Attributes["value"].Value="false";
break;
}
}
} }catch{Report.Info("No Hold for Review Flag");}

doc.Save(OCoEValues.RequestXML);
}

public void CallSOAPWebService()
{
if(!OCoEValues.continueExecution)
{
return;
}
string path = OCoEValues.RequestXML;
string xmlString = System.IO.File.ReadAllText(OCoEValues.RequestXML);
Report.Success("XML Request: ",xmlString);
string url;
switch(OCoEValues.AppName)
{

case "TIMDSDS":
url = OCoEValues.SOAPuiEP+"/cc-channels/action/soap";
break;
default:
url = OCoEValues.SOAPuiEP+"/cc-channels/action/raw";
break;
}

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xmlString);

req.Method = "POST";
req.ContentType = "text/xml;charset=utf-8";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();

StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string responseXML = sr.ReadToEnd();

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(responseXML);
xdoc.Save(responseXMLPath);
Report.Info("Request","URL Used: "+ url);
Report.Success(responseXML);

sr.Close();
res.Close();

}


public void CallRESTPOSTWebService()
{

string xmlString = System.IO.File.ReadAllText(OCoEValues.RequestXML);

Report.Info("XML Request",xmlString);

string url;
switch(OCoEValues.AppName)
{
case "ANOC":
url = OCoEValues.SOAPuiEP+"/cc-channels/action/soap";
break;
default:
url = OCoEValues.SOAPuiEP+"/cc-channels/action/raw";
break;
}

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xmlString);
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string responseXML = sr.ReadToEnd();

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(responseXML);
xdoc.Save(responseXMLPath);
Report.Success(responseXML);

sr.Close();
res.Close();

}




public string readJobID()
{
if(!OCoEValues.continueExecution)
{
return null;
}
var doc = new XmlDocument();
doc.Load(responseXMLPath);
string job_ID = "";
XmlNodeList elemList = doc.GetElementsByTagName("attribute");
for (int i = 0; i<= elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["name"].Value;
if(attrVal=="job_id")
{
job_ID = elemList[i].Attributes["value"].Value;
break;
}
}
return job_ID;
}

private void VerifyMem360ActivityAction(string expectedActivityAction)
{
using (StreamReader r = new StreamReader(responseJsonPath))
{
string json = r.ReadToEnd();
List<LibraryFiles.Member360Pojo> items = JsonConvert.DeserializeObject<List<LibraryFiles.Member360Pojo>>(json);

int k = items.Count;

string memberID = items[k-1].memberId;
string actualActivityAction = items[k-1].activityAction;
string deliveryType = items[k-1].deliveryType;
string documentType = items[k-1].documentType;

if(actualActivityAction.contains(expectedActivityAction)){
Report.Success("Member with Member ID: "+memberID+", delivery type :"+deliveryType+", document Type :"+documentType+" , has following Activity Action: "+actualActivityAction);
}
else{
Report.Error("Expected activity Code: "+expectedActivityAction+" is not matching: "+actualActivityAction);
}
}
}


private string getValue(XmlNode Xnode, string attributeName)
{
XmlNode innerNode = Xnode.SelectSingleNode(attributeName);
string attributeValue = innerNode.InnerText;
return attributeValue;
}


private void triggerMem360(string memberID)
{
if(!OCoEValues.continueExecution)
{
return;
}
string dateToday = System.DateTime.Now.ToString("yyyy-MM-dd");
string dateTomorrow = System.DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
string WEBSERVICE_URL = OCoEValues.Mem360+memberID+"&startDate="+dateToday+"T00:00:00.000Z&endDate="+dateTomorrow+"T00:00:00.000Z";
try
{
Report.Info("Get Request url: "+WEBSERVICE_URL);
var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
if (webRequest != null)
{
webRequest.Method = "GET";
//webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
webRequest.Headers.Add("Authorization", "Basic MzlhYTY3ZTktYTM2Yi00YzMwLWI3ODYtZjFiM2E5NDMzYTQ0Og==");
using (Stream s = webRequest.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
var jsonResponse = sr.ReadToEnd() ;
var formattedJsonResponse = JToken.Parse(jsonResponse).ToString(Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(responseJsonPath, formattedJsonResponse);
Report.Info("Path of Json response: "+responseJsonPath);
}
}
}
}
catch (System.Exception ex){
Report.Warn(ex.ToString());
}
}


private void updateValue(XmlNode Xnode, string attributeName,string attValue)
{
XmlNode innerNode = Xnode.SelectSingleNode(attributeName);
innerNode.InnerText=attValue;
}