Bumped package version to 1.8.0. Added support for automatic gzip decompression in HttpListenerRequestExtensions. Added a new compress parameter for json requests in client generated code that auto compresses the json if set. Fixed a few bugs and cleaned up code.

This commit is contained in:
MattMo 2024-01-13 10:53:44 -08:00
parent 5f83b30cb2
commit 50861d5381
7 changed files with 377 additions and 162 deletions

View File

@ -1,18 +1,14 @@
//Generated using MontoyaTech.Rest.Net
using System;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
public class Client
{
public string BaseUrl;
public CookieContainer CookieContainer;
public System.Net.CookieContainer CookieContainer;
public HttpMessageHandler MessageHandler;
public System.Net.Http.HttpMessageHandler MessageHandler;
public HttpClient HttpClient;
public System.Net.Http.HttpClient HttpClient;
public TestApi Test;
@ -20,32 +16,34 @@ public class Client
public StreamApi Stream;
public Client(string baseUrl, HttpMessageHandler handler = null)
public FormApi Form;
public Client(string baseUrl, System.Net.Http.HttpMessageHandler handler = null)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentException("baseUrl must not be null or whitespace.");
throw new System.ArgumentException("baseUrl must not be null or whitespace.");
if (baseUrl.EndsWith('/'))
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
this.BaseUrl = baseUrl;
this.CookieContainer = new CookieContainer();
this.CookieContainer = new System.Net.CookieContainer();
if (handler == null)
{
handler = new HttpClientHandler()
handler = new System.Net.Http.HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = this.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
};
}
this.MessageHandler = handler;
this.HttpClient = new HttpClient(handler);
this.HttpClient = new System.Net.Http.HttpClient(handler);
this.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
@ -58,6 +56,8 @@ public class Client
this.Auth = new AuthApi(this);
this.Stream = new StreamApi(this);
this.Form = new FormApi(this);
}
public class TestApi
@ -71,9 +71,9 @@ public class Client
public string Status()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/status");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -82,19 +82,19 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public string Add(double a, double b)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -103,19 +103,19 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public string Compress()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/compress");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/compress");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -124,19 +124,19 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public string CompressFile()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/file/compress");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/file/compress");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -145,11 +145,11 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
@ -165,9 +165,9 @@ public class Client
public bool UserExists(string name)
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -180,27 +180,47 @@ public class Client
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public void Signup(UserDto request)
public void Signup(UserDto request, bool compress = false)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
message.Content = new StringContent(JsonConvert.SerializeObject(request));
if (compress)
{
using (var uncompressedStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(request))))
{
using (var compressedStream = new System.IO.MemoryStream())
{
using (var gzipStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Compress, true))
uncompressedStream.CopyTo(gzipStream);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
message.Content = new System.Net.Http.ByteArrayContent(compressedStream.ToArray());
message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);
message.Content.Headers.ContentEncoding.Add("gzip");
}
}
}
else
{
message.Content = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(request));
message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);
}
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
public UserDto Get()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/auth/");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -209,19 +229,40 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserDto>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<UserDto>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public dynamic Dynamic()
{
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/auth/dynamic");
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(content);
}
else
{
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public UserRole GetRole()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/role");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/auth/role");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -230,11 +271,11 @@ public class Client
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserRole>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<UserRole>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
@ -248,25 +289,25 @@ public class Client
this.Client = client;
}
public void Upload(System.IO.MemoryStream request)
public void Upload(System.IO.MemoryStream request, bool compress = false)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/upload");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{this.Client.BaseUrl}/upload");
request.Seek(0, System.IO.SeekOrigin.Begin);
message.Content = new StreamContent(request);
message.Content = new System.Net.Http.StreamContent(request);
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
public System.IO.MemoryStream Download()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/download");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{this.Client.BaseUrl}/download");
var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -278,7 +319,38 @@ public class Client
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
public class FormApi
{
public Client Client;
public FormApi(Client client)
{
this.Client = client;
}
public System.Collections.Generic.Dictionary<string, string> FormTest()
{
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{this.Client.BaseUrl}/form");
var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, string>>(content);
}
else
{
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}

View File

@ -1,45 +1,41 @@
//Generated using MontoyaTech.Rest.Net
using System;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
public class StaticClient
{
public static string BaseUrl;
public static CookieContainer CookieContainer;
public static System.Net.CookieContainer CookieContainer;
public static HttpMessageHandler MessageHandler;
public static System.Net.Http.HttpMessageHandler MessageHandler;
public static HttpClient HttpClient;
public static System.Net.Http.HttpClient HttpClient;
public static void Init(string baseUrl, HttpMessageHandler handler = null)
public static void Init(string baseUrl, System.Net.Http.HttpMessageHandler handler = null)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentException("baseUrl must not be null or whitespace.");
throw new System.ArgumentException("baseUrl must not be null or whitespace.");
if (baseUrl.EndsWith('/'))
baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
StaticClient.BaseUrl = baseUrl;
StaticClient.CookieContainer = new CookieContainer();
StaticClient.CookieContainer = new System.Net.CookieContainer();
if (handler == null)
{
handler = new HttpClientHandler()
handler = new System.Net.Http.HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = StaticClient.CookieContainer,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
};
}
StaticClient.MessageHandler = handler;
StaticClient.HttpClient = new HttpClient(handler);
StaticClient.HttpClient = new System.Net.Http.HttpClient(handler);
StaticClient.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
@ -52,9 +48,9 @@ public class StaticClient
{
public static string Status()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/status");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/status");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -63,19 +59,19 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static string Add(double a, double b)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/add/{a}/{b}");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{StaticClient.BaseUrl}/add/{a}/{b}");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -84,19 +80,19 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static string Compress()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/compress");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/compress");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -105,19 +101,19 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static string CompressFile()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/file/compress");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/file/compress");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -126,11 +122,11 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<string>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
@ -139,9 +135,9 @@ public class StaticClient
{
public static bool UserExists(string name)
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/{name}");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/{name}");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -154,27 +150,47 @@ public class StaticClient
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static void Signup(UserDto request)
public static void Signup(UserDto request, bool compress = false)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{StaticClient.BaseUrl}/auth/signup");
message.Content = new StringContent(JsonConvert.SerializeObject(request));
if (compress)
{
using (var uncompressedStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(request))))
{
using (var compressedStream = new System.IO.MemoryStream())
{
using (var gzipStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Compress, true))
uncompressedStream.CopyTo(gzipStream);
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
message.Content = new System.Net.Http.ByteArrayContent(compressedStream.ToArray());
message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);
message.Content.Headers.ContentEncoding.Add("gzip");
}
}
}
else
{
message.Content = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(request));
message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);
}
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
public static UserDto Get()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -183,19 +199,40 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserDto>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<UserDto>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static dynamic Dynamic()
{
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/dynamic");
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(content);
}
else
{
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
public static UserRole GetRole()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/role");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/auth/role");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -204,36 +241,36 @@ public class StaticClient
if (string.IsNullOrEmpty(content))
return default;
return JsonConvert.DeserializeObject<UserRole>(content);
return Newtonsoft.Json.JsonConvert.DeserializeObject<UserRole>(content);
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
public class Stream
{
public static void Upload(System.IO.MemoryStream request)
public static void Upload(System.IO.MemoryStream request, bool compress = false)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"{StaticClient.BaseUrl}/upload");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{StaticClient.BaseUrl}/upload");
request.Seek(0, System.IO.SeekOrigin.Begin);
message.Content = new StreamContent(request);
message.Content = new System.Net.Http.StreamContent(request);
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
public static System.IO.MemoryStream Download()
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{StaticClient.BaseUrl}/download");
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, $"{StaticClient.BaseUrl}/download");
var response = StaticClient.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
@ -245,7 +282,31 @@ public class StaticClient
}
else
{
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}
public class Form
{
public static System.Collections.Generic.Dictionary<string, string> FormTest()
{
var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{StaticClient.BaseUrl}/form");
var response = StaticClient.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
if (string.IsNullOrEmpty(content))
return default;
return Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, string>>(content);
}
else
{
throw new System.Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
}
}
}

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.IO.Compression;
namespace MontoyaTech.Rest.Net
{
@ -23,9 +24,20 @@ namespace MontoyaTech.Rest.Net
{
try
{
using (var input = request.InputStream)
//If the request has been compressed, automatically handle it.
if (request.Headers["Content-Encoding"] == "gzip")
{
using (var inputStream = request.InputStream)
using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress, true))
using (var stream = new StreamReader(gzipStream))
return stream.ReadToEnd();
}
else
{
using (var input = request.InputStream)
using (var stream = new StreamReader(input))
return stream.ReadToEnd();
}
}
catch
{
@ -43,9 +55,20 @@ namespace MontoyaTech.Rest.Net
{
try
{
using (var input = request.InputStream)
using (var stream = new StreamReader(input))
//If the request has been compressed, automatically handle it.
if (request.Headers["Content-Encoding"] == "gzip")
{
using (var inputStream = request.InputStream)
using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress, true))
using (var stream = new StreamReader(gzipStream))
return JsonConvert.DeserializeObject<T>(stream.ReadToEnd());
}
else
{
using (var inputStream = request.InputStream)
using (var stream = new StreamReader(inputStream))
return JsonConvert.DeserializeObject<T>(stream.ReadToEnd());
}
}
catch
{
@ -62,12 +85,31 @@ namespace MontoyaTech.Rest.Net
{
try
{
using (var input = request.InputStream)
//If the request has been compressed, automatically handle it.
if (request.Headers["Content-Encoding"] == "gzip")
{
using (var stream = new MemoryStream())
using (var inputStream = request.InputStream)
{
input.CopyTo(stream);
return stream.ToArray();
using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress, true))
{
using (var memoryStream = new MemoryStream())
{
gzipStream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
}
}
else
{
using (var input = request.InputStream)
{
using (var stream = new MemoryStream())
{
input.CopyTo(stream);
return stream.ToArray();
}
}
}
}
@ -87,8 +129,22 @@ namespace MontoyaTech.Rest.Net
{
try
{
using (var input = request.InputStream)
input.CopyTo(stream);
//If the request has been compressed, automatically handle it.
if (request.Headers["Content-Encoding"] == "gzip")
{
using (var inputStream = request.InputStream)
{
using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress, true))
{
gzipStream.CopyTo(stream);
}
}
}
else
{
using (var input = request.InputStream)
input.CopyTo(stream);
}
return true;
}

View File

@ -82,10 +82,10 @@ namespace MontoyaTech.Rest.Net
response.Headers.Add("Content-Encoding", "gzip");
var bytes = Encoding.UTF8.GetBytes(text);
using (var memoryStream = new MemoryStream())
{
var bytes = Encoding.UTF8.GetBytes(text);
using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
compressedStream.Write(bytes, 0, bytes.Length);
@ -152,12 +152,13 @@ namespace MontoyaTech.Rest.Net
public static HttpListenerResponse WithCompressedJson(this HttpListenerResponse response, object obj)
{
response.ContentType = "application/json; charset=utf-8";
response.Headers.Add("Content-Encoding", "gzip");
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
response.Headers.Add("Content-Encoding", "gzip");
using (var memoryStream = new MemoryStream())
{
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
using (var compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
compressedStream.Write(bytes, 0, bytes.Length);

View File

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

View File

@ -35,11 +35,6 @@ namespace MontoyaTech.Rest.Net
writer.WriteLine("//Generated using MontoyaTech.Rest.Net");
writer.WriteLine("using System;");
writer.WriteLine("using System.Net;");
writer.WriteLine("using System.Net.Http;");
writer.WriteLine("using Newtonsoft.Json;");
writer.WriteBreak().WriteLine($"public class {this.ClientName}").WriteLine("{").Indent();
//Create the base url field
@ -50,21 +45,21 @@ namespace MontoyaTech.Rest.Net
//Create the cookie container field
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static CookieContainer CookieContainer;");
writer.WriteBreak().WriteLine("public static System.Net.CookieContainer CookieContainer;");
else
writer.WriteBreak().WriteLine("public CookieContainer CookieContainer;");
writer.WriteBreak().WriteLine("public System.Net.CookieContainer CookieContainer;");
//Create the client handler field
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static HttpMessageHandler MessageHandler;");
writer.WriteBreak().WriteLine("public static System.Net.Http.HttpMessageHandler MessageHandler;");
else
writer.WriteBreak().WriteLine("public HttpMessageHandler MessageHandler;");
writer.WriteBreak().WriteLine("public System.Net.Http.HttpMessageHandler MessageHandler;");
//Create the http client field
if (this.StaticCode)
writer.WriteBreak().WriteLine("public static HttpClient HttpClient;");
writer.WriteBreak().WriteLine("public static System.Net.Http.HttpClient HttpClient;");
else
writer.WriteBreak().WriteLine("public HttpClient HttpClient;");
writer.WriteBreak().WriteLine("public System.Net.Http.HttpClient HttpClient;");
//Create fields foreach route group so they can be accessed.
if (!this.StaticCode)
@ -74,11 +69,11 @@ namespace MontoyaTech.Rest.Net
//Create the client constructor or init method
if (this.StaticCode)
{
writer.WriteBreak().WriteLine("public static void Init(string baseUrl, HttpMessageHandler handler = null)").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("public static void Init(string baseUrl, System.Net.Http.HttpMessageHandler handler = null)").WriteLine("{").Indent();
//Make sure the base url isn't null or whitespace
writer.WriteBreak().WriteLine("if (string.IsNullOrWhiteSpace(baseUrl))");
writer.Indent().WriteLine(@"throw new ArgumentException(""baseUrl must not be null or whitespace."");").Outdent();
writer.Indent().WriteLine(@"throw new System.ArgumentException(""baseUrl must not be null or whitespace."");").Outdent();
//If the baseUrl ends with a /, remove it.
writer.WriteBreak().WriteLine("if (baseUrl.EndsWith('/'))");
@ -88,17 +83,17 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine($"{this.ClientName}.BaseUrl = baseUrl;");
//Init the cookie container
writer.WriteBreak().WriteLine($"{this.ClientName}.CookieContainer = new CookieContainer();");
writer.WriteBreak().WriteLine($"{this.ClientName}.CookieContainer = new System.Net.CookieContainer();");
//Init the client handler
writer.WriteBreak().WriteLine("if (handler == null)");
writer.WriteLine("{").Indent();
writer.WriteLine($"handler = new HttpClientHandler()");
writer.WriteLine($"handler = new System.Net.Http.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.WriteLine("AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate");
writer.Outdent().WriteLine("};");
writer.Outdent().WriteLine("}");
@ -106,7 +101,7 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine($"{this.ClientName}.MessageHandler = handler;");
//Init the http client
writer.WriteBreak().WriteLine($"{this.ClientName}.HttpClient = new HttpClient(handler);");
writer.WriteBreak().WriteLine($"{this.ClientName}.HttpClient = new System.Net.Http.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"");");
@ -115,11 +110,11 @@ namespace MontoyaTech.Rest.Net
}
else
{
writer.WriteBreak().WriteLine("public Client(string baseUrl, HttpMessageHandler handler = null)").WriteLine("{").Indent();
writer.WriteBreak().WriteLine($"public {this.ClientName}(string baseUrl, System.Net.Http.HttpMessageHandler handler = null)").WriteLine("{").Indent();
//Make sure the base url isn't null or whitespace
writer.WriteBreak().WriteLine("if (string.IsNullOrWhiteSpace(baseUrl))");
writer.Indent().WriteLine(@"throw new ArgumentException(""baseUrl must not be null or whitespace."");").Outdent();
writer.Indent().WriteLine(@"throw new System.ArgumentException(""baseUrl must not be null or whitespace."");").Outdent();
//If the baseUrl ends with a /, remove it.
writer.WriteBreak().WriteLine("if (baseUrl.EndsWith('/'))");
@ -129,17 +124,17 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine("this.BaseUrl = baseUrl;");
//Init the cookie container
writer.WriteBreak().WriteLine("this.CookieContainer = new CookieContainer();");
writer.WriteBreak().WriteLine("this.CookieContainer = new System.Net.CookieContainer();");
//Init the client handler
writer.WriteBreak().WriteLine("if (handler == null)");
writer.WriteLine("{").Indent();
writer.WriteLine("handler = new HttpClientHandler()");
writer.WriteLine("handler = new System.Net.Http.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.WriteLine("AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate");
writer.Outdent().WriteLine("};");
writer.Outdent().WriteLine("}");
@ -147,7 +142,7 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine("this.MessageHandler = handler;");
//Init the http client
writer.WriteBreak().WriteLine("this.HttpClient = new HttpClient(handler);");
writer.WriteBreak().WriteLine("this.HttpClient = new System.Net.Http.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"");");
@ -418,36 +413,43 @@ namespace MontoyaTech.Rest.Net
writer.Write(" input");
}
//If JSON, add a compress parameter to control compressing the content.
if (routeRequest != null && routeRequest.Json)
{
writer.WriteSeparator();
writer.Write("bool compress = false");
}
writer.WriteLine(")").WriteLine("{").Indent();
//Generate the message code
writer.WriteBreak().Write($"var message = new HttpRequestMessage(");
writer.WriteBreak().Write($"var message = new System.Net.Http.HttpRequestMessage(");
switch (route.Method.ToLower())
{
case "post":
writer.Write("HttpMethod.Post");
writer.Write("System.Net.Http.HttpMethod.Post");
break;
case "get":
writer.Write("HttpMethod.Get");
writer.Write("System.Net.Http.HttpMethod.Get");
break;
case "delete":
writer.Write("HttpMethod.Delete");
writer.Write("System.Net.Http.HttpMethod.Delete");
break;
case "put":
writer.Write("HttpMethod.Put");
writer.Write("System.Net.Http.HttpMethod.Put");
break;
case "options":
writer.Write("HttpMethod.Options");
writer.Write("System.Net.Http.HttpMethod.Options");
break;
case "patch":
writer.Write("HttpMethod.Patch");
writer.Write("System.Net.Http.HttpMethod.Patch");
break;
case "head":
writer.Write("HttpMethod.Head");
writer.Write("System.Net.Http.HttpMethod.Head");
break;
case "trace":
writer.Write("HttpMethod.Trace");
writer.Write("System.Net.Http.HttpMethod.Trace");
break;
default:
throw new NotSupportedException("Unsupport route method:" + route.Method);
@ -496,23 +498,46 @@ namespace MontoyaTech.Rest.Net
{
writer.WriteBreak().WriteLine("request.Seek(0, System.IO.SeekOrigin.Begin);");
writer.WriteBreak().WriteLine("message.Content = new StreamContent(request);");
writer.WriteBreak().WriteLine("message.Content = new System.Net.Http.StreamContent(request);");
}
else if (routeRequest.Json)
{
writer.WriteBreak().WriteLine("message.Content = new StringContent(JsonConvert.SerializeObject(request));");
writer.WriteBreak().WriteLine("if (compress)").WriteLine("{").Indent();
writer.WriteLine("using (var uncompressedStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(request))))");
writer.WriteLine("{").Indent();
writer.WriteLine("using (var compressedStream = new System.IO.MemoryStream())");
writer.WriteLine("{").Indent();
writer.WriteLine("using (var gzipStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Compress, true))");
writer.Indent().WriteLine("uncompressedStream.CopyTo(gzipStream);").Outdent();
writer.WriteBreak();
writer.WriteLine("message.Content = new System.Net.Http.ByteArrayContent(compressedStream.ToArray());");
writer.WriteLine("message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);");
writer.WriteLine("message.Content.Headers.ContentEncoding.Add(\"gzip\");");
writer.Outdent().WriteLine("}");
writer.Outdent().WriteLine("}");
writer.Outdent().WriteLine("}");
writer.WriteLine("else").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("message.Content = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(request));");
writer.WriteBreak().WriteLine("message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Json);");
writer.Outdent().WriteLine("}");
}
else
{
writer.WriteBreak().WriteLine("message.Content = new StringContent(request.ToString());");
writer.WriteBreak().WriteLine("message.Content = new System.Net.Http.StringContent(request.ToString());");
}
}
//Generate the response code
if (this.StaticCode)
writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);");
writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);");
else
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);");
writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);");
//Handle the response
if (routeResponse != null)
@ -544,7 +569,7 @@ namespace MontoyaTech.Rest.Net
if (routeResponse.Json)
{
writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{(routeResponse.Dynamic ? "dynamic" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))}>(content);");
writer.WriteBreak().WriteLine($"return Newtonsoft.Json.JsonConvert.DeserializeObject<{(routeResponse.Dynamic ? "dynamic" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))}>(content);");
}
else
{
@ -563,7 +588,7 @@ namespace MontoyaTech.Rest.Net
break;
case TypeCode.DateTime:
writer.WriteBreak().WriteLine("return DateTime.Parse(content);");
writer.WriteBreak().WriteLine("return System.DateTime.Parse(content);");
break;
case TypeCode.Decimal:
@ -617,13 +642,13 @@ namespace MontoyaTech.Rest.Net
}
writer.Outdent().WriteLine("}").WriteLine("else").WriteLine("{").Indent();
writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);");
writer.WriteLine(@"throw new System.Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);");
writer.Outdent().WriteLine("}");
}
else
{
writer.WriteBreak().WriteLine("if (!response.IsSuccessStatusCode)").Indent();
writer.WriteLine(@"throw new Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent();
writer.WriteLine(@"throw new System.Exception(""Unexpected Http Response StatusCode:"" + response.StatusCode);").Outdent();
}
//Close off the route function.

View File

@ -248,7 +248,7 @@ namespace MontoyaTech.Rest.Net
case TypeCode.Char:
return "char";
case TypeCode.DateTime:
return "DateTime";
return "System.DateTime";
case TypeCode.Decimal:
return "decimal";
case TypeCode.Double: