From 09f444d173081cdadbfd6df4dec0008784eb4868 Mon Sep 17 00:00:00 2001 From: hoangitk Date: Fri, 12 Nov 2021 21:38:30 +0700 Subject: [PATCH] Remove: override JsonConvert.DefaultSettings --- InterFAX.Api/Documents.cs | 19 +++++++++---------- InterFAX.Api/FaxClient.cs | 38 ++++++++++++++------------------------ 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/InterFAX.Api/Documents.cs b/InterFAX.Api/Documents.cs index b928d4c..8a4977a 100644 --- a/InterFAX.Api/Documents.cs +++ b/InterFAX.Api/Documents.cs @@ -1,6 +1,8 @@ +using InterFAX.Api.Dtos; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; -using System.Configuration; using System.IO; using System.Linq; using System.Net; @@ -8,9 +10,6 @@ using System.Net.Http.Headers; using System.Reflection; using System.Threading.Tasks; -using InterFAX.Api.Dtos; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; namespace InterFAX.Api { @@ -49,8 +48,8 @@ public Dictionary SupportedMediaTypes } var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter { CamelCaseText = true }); - - var mappings = JsonConvert.DeserializeObject>(File.ReadAllText(typesFile)); + + var mappings = JsonConvert.DeserializeObject>(File.ReadAllText(typesFile), settings); _supportedMediaTypes = mappings.ToDictionary( mapping => mapping.FileType, mapping => mapping.MediaType); @@ -107,7 +106,7 @@ public IFaxDocument BuildFaxDocument(string filePath) public IFaxDocument BuildFaxDocument(string fileName, FileStream fileStream) { var extension = Path.GetExtension(fileName) ?? "*"; - extension = extension.TrimStart('.').ToLower();; + extension = extension.TrimStart('.').ToLower(); ; var mediaType = SupportedMediaTypes.Keys.Contains(extension) ? SupportedMediaTypes[extension] @@ -185,7 +184,7 @@ public void UploadFileStreamToSession(string sessionId, FileStream fileStream) throw new ApiException(response.StatusCode, new Error { - Code = (int) response.StatusCode, + Code = (int)response.StatusCode, Message = response.ReasonPhrase, MoreInfo = response.Content.ReadAsStringAsync().Result }); @@ -208,7 +207,7 @@ public UploadSession UploadDocument(string fileName, FileStream fileStream) UploadFileStreamToSession(sessionId, fileStream); - return GetUploadSession(sessionId).Result; + return GetUploadSession(sessionId).Result; } /// @@ -226,7 +225,7 @@ public UploadSession UploadDocument(string filePath) var sessionId = CreateUploadSession(new UploadSessionOptions { Name = fileInfo.Name, - Size = (int) fileInfo.Length + Size = (int)fileInfo.Length }).Result; using (var fileStream = File.OpenRead(filePath)) diff --git a/InterFAX.Api/FaxClient.cs b/InterFAX.Api/FaxClient.cs index cb2b678..3830d0c 100644 --- a/InterFAX.Api/FaxClient.cs +++ b/InterFAX.Api/FaxClient.cs @@ -1,17 +1,7 @@ using System; using System.Collections.Generic; -using System.Configuration; -using System.Diagnostics; -using System.IO; -using System.Linq; using System.Net.Http; using System.Net.Http.Headers; -using System.Text; -using System.Threading.Tasks; -using System.Web; -using InterFAX.Api.Dtos; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; namespace InterFAX.Api { @@ -42,7 +32,7 @@ public enum ApiRoot /// /// /// (Optional) Alternative API Root - public FaxClient(string username, string password, HttpMessageHandler messageHandler = null, ApiRoot apiRoot=ApiRoot.InterFAX_Default) + public FaxClient(string username, string password, HttpMessageHandler messageHandler = null, ApiRoot apiRoot = ApiRoot.InterFAX_Default) { Initialise(username, password, apiRoot: apiRoot, messageHandler: messageHandler); } @@ -65,11 +55,11 @@ public FaxClient(HttpMessageHandler messageHandler = null, ApiRoot apiRoot = Api /// Custom httpClient instance /// (Optional) Alternative API Root public FaxClient(string username, string password, HttpClient httpClient, ApiRoot apiRoot = ApiRoot.InterFAX_Default) - { - Initialise(username, password, apiRoot:apiRoot, messageHandler:null, httpClient:httpClient); - } + { + Initialise(username, password, apiRoot: apiRoot, messageHandler: null, httpClient: httpClient); + } - private void Initialise(string username, string password, ApiRoot apiRoot, HttpMessageHandler messageHandler = null, HttpClient httpClient = null) + private void Initialise(string username, string password, ApiRoot apiRoot, HttpMessageHandler messageHandler = null, HttpClient httpClient = null) { if (string.IsNullOrEmpty(username)) throw new ArgumentException($"{UsernameConfigKey} has not been set."); @@ -83,7 +73,7 @@ private void Initialise(string username, string password, ApiRoot apiRoot, HttpM Documents = new Documents(this); HttpClient = messageHandler == null ? new HttpClient() : new HttpClient(messageHandler); - HttpClient = httpClient != null ? httpClient : HttpClient; + HttpClient = httpClient != null ? httpClient : HttpClient; var root = ""; switch (apiRoot) @@ -105,15 +95,15 @@ private void Initialise(string username, string password, ApiRoot apiRoot, HttpM HttpClient.BaseAddress = new Uri(root); HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpClient.DefaultRequestHeaders.Add("Authorization", - new List {$"Basic {Utils.Base64Encode($"{username}:{password}")}"}); + new List { $"Basic {Utils.Base64Encode($"{username}:{password}")}" }); - JsonConvert.DefaultSettings = () => - { - var settings = new JsonSerializerSettings(); - //Required StringEnumConverter moved to Documents.cs as Custom settings, instead of overriding defaults. - //settings.Converters.Add(new StringEnumConverter { CamelCaseText = true }); - return settings; - }; + // JsonConvert.DefaultSettings = () => + // { + // var settings = new JsonSerializerSettings(); + ////Required StringEnumConverter moved to Documents.cs as Custom settings, instead of overriding defaults. + // //settings.Converters.Add(new StringEnumConverter { CamelCaseText = true }); + // return settings; + // }; } } }