Skip to content

Commit

Permalink
sdk v 5.2.2 (#18)
Browse files Browse the repository at this point in the history
* https/https challenge
* removed MaxResponseContentBufferSize
* secure auto https upgrade 
* increased upload buffer size
* added response headers to ApiResult
  • Loading branch information
strongtigerman authored Sep 14, 2022
1 parent fca0a4d commit 28854d7
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 118 deletions.
17 changes: 17 additions & 0 deletions src/Client/IJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Morph.Server.Sdk.Dto.Commands;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Morph.Server.Sdk.Client
{

public interface IJsonSerializer
{
T Deserialize<T>(string input)
where T : new ();

string Serialize<T>(T obj);
}
}
4 changes: 3 additions & 1 deletion src/Client/IMorphServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IHasConfig
IClientConfiguration Config { get; }
}

internal interface ICanCloseSession: IHasConfig, IDisposable
public interface ICanCloseSession: IHasConfig, IDisposable
{
Task CloseSessionAsync(ApiSession apiSession, CancellationToken cancellationToken);

Expand All @@ -28,6 +28,8 @@ public interface IMorphServerApiClient: IHasConfig, IDisposable
{
event EventHandler<FileTransferProgressEventArgs> OnDataDownloadProgress;
event EventHandler<FileTransferProgressEventArgs> OnDataUploadProgress;

HttpSecurityState HttpSecurityState { get; }



Expand Down
4 changes: 3 additions & 1 deletion src/Client/IRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace Morph.Server.Sdk.Client
{
public interface IRestClient : IDisposable
{
HttpClient HttpClient { get; set; }

HttpSecurityState HttpSecurityState { get; }
HttpClient HttpClient { get; }
Task<ApiResult<TResult>> GetAsync<TResult>(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
where TResult : new();
Task<ApiResult<TResult>> HeadAsync<TResult>(string url, NameValueCollection urlParameters, HeadersCollection headersCollection, CancellationToken cancellationToken)
Expand Down
6 changes: 3 additions & 3 deletions src/Client/LowLevelApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,19 @@ public async Task<ApiResult<bool>> WebFileExistsAsync(ApiSession apiSession, str
// http ok or http no content means that file exists
if (apiResult.IsSucceed)
{
return ApiResult<bool>.Ok(true);
return ApiResult<bool>.Ok(true, apiResult.ResponseHeaders);
}
else
{
// if not found, return Ok with false result
if(apiResult.Error is MorphApiNotFoundException)
{
return ApiResult<bool>.Ok(false);
return ApiResult<bool>.Ok(false, apiResult.ResponseHeaders);
}
else
{
// some error occured - return internal error from api result
return ApiResult<bool>.Fail(apiResult.Error);
return ApiResult<bool>.Fail(apiResult.Error, apiResult.ResponseHeaders);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Morph.Server.Sdk.Dto;
using Morph.Server.Sdk.Dto.Commands;
using Morph.Server.Sdk.Exceptions;
using System;
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Morph.Server.Sdk.Dto;
using Morph.Server.Sdk.Exceptions;

namespace Morph.Server.Sdk.Helper
namespace Morph.Server.Sdk.Client
{
public static class JsonSerializationHelper
public class MorphDataContractJsonSerializer: IJsonSerializer
{
public static T Deserialize<T>(string input)
public T Deserialize<T>(string input)
where T: new()
{
try
Expand All @@ -24,7 +19,8 @@ public static T Deserialize<T>(string input)
{
return new T();
}
var serializer = new DataContractJsonSerializer(typeof(T));
var serializer = new DataContractJsonSerializer(typeof(T),
new DataContractJsonSerializerSettings() { UseSimpleDictionaryFormat = true });
var d = Encoding.UTF8.GetBytes(input);
using (var ms = new MemoryStream(d))
{
Expand All @@ -37,7 +33,7 @@ public static T Deserialize<T>(string input)
throw new ResponseParseException("An error occurred while deserializing the response: "+ ex.Message, input);
}
}
public static string Serialize<T>(T obj)
public string Serialize<T>(T obj)
{

var serializer = new DataContractJsonSerializer(typeof(T));
Expand All @@ -52,4 +48,4 @@ public static string Serialize<T>(T obj)


}
}
}
25 changes: 15 additions & 10 deletions src/Client/MorphServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ internal ILowLevelApiClient BuildApiClient(ClientConfiguration configuration)
// handler will be disposed automatically
HttpClientHandler aHandler = new HttpClientHandler()
{
ClientCertificateOptions = ClientCertificateOption.Automatic
ClientCertificateOptions = ClientCertificateOption.Automatic
};
#else
Not implemented
#endif
var httpClient = BuildHttpClient(configuration, aHandler);
var restClient = ConstructRestApiClient(httpClient);
var restClient = ConstructRestApiClient(httpClient, BuildBaseAddress(configuration), clientConfiguration);
return new LowLevelApiClient(restClient);
}

Expand Down Expand Up @@ -101,27 +101,33 @@ public MorphServerApiClient(ClientConfiguration clientConfiguration)
}


protected virtual IRestClient ConstructRestApiClient(HttpClient httpClient)
protected virtual IRestClient ConstructRestApiClient(HttpClient httpClient, Uri baseAddress, ClientConfiguration clientConfiguration)
{
if (httpClient == null)
{
throw new ArgumentNullException(nameof(httpClient));
}

return new MorphServerRestClient(httpClient);
return new MorphServerRestClient(httpClient, baseAddress, clientConfiguration.HttpSecurityState);
}


protected virtual Uri BuildBaseAddress(ClientConfiguration config)
{
var baseAddress = new Uri(config.ApiUri, new Uri(_api_v1, UriKind.Relative));
return baseAddress;

}

protected HttpClient BuildHttpClient(ClientConfiguration config, HttpClientHandler httpClientHandler)

protected virtual HttpClient BuildHttpClient(ClientConfiguration config, HttpClientHandler httpClientHandler)
{
if (httpClientHandler == null)
{
throw new ArgumentNullException(nameof(httpClientHandler));
}

var client = new HttpClient(httpClientHandler, true);
client.BaseAddress = new Uri(config.ApiUri, new Uri(_api_v1, UriKind.Relative));

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
Expand All @@ -137,7 +143,7 @@ protected HttpClient BuildHttpClient(ClientConfiguration config, HttpClientHandl
client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
client.DefaultRequestHeaders.Add("Keep-Alive", "timeout=120");

client.MaxResponseContentBufferSize = 100 * 1024;

client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
Expand Down Expand Up @@ -425,8 +431,7 @@ public Task<SpaceStatus> GetSpaceStatusAsync(ApiSession apiSession, Cancellation
}




public HttpSecurityState HttpSecurityState => RestClient.HttpSecurityState;

/// <summary>
/// Returns server status. May raise exception if server is unreachable
Expand Down Expand Up @@ -658,7 +663,7 @@ public async Task<ApiSession> OpenSessionAsync(OpenSessionRequest openSessionReq
new OpenSessionAuthenticatorContext(
_lowLevelApiClient,
this as ICanCloseSession,
(handler) => ConstructRestApiClient(BuildHttpClient(clientConfiguration, handler))),
(handler) => ConstructRestApiClient(BuildHttpClient(clientConfiguration, handler), BuildBaseAddress(clientConfiguration), clientConfiguration)),
openSessionRequest, cancellationToken);

return session;
Expand Down
1 change: 1 addition & 0 deletions src/Client/MorphServerApiClientGlobalConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static class MorphServerApiClientGlobalConfig
/// </summary>
public static bool AutoDisposeClientOnSessionClose { get; set; } = true;


static MorphServerApiClientGlobalConfig()
{
// set sdk version string
Expand Down
Loading

0 comments on commit 28854d7

Please sign in to comment.