Bumped package version to 1.5.9, exposed a parameter in the init method to allow a custom message handler to be used with the client for C#.

This commit is contained in:
MattMo 2023-06-01 06:29:39 -07:00
parent 22ff3c1312
commit efd744974d
4 changed files with 129 additions and 40 deletions

View File

@ -1,3 +1,4 @@
//Generated using MontoyaTech.Rest.Net
using System;
using System.Net;
using System.Net.Http;
@ -9,7 +10,7 @@ public class Client
public CookieContainer CookieContainer;
public HttpClientHandler ClientHandler;
public HttpMessageHandler MessageHandler;
public HttpClient HttpClient;
@ -19,7 +20,7 @@ public class Client
public StreamApi Stream;
public Client(string baseUrl)
public Client(string baseUrl, HttpMessageHandler handler = null)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentException("baseUrl must not be null or whitespace.");
@ -31,15 +32,20 @@ public class Client
this.CookieContainer = new CookieContainer();
this.ClientHandler = new HttpClientHandler()
if (handler == null)
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = this.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
handler = new HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = this.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
}
this.HttpClient = new HttpClient(this.ClientHandler);
this.MessageHandler = handler;
this.HttpClient = new HttpClient(handler);
this.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
@ -67,7 +73,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -88,7 +94,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -109,7 +115,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/compress");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -130,7 +136,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/file/compress");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -161,7 +167,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -184,7 +190,7 @@ public class Client
message.Content = new StringContent(JsonConvert.SerializeObject(request));
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
@ -194,7 +200,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -215,7 +221,7 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/role");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -250,7 +256,7 @@ public class Client
message.Content = new StreamContent(request);
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
@ -260,15 +266,13 @@ public class Client
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/download");
var response = this.Client.HttpClient.Send(message);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var stream = new System.IO.MemoryStream();
response.Content.ReadAsStream().CopyTo(stream);
stream.Seek(0, System.IO.SeekOrigin.Begin);
response.Content.CopyToAsync(stream).GetAwaiter().GetResult();
return stream;
}
@ -282,6 +286,18 @@ public class Client
public class IncludedType
{
public int Test;
public IncludedType() { }
public IncludedType(IncludedType instance)
{
this.Test = instance.Test;
}
public IncludedType(int Test = 0)
{
this.Test = Test;
}
}
public class UserDto : BaseUser
@ -292,23 +308,79 @@ public class Client
public System.Collections.Generic.List<string> List;
public System.String[] Array;
public ulong Property { get; set; }
public UserDto() { }
public UserDto(UserDto instance)
{
this.MachineType = instance.MachineType;
this.Name = instance.Name;
this.List = instance.List;
this.Array = instance.Array;
this.Property = instance.Property;
}
public UserDto(System.PlatformID MachineType = 0, string Name = null, System.Collections.Generic.List<string> List = null, System.String[] Array = null, ulong Property = 0)
{
this.MachineType = MachineType;
this.Name = Name;
this.List = List;
this.Array = Array;
this.Property = Property;
}
}
public class BaseUser
{
public string Id;
public char FirstInitial;
public System.Collections.Generic.List<Permission> Permissions;
public UserRole Role { get; set; }
public BaseUser() { }
public BaseUser(BaseUser instance)
{
this.Id = instance.Id;
this.FirstInitial = instance.FirstInitial;
this.Permissions = instance.Permissions;
this.Role = instance.Role;
}
public BaseUser(string Id = null, char FirstInitial = '\0', System.Collections.Generic.List<Permission> Permissions = null, UserRole Role = 0)
{
this.Id = Id;
this.FirstInitial = FirstInitial;
this.Permissions = Permissions;
this.Role = Role;
}
public class Permission
{
public string Name;
public Types Type;
public Permission() { }
public Permission(Permission instance)
{
this.Name = instance.Name;
this.Type = instance.Type;
}
public Permission(string Name = null, Types Type = 0)
{
this.Name = Name;
this.Type = Type;
}
public enum Types : int
{
Read = 0,

View File

@ -10,11 +10,11 @@ public class StaticClient
public static CookieContainer CookieContainer;
public static HttpClientHandler ClientHandler;
public static HttpMessageHandler MessageHandler;
public static HttpClient HttpClient;
public static void Init(string baseUrl)
public static void Init(string baseUrl, HttpMessageHandler handler = null)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentException("baseUrl must not be null or whitespace.");
@ -26,15 +26,20 @@ public class StaticClient
StaticClient.CookieContainer = new CookieContainer();
StaticClient.ClientHandler = new HttpClientHandler()
if (handler == null)
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = StaticClient.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
handler = new HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = StaticClient.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
}
StaticClient.HttpClient = new HttpClient(StaticClient.ClientHandler);
StaticClient.MessageHandler = handler;
StaticClient.HttpClient = new HttpClient(handler);
StaticClient.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.5.8</Version>
<Version>1.5.9</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -56,9 +56,9 @@ namespace MontoyaTech.Rest.Net
//Create the client handler field
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static HttpClientHandler ClientHandler;");
writer.WriteBreak().WriteLine("public static HttpMessageHandler MessageHandler;");
else
writer.WriteBreak().WriteLine("public HttpClientHandler ClientHandler;");
writer.WriteBreak().WriteLine("public HttpMessageHandler MessageHandler;");
//Create the http client field
if (this.StaticCode)
@ -74,7 +74,7 @@ namespace MontoyaTech.Rest.Net
//Create the client constructor or init method
if (this.StaticCode)
{
writer.WriteBreak().WriteLine("public static void Init(string baseUrl)").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("public static void Init(string baseUrl, HttpMessageHandler handler = null)").WriteLine("{").Indent();
//Make sure the base url isn't null or whitespace
writer.WriteBreak().WriteLine("if (string.IsNullOrWhiteSpace(baseUrl))");
@ -91,16 +91,22 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine($"{this.ClientName}.CookieContainer = new CookieContainer();");
//Init the client handler
writer.WriteBreak().WriteLine($"{this.ClientName}.ClientHandler = new HttpClientHandler()");
writer.WriteBreak().WriteLine("if (handler == null)");
writer.WriteLine("{").Indent();
writer.WriteLine($"handler = new HttpClientHandler()");
writer.WriteLine("{").Indent();
writer.WriteLine("AllowAutoRedirect = true,");
writer.WriteLine("UseCookies = true,");
writer.WriteLine($"CookieContainer = {this.ClientName}.CookieContainer,");
writer.WriteLine("AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate");
writer.Outdent().WriteLine("};");
writer.Outdent().WriteLine("}");
//Store the message handler
writer.WriteBreak().WriteLine($"{this.ClientName}.MessageHandler = handler;");
//Init the http client
writer.WriteBreak().WriteLine($"{this.ClientName}.HttpClient = new HttpClient({this.ClientName}.ClientHandler);");
writer.WriteBreak().WriteLine($"{this.ClientName}.HttpClient = new HttpClient(handler);");
writer.WriteBreak().WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Accept"", ""*/*"");");
writer.WriteBreak().WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Connection"", ""keep-alive"");");
writer.WriteBreak().WriteLine(@$"{this.ClientName}.HttpClient.DefaultRequestHeaders.Add(""Accept-Encoding"", ""identity"");");
@ -109,7 +115,7 @@ namespace MontoyaTech.Rest.Net
}
else
{
writer.WriteBreak().WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("public Client(string baseUrl, HttpMessageHandler handler = null)").WriteLine("{").Indent();
//Make sure the base url isn't null or whitespace
writer.WriteBreak().WriteLine("if (string.IsNullOrWhiteSpace(baseUrl))");
@ -126,16 +132,22 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine("this.CookieContainer = new CookieContainer();");
//Init the client handler
writer.WriteBreak().WriteLine("this.ClientHandler = new HttpClientHandler()");
writer.WriteBreak().WriteLine("if (handler == null)");
writer.WriteLine("{").Indent();
writer.WriteLine("handler = new HttpClientHandler()");
writer.WriteLine("{").Indent();
writer.WriteLine("AllowAutoRedirect = true,");
writer.WriteLine("UseCookies = true,");
writer.WriteLine("CookieContainer = this.CookieContainer,");
writer.WriteLine("AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate");
writer.Outdent().WriteLine("};");
writer.Outdent().WriteLine("}");
//Store the message handler
writer.WriteBreak().WriteLine("this.MessageHandler = handler;");
//Init the http client
writer.WriteBreak().WriteLine("this.HttpClient = new HttpClient(this.ClientHandler);");
writer.WriteBreak().WriteLine("this.HttpClient = new HttpClient(handler);");
writer.WriteBreak().WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept"", ""*/*"");");
writer.WriteBreak().WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Connection"", ""keep-alive"");");
writer.WriteBreak().WriteLine(@"this.HttpClient.DefaultRequestHeaders.Add(""Accept-Encoding"", ""identity"");");