Adding
This commit is contained in:
+258
@@ -0,0 +1,258 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
using Serilog;
|
||||
|
||||
namespace LCU.API.Internal.RepoApi.Repos.WebServices.AutoIMS
|
||||
{
|
||||
public class AutoImsWebService : IAutoImsWebService
|
||||
{
|
||||
private string URL = "https://wstls.autoims.com/ClientWebservice?clientId={0}";
|
||||
private readonly AutoImsServiceConfig _config;
|
||||
|
||||
public AutoImsWebService(AutoImsServiceConfig config)
|
||||
{
|
||||
_config = config;
|
||||
URL = string.Format(URL, config.ClientId);
|
||||
}
|
||||
|
||||
public static string GetFormattedVIN(string vin)
|
||||
{
|
||||
return vin.Trim().PadLeft(17, '0').ToUpper();
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> RemoveFromAuction(string vin, string clientStockNumber)
|
||||
{
|
||||
WebServiceResponse webServiceResponse = null;
|
||||
|
||||
URL = string.Format(URL + "&vin={0}&clientStockNum={1}", vin, clientStockNumber);
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
using (var req = new HttpRequestMessage(HttpMethod.Delete, URL))
|
||||
{
|
||||
string authInfo = String.Format("{0}:{1}", _config.Login, _config.Password);
|
||||
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
|
||||
req.Headers.Add("Authorization", "Basic " + authInfo);
|
||||
req.Headers.Remove("Expect");
|
||||
req.Headers.Add("Expect", "");
|
||||
|
||||
using (HttpResponseMessage resp = await client.SendAsync(req))
|
||||
{
|
||||
//resp.EnsureSuccessStatusCode();
|
||||
|
||||
var cont = await resp.Content.ReadAsStringAsync();
|
||||
webServiceResponse = DeserializeResponse<WebServiceResponse>(cont);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return webServiceResponse;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> PostVehicleAsync<T>(T vehicle)
|
||||
{
|
||||
var xml = Serialize<T>(vehicle);
|
||||
WebServiceResponse WebServiceResponse = null;
|
||||
|
||||
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
using (var content = new StringContent(xml))
|
||||
{
|
||||
content.Headers.Remove("Content-Type");
|
||||
content.Headers.Add("Content-Type", "application/xml");
|
||||
|
||||
using (var req = new HttpRequestMessage(HttpMethod.Post, URL))
|
||||
{
|
||||
string authInfo = String.Format("{0}:{1}", _config.Login, _config.Password);
|
||||
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
|
||||
req.Headers.Add("Authorization", "Basic " + authInfo);
|
||||
req.Headers.Remove("Expect");
|
||||
req.Headers.Add("Expect", "");
|
||||
req.Content = content;
|
||||
|
||||
using (HttpResponseMessage resp = await client.SendAsync(req))
|
||||
{
|
||||
var cont = await resp.Content.ReadAsStringAsync();
|
||||
WebServiceResponse = DeserializeResponse<WebServiceResponse>(cont);
|
||||
|
||||
// 6 is duplicate.
|
||||
if (WebServiceResponse.Status.Code == 6)
|
||||
{
|
||||
Log.Information($"Auto Ims Status Code: { WebServiceResponse.Status.Code}, {WebServiceResponse.Status.Description}");
|
||||
WebServiceResponse.Status.Code = 1;
|
||||
WebServiceResponse.Status.Description = "Notification Already Sent To AutoIMS";
|
||||
}
|
||||
else if (WebServiceResponse.Status.Code != 1)
|
||||
{
|
||||
throw new Exception("Auto Ims Status Code: " + WebServiceResponse.Status.Code + " " + string.Join(",",
|
||||
WebServiceResponse.Status.ValidationError.Select(x =>
|
||||
x.Message)));
|
||||
}
|
||||
|
||||
//resp.EnsureSuccessStatusCode();
|
||||
if (resp.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
throw new Exception("Status Code: " + resp.StatusCode + " " + string.Join(",",
|
||||
WebServiceResponse.Status.ValidationError.Select(x =>
|
||||
x.Message)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return WebServiceResponse;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> UpdateAuctionIdentifyingValuesAsync(string customerNum,
|
||||
string vin,
|
||||
string auctionAssignmentId)
|
||||
{
|
||||
VehicleIdentiferUpdate veh = new VehicleIdentiferUpdate();
|
||||
veh.ClientAccountNumber = customerNum;
|
||||
veh.VIN = vin;
|
||||
veh.ClientField1 = auctionAssignmentId;
|
||||
|
||||
var response = await PostVehicleAsync<VehicleIdentiferUpdate>(veh);
|
||||
response.AuctionCollateralVin = vin;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> AddAuctionNote(string vin, string customerNumber, string note,
|
||||
string writer)
|
||||
{
|
||||
WebServiceResponse response = null;
|
||||
VehicleNote vehicle = new VehicleNote();
|
||||
vehicle.VIN = vin;
|
||||
|
||||
//do not include and AutoIMS will timestamp.
|
||||
//vehicle.Note.CreationDate = String.Empty;// DateTime.Now.ToString("yyyy-MM-dd");
|
||||
vehicle.Note.NoteText = note;
|
||||
vehicle.Note.Writer = writer;
|
||||
vehicle.Note.Private = false;
|
||||
|
||||
response = await PostVehicleAsync<VehicleNote>(vehicle);
|
||||
response.AuctionCollateralVin = vin;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> RemoveAuction(string VIN, string customerNumber)
|
||||
{
|
||||
WebServiceResponse response = await RemoveFromAuction(VIN, customerNumber);
|
||||
response.AuctionCollateralVin = VIN;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> Redeem(string VIN)
|
||||
{
|
||||
WebServiceResponse response = null;
|
||||
RedeemVehicle vehicle = new RedeemVehicle();
|
||||
vehicle.VIN = VIN;
|
||||
vehicle.AuctionAssignment.RedeemVehicle = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
|
||||
response = await PostVehicleAsync<RedeemVehicle>(vehicle);
|
||||
response.AuctionCollateralVin = VIN;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> TitleSent(string VIN)
|
||||
{
|
||||
WebServiceResponse response = null;
|
||||
VehicleTitleSent vehicle = new VehicleTitleSent();
|
||||
vehicle.VIN = VIN;
|
||||
vehicle.AuctionAssignment.TitleSent = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
|
||||
response = await PostVehicleAsync<VehicleTitleSent>(vehicle);
|
||||
response.AuctionCollateralVin = VIN;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<WebServiceResponse> PlaceHold(string VIN, int holdCode, DateTime holdUntil)
|
||||
{
|
||||
WebServiceResponse response = null;
|
||||
VehicleHold vehicle = new VehicleHold();
|
||||
vehicle.VIN = VIN;
|
||||
vehicle.AuctionAssignment.SaleHold.HoldUntil = holdUntil.ToString("yyyy-MM-dd");
|
||||
vehicle.AuctionAssignment.SaleHold.HoldStatus = holdCode;
|
||||
|
||||
response = await PostVehicleAsync<VehicleHold>(vehicle);
|
||||
response.AuctionCollateralVin = VIN;
|
||||
return response;
|
||||
}
|
||||
|
||||
public string GetCollateralImage(string conditionReporUrl)
|
||||
{
|
||||
string html = String.Empty;
|
||||
string imageLink = String.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
var response = client.GetAsync(conditionReporUrl).Result;
|
||||
|
||||
html = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
var aTag = html.Substring(html.IndexOf("pic_0", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
var imgTag = aTag.Substring(aTag.IndexOf("addpic(", StringComparison.OrdinalIgnoreCase) + 7);
|
||||
|
||||
//var src = imgTag.Substring(imgTag.IndexOf("src=\"", StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
imageLink = imgTag.Substring(0, imgTag.IndexOf(",", StringComparison.OrdinalIgnoreCase))
|
||||
.Replace("'", "").Replace("\"", "");
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
imageLink = null;
|
||||
}
|
||||
|
||||
return imageLink;
|
||||
}
|
||||
|
||||
private static string Serialize<T>(T vehicle)
|
||||
{
|
||||
XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
|
||||
|
||||
string xml = "";
|
||||
|
||||
using (var sww = new StringWriter())
|
||||
{
|
||||
using (XmlWriter writer = XmlWriter.Create(sww))
|
||||
{
|
||||
xsSubmit.Serialize(writer, vehicle);
|
||||
xml = sww.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return xml;
|
||||
}
|
||||
|
||||
private T DeserializeResponse<T>(string xml)
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream(StringToByteArrayUtf8(xml)))
|
||||
{
|
||||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
|
||||
|
||||
using (StreamReader xmlStreamReader = new StreamReader(memoryStream, Encoding.UTF8))
|
||||
{
|
||||
return (T) xmlSerializer.Deserialize(xmlStreamReader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] StringToByteArrayUtf8(string value)
|
||||
{
|
||||
UTF8Encoding encoding = new UTF8Encoding();
|
||||
return encoding.GetBytes(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user