Added models for qb request and responses to communicate with web app.

This commit is contained in:
Patrick Fic
2020-06-02 17:47:52 -07:00
parent 6a035dcdf4
commit fe4cb24742
4 changed files with 120 additions and 11 deletions

View File

@@ -327,6 +327,8 @@
<Compile Include="Models\DTO_Base.cs" />
<Compile Include="Models\DTO_QueueItem.cs" />
<Compile Include="Models\Monitor.cs" />
<Compile Include="Models\QbRequestItem.cs" />
<Compile Include="Models\QbResponseItem.cs" />
<Compile Include="Models\QbXmlResponse.cs" />
<Compile Include="Properties\Resources.es-MX.Designer.cs">
<DependentUpon>Resources.es-MX.resx</DependentUpon>

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace BodyshopPartner.Models
{
public class QbRequestItem : DTO_Base
{
public QbRequestItem()
{
}
private string _id;
[JsonProperty("id")]
public string Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private string[] _okStatusCodes;
[JsonProperty("OkStatusCodes")]
public string[] OkStatusCodes
{
get { return _okStatusCodes; }
set { SetProperty(ref _okStatusCodes, value); }
}
private string _qbxml;
[JsonProperty("qbxml")]
public string QbXML
{
get { return _qbxml; }
set { SetProperty(ref _qbxml, value); }
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace BodyshopPartner.Models
{
public class QbResponseItem : DTO_Base
{
public QbResponseItem()
{
}
private string _id;
[JsonProperty("id")]
public string Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private bool _success;
[JsonProperty("success")]
public bool Success
{
get { return _success; }
set { SetProperty(ref _success, value); }
}
private string _errorMessage;
[JsonProperty("errorMessage")]
public string ErrorMessage
{
get { return _errorMessage; }
set { SetProperty(ref _errorMessage, value); }
}
}
}

View File

@@ -8,6 +8,7 @@ using System.Xml.Linq;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BodyshopPartner.Models;
namespace BodyshopPartner.Utils
{
@@ -38,26 +39,48 @@ namespace BodyshopPartner.Utils
private static void HandleQbPost(System.Net.HttpListenerRequest req, System.Net.HttpListenerResponse res)
{
logger.Debug("/qb/ - POST");
//Input will be an array of XMLs.
string[] requestXMLstrings = ParseBodyToArray(req);
logger.Trace("/qb/ - POST");
hlog("Received QuickBooks Receivable request.");
//Input will be an array of objects containing XMLs.
List<QbRequestItem> AllRequests = ParseRequest(req);
List<QbResponseItem> HttpResponse = new List<QbResponseItem>();
// string requestXMLstring = ParseBodyToString(req);
foreach (var requestXMLstring in requestXMLstrings)
foreach (QbRequestItem request in AllRequests)
{
XDocument requestXMLdoc = ParseStringToXml(requestXMLstring);
XDocument response = XDocument.Parse(QuickBooksInterop.ProcessQBXmlRequest(requestXMLstring));
Models.QbXmlResponse ResponseStatus = QuickBooksInterop.ParseResponseXml(response);
logger.Info(response.ToString());
logger.Info(ResponseStatus.ToString());
XDocument response = XDocument.Parse(QuickBooksInterop.ProcessQBXmlRequest(request.QbXML));
QbXmlResponse ResponseStatus = QuickBooksInterop.ParseResponseXml(response);
logger.Trace(response.ToString());
logger.Debug(ResponseStatus.ToString());
hlog(ResponseStatus.ToString());
QbResponseItem r = new QbResponseItem()
{
Id = request.Id,
Success = ResponseStatus.StatusCode == "0" || request.OkStatusCodes.Contains(ResponseStatus.StatusCode),
ErrorMessage = ResponseStatus.StatusMessage
};
HttpResponse.Add(r);
}
hlog("Completed QuickBooks request.");
res.WithCORS().Close();
res.WithCORS().AsText(JsonConvert.SerializeObject(HttpResponse));
}
public static List<QbRequestItem> ParseRequest(System.Net.HttpListenerRequest req)
{
System.IO.Stream body = req.InputStream;
System.Text.Encoding encoding = req.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
string s = reader.ReadToEnd();
body.Close();
reader.Close();
return JsonConvert.DeserializeObject<List<QbRequestItem>>(s);
}
public static string[] ParseBodyToArray(System.Net.HttpListenerRequest req)