Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override JsonConvert.DefaultSettings in FaxClient can make legacy app is broken. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions InterFAX.Api/Documents.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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;
using System.Net.Http;
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
{
Expand Down Expand Up @@ -49,8 +48,8 @@ public Dictionary<string, string> SupportedMediaTypes
}
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
var mappings = JsonConvert.DeserializeObject<List<MediaTypeMapping>>(File.ReadAllText(typesFile));

var mappings = JsonConvert.DeserializeObject<List<MediaTypeMapping>>(File.ReadAllText(typesFile), settings);
_supportedMediaTypes = mappings.ToDictionary(
mapping => mapping.FileType,
mapping => mapping.MediaType);
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
});
Expand All @@ -208,7 +207,7 @@ public UploadSession UploadDocument(string fileName, FileStream fileStream)

UploadFileStreamToSession(sessionId, fileStream);

return GetUploadSession(sessionId).Result;
return GetUploadSession(sessionId).Result;
}

/// <summary>
Expand All @@ -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))
Expand Down
38 changes: 14 additions & 24 deletions InterFAX.Api/FaxClient.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -42,7 +32,7 @@ public enum ApiRoot
/// <param name="password"></param>
/// <param name="messageHandler"></param>
/// <param name="ApiRoot">(Optional) Alternative API Root</param>
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);
}
Expand All @@ -65,11 +55,11 @@ public FaxClient(HttpMessageHandler messageHandler = null, ApiRoot apiRoot = Api
/// <param name="httpClient">Custom httpClient instance</param>
/// <param name="ApiRoot">(Optional) Alternative API Root</param>
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.");
Expand All @@ -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)
Expand All @@ -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<string> {$"Basic {Utils.Base64Encode($"{username}:{password}")}"});
new List<string> { $"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;
// };
}
}
}