Skip to content
Merged
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
9 changes: 0 additions & 9 deletions MojangAPI/Cache/ICacheManager.cs

This file was deleted.

41 changes: 0 additions & 41 deletions MojangAPI/Cache/JsonFileCacheManager.cs

This file was deleted.

21 changes: 0 additions & 21 deletions MojangAPI/Cache/SessionFileCacheManager.cs

This file was deleted.

75 changes: 47 additions & 28 deletions MojangAPI/HttpAction/HttpResponseHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MojangAPI.Serialization;
using System.Text.Json.Serialization.Metadata;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -11,16 +13,19 @@ public static class HttpResponseHandlers
{
public static Func<HttpResponseMessage, Task<T>> GetDefaultResponseHandler<T>()
{
if (typeof(T) == typeof(bool))
return GetSuccessCodeResponseHandler() as Func<HttpResponseMessage, Task<T>>;
if (typeof(T) == typeof(int))
return GetStatusCodeResponseHandler() as Func<HttpResponseMessage, Task<T>>;
if (typeof(T) == typeof(string))
return GetStringResponseHandler() as Func<HttpResponseMessage, Task<T>>;
if (typeof(T) == typeof(Stream))
return GetStreamResponseHandler() as Func<HttpResponseMessage, Task<T>>;

return GetJsonHandler<T>();
if (GetSuccessCodeResponseHandler() is Func<HttpResponseMessage, Task<T>> successHandler)
return successHandler;

if (GetStatusCodeResponseHandler() is Func<HttpResponseMessage, Task<T>> statusCodeHandler)
return statusCodeHandler;

if (GetStringResponseHandler() is Func<HttpResponseMessage, Task<T>> stringHandler)
return stringHandler;

if (GetStreamResponseHandler() is Func<HttpResponseMessage, Task<T>> streamHandler)
return streamHandler;

return response => GetJsonHandler(MojangJson.GetTypeInfo<T>())(response);
}

public static Func<HttpResponseMessage, Task<bool>> GetSuccessCodeResponseHandler() =>
Expand Down Expand Up @@ -63,51 +68,59 @@ public static Func<HttpResponseMessage, Task<T>> GetSuccessCodeResponseHandler<T
throw new HttpRequestException();
};

private static readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
public static Func<HttpResponseMessage, Exception?, Task<T>> GetJsonErrorHandler<T>(JsonTypeInfo<T> typeInfo) where T : new() =>
GetJsonErrorHandler(new T(), typeInfo);

public static Func<HttpResponseMessage, Exception?, Task<T>> GetJsonErrorHandler<T>() where T : new() => GetJsonErrorHandler(new T());
public static Func<HttpResponseMessage, Exception?, Task<T>> GetJsonErrorHandler<T>(T defaultObj, JsonTypeInfo<T> typeInfo)
{
if (typeInfo == null)
throw new ArgumentNullException(nameof(typeInfo));

public static Func<HttpResponseMessage, Exception?, Task<T>> GetJsonErrorHandler<T>(T defaultObj) =>
async (response, ex) =>
return async (response, ex) =>
{
//
if (ex == null)
{
try
{
var resBody = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(resBody))
{
var obj = JsonSerializer.Deserialize<T>(resBody, jsonSerializerOptions);
var obj = JsonSerializer.Deserialize(resBody, typeInfo);
if (obj != null)
return obj;
}
}
catch
{
// ignore exception to execute next error handler
// If the response cannot be parsed, fall back to the HTTP status and original exception.
}
}

response.EnsureSuccessStatusCode();
if (ex != null)
throw ex;

return defaultObj;
};
}

public static Func<HttpResponseMessage, Task<T>> GetJsonHandler<T>() =>
async (response) =>
public static Func<HttpResponseMessage, Task<T>> GetJsonHandler<T>(JsonTypeInfo<T> typeInfo)
{
if (typeInfo == null)
throw new ArgumentNullException(nameof(typeInfo));

return async response =>
{
string res = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(res))
res = "{}";
return JsonSerializer.Deserialize<T>(res, jsonSerializerOptions);

return JsonSerializer.Deserialize(res, typeInfo)
?? throw new JsonException("The response must contain a non-null JSON value.");
};
}

public static Func<HttpResponseMessage, Exception?, Task<T>> GetDefaultErrorHandler<T>() =>
public static Func<HttpResponseMessage, Exception?, Task<T?>> GetDefaultErrorHandler<T>() =>
(response, ex) =>
{
response.EnsureSuccessStatusCode();
Expand All @@ -117,8 +130,12 @@ public static Func<HttpResponseMessage, Task<T>> GetJsonHandler<T>() =>
return Task.FromResult(default(T));
};

public static Func<HttpResponseMessage, Task<T[]>> GetJsonArrayHandler<T>() =>
async (response) =>
public static Func<HttpResponseMessage, Task<T[]>> GetJsonArrayHandler<T>(JsonTypeInfo<T> typeInfo)
{
if (typeInfo == null)
throw new ArgumentNullException(nameof(typeInfo));

return async response =>
{
string res = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(res))
Expand All @@ -128,8 +145,10 @@ public static Func<HttpResponseMessage, Task<T[]>> GetJsonArrayHandler<T>() =>
var root = doc.RootElement;

return root.EnumerateArray()
.Select(elem => elem.Deserialize<T>(jsonSerializerOptions))
.Select(elem => JsonSerializer.Deserialize(elem.GetRawText(), typeInfo)
?? throw new JsonException("The response array must not contain null elements."))
.ToArray();
};
}
}
}
33 changes: 18 additions & 15 deletions MojangAPI/HttpAction/JsonHttpContent.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
using System.Net.Http;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace HttpAction
{
public class JsonHttpContent : StringContent
{
private const string DefaultMediaType = "application/json";

public JsonHttpContent(object obj)
: this(obj, null)
public JsonHttpContent(object? obj, JsonTypeInfo typeInfo)
: this(obj, typeInfo, null)
{

}

public JsonHttpContent(object obj, Encoding? encoding)
: this(obj, encoding, DefaultMediaType)
public JsonHttpContent(object? obj, JsonTypeInfo typeInfo, Encoding? encoding)
: this(
obj,
typeInfo,
encoding,
DefaultMediaType)
{

}

public JsonHttpContent(object obj, Encoding? encoding, string mediaType)
: base(serialize(obj), encoding, mediaType)
public JsonHttpContent(
object? obj,
JsonTypeInfo typeInfo,
Encoding? encoding,
string mediaType)
: base(serialize(obj, typeInfo), encoding, mediaType)
{

}

public static string serialize(object obj)
public static string serialize(object? obj, JsonTypeInfo typeInfo)
{
return JsonSerializer.Serialize(obj, new JsonSerializerOptions
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
});
return JsonSerializer.Serialize(obj, typeInfo);
}
}
}
Loading