Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
- Upgrade to .NET 8.0
- Migrate from WebClient to HttpClient
  • Loading branch information
swhitley committed Dec 18, 2023
1 parent e7628bc commit 01526d7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 71 deletions.
14 changes: 7 additions & 7 deletions CLARiNET.csproj
Expand Up @@ -2,20 +2,20 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Authors>Whitley Media</Authors>
<PackageIcon>CLARiNET.ico</PackageIcon>
<PackageIconUrl />
<ApplicationIcon>CLARiNET.ico</ApplicationIcon>
<Version>1.1.2</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.39" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="6.0.0" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.55" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Extensions.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -29,5 +31,13 @@ public static class Extensions
if (count > 0)
yield return items.Take(count);
}


public static void BasicAuth(this HttpRequestMessage http, string username, string password)
{
var authenticationString = $"{username}:{password}";
var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString));
http.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64String);
}
}
}
16 changes: 14 additions & 2 deletions Photo.cs
Expand Up @@ -18,6 +18,9 @@ public static string Download(Options options, string file, string soapUrl)
string workerRef = Resources.Worker_Reference;
string result = "";
string path = Path.GetDirectoryName(file);
int batchErrors = 0;
int errors = 0;
int batchNum = 1;

Dictionary<string,string> ids = File.ReadLines(file).Select(line => line.Split(',')).ToDictionary(line => line[0], line => line.Count() > 1 ? line[1] : "");

Expand Down Expand Up @@ -72,19 +75,28 @@ public static string Download(Options options, string file, string soapUrl)
{
Console.WriteLine("\n\nError: " + ex3.Message);
Console.WriteLine("\n");
errors++;
}
}
}
}
else
{
Console.WriteLine("\n\nBatch {0:N0} Error: No data returned.\n\n{1}", batchNum, result);
Console.WriteLine("\n");
errors++;
}

}
catch(Exception batchEx)
{
Console.WriteLine("\n\nError: " + batchEx.Message);
Console.WriteLine("\n\nBatch {0:N0} Error: {1}", batchNum, batchEx.Message);
Console.WriteLine("\n");
batchErrors++;
}
batchNum++;
}
result = String.Format("Processed {0:N0} id{1}.", ids.Count(), ids.Count() == 1 ? "" : "s");
result = String.Format("Processed {0:N0} id{1}\nFile Errors: {2:N0}\nBatch Errors: {3:N0}\n", ids.Count(), ids.Count() == 1 ? "" : "s", errors, batchErrors);
}
catch (Exception ex)
{
Expand Down
137 changes: 75 additions & 62 deletions WDWebService.cs
Expand Up @@ -7,34 +7,37 @@
using System.Text;
using System.IO;
using HtmlAgilityPack;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Net.Http.Headers;
using static System.Net.WebRequestMethods;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Reflection.PortableExecutable;
using System.Xml.XPath;

namespace CLARiNET
{
public class WDWebService
{
static readonly HttpClient _client = new HttpClient();

public static byte[] CallRest(string tenant, string username, string password, string url, string method, byte[] data)
{
using (var webClient = new WebClient())
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
if (!String.IsNullOrEmpty(username))
{
webClient.Credentials = new NetworkCredential(username, password);
webClient.Headers.Add("X-Originator", "CLARiNET");
webClient.Headers.Add("X-Tenant", tenant);
if (method == WebRequestMethods.Http.Get)
{
return webClient.DownloadData(url);
}
else
{
return webClient.UploadData(url, data);
}
}
HttpRequestMessage http = new HttpRequestMessage();
http.Headers.Add("X-Originator", "CLARiNET");
http.Headers.Add("X-Tenant", tenant);
http.RequestUri = new Uri(url);
http.Method = new HttpMethod(method);
http.BasicAuth(username, password);

if (method != WebRequestMethods.Http.Get)
{
http.Content = new ByteArrayContent(data);
}
return null;

HttpResponseMessage response = _client.Send(http);
return response.Content.ReadAsByteArrayAsync().Result;

}

public static string WrapSOAP(string username, string password, string xmlBody)
Expand Down Expand Up @@ -120,10 +123,13 @@ public static string WrapSOAP(string username, string password, string xmlBody)
string html = "";
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

using (var webClient = new WebClient())
{
html = webClient.DownloadString(url);
}
HttpRequestMessage http = new HttpRequestMessage();
http.RequestUri = new Uri(url);


HttpResponseMessage response = _client.Send(http);
html = response.Content.ReadAsStringAsync().Result;

htmlDoc.LoadHtml(html);

HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//a[contains(@href, '.xsd')]");
Expand All @@ -144,60 +150,67 @@ public static string WrapSOAP(string username, string password, string xmlBody)

public static string GetServiceURL(string envURL, string tenant, string username, string password)
{
string result = "";

using (var webClient = new WebClient())
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
webClient.Credentials = new NetworkCredential(username + "@" + tenant, password);
result = webClient.DownloadString(envURL + "/cc-cloud-master/service-gateway");
}
HttpRequestMessage http = new HttpRequestMessage();
http.RequestUri = new Uri(envURL + "/cc-cloud-master/service-gateway");
http.BasicAuth(username + "@" + tenant, password);

HttpResponseMessage response = _client.Send(http);
return response.Content.ReadAsStringAsync().Result;

return result;
}

public static string CallAPI(string username, string password, string url, string xmlData)
{
try
{
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "text/xml; charset=utf-8");
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
webClient.Credentials = new NetworkCredential(username, password);
byte[] data = Encoding.UTF8.GetBytes(WDWebService.WrapSOAP(username, password, xmlData));
byte[] rData = webClient.UploadData(url, data);

return new XDeclaration("1.0", "UTF-8", null).ToString() + Environment.NewLine + XDocument.Parse(Encoding.UTF8.GetString(rData)).ToString() + Environment.NewLine;
}
}
catch (WebException webEx)
{
String responseFromServer = webEx.Message.ToString() + Environment.NewLine;
if (webEx.Response != null)
HttpRequestMessage http = new HttpRequestMessage();

http.RequestUri = new Uri(url);
http.Method = new HttpMethod(WebRequestMethods.Http.Post);
http.BasicAuth(username, password);


byte[] data = Encoding.UTF8.GetBytes(WDWebService.WrapSOAP(username, password, xmlData));
http.Content = new ByteArrayContent(data);
http.Content.Headers.Add("Content-Type", "text/xml; charset=utf-8");

HttpResponseMessage response = _client.Send(http);

if (!response.IsSuccessStatusCode)
{
using (WebResponse response = webEx.Response)
try
{
Stream dataRs = response.GetResponseStream();
using (StreamReader reader = new StreamReader(dataRs))
string result = response.Content.ReadAsStringAsync().Result;
var xDoc = XDocument.Parse(result);
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("wd", "urn:com.workday/bsvc");
if (xDoc != null)
{
try
{
responseFromServer += XDocument.Parse(reader.ReadToEnd());
}
catch
{
// ignore exception
}
result = xDoc.XPathSelectElement("//faultstring", ns).Value;
}
return result;
}
catch
{
// ignore exception
}
return null;

}

byte[] rData = response.Content.ReadAsByteArrayAsync().Result;
return new XDeclaration("1.0", "UTF-8", null).ToString() + Environment.NewLine + XDocument.Parse(Encoding.UTF8.GetString(rData)).ToString() + Environment.NewLine;

}
catch (HttpRequestException webEx)
{

string responseFromServer = webEx.Message.ToString() + Environment.NewLine;
return responseFromServer;
}
}


}
}

0 comments on commit 01526d7

Please sign in to comment.