Skip to content

Commit

Permalink
Merge pull request #1 from tekgator/dev
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
tekgator committed Dec 6, 2022
2 parents c076e5a + 9f1277a commit f61da74
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 33 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [1.1.0] - 2022-12-06
### Changed
- Remove JsonPropertyName attribute from models and work with JsonSerializerOptions case insensitive property instead

### Added
- Add ToList method on JarTypes Model to return a list of JarTypeItems for easier access via enumerable


## [1.0.2] - 2022-12-02
### Changed
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ Simply instantiate the `ServerJars` class and use it's methods to gather informa

```CSharp
using ServerJarsAPI;
using ServerJarsAPI.Events;
using ServerJarsAPI.Extensions;
using System.Text.Json;

var serverJar = new ServerJars();

// GetTypes
var types = await serverJar.GetTypes();
Console.WriteLine(JsonSerializer.Serialize(types, jsonOptions));

// GetTypes.ToList() extension
SetConsoleColor(ConsoleColor.White, ConsoleColor.Red);
Console.WriteLine("\nAPI call - GetTypes.ToList():\n");
ResetConsoleColor();

types.ToList().ForEach(t => Console.WriteLine(t.ToString()));

// GetDetails
var details = await serverJar.GetDetails("servers", "spigot", "1.19.1");
Console.WriteLine(JsonSerializer.Serialize(details, jsonOptions));
Expand Down
8 changes: 8 additions & 0 deletions ServerJars.Demo.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// See https://aka.ms/new-console-template for more information
using ServerJarsAPI;
using ServerJarsAPI.Events;
using ServerJarsAPI.Extensions;
using System.Text.Json;

var jsonOptions = new JsonSerializerOptions
Expand All @@ -20,6 +21,13 @@
var types = await serverJar.GetTypes();
Console.WriteLine(JsonSerializer.Serialize(types, jsonOptions));

// GetTypes.ToList() extension
SetConsoleColor(ConsoleColor.White, ConsoleColor.Red);
Console.WriteLine("\nAPI call - GetTypes.ToList() extension:\n");
ResetConsoleColor();

types.ToList().ForEach(t => Console.WriteLine(t.ToString()));


// GetDetails
SetConsoleColor(ConsoleColor.White, ConsoleColor.Red);
Expand Down
16 changes: 12 additions & 4 deletions ServerJars.Tests/ServerJarsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ServerJarsAPI.Events;
using ServerJarsAPI.Extensions;
using System.Text.Json;

namespace ServerJarsAPI.Tests;
Expand All @@ -9,9 +10,7 @@ public class ServerJarsTests

[SetUp]
public void Setup()
{

}
{ }

[TestCase("")]
[TestCase("servers")]
Expand All @@ -27,6 +26,16 @@ public void GetTypes_InvalidType(string type)
Assert.ThrowsAsync<JsonException>(async () => await _serverJars.GetTypes(type));
}

[TestCase("")]
[TestCase("servers")]
public async Task GetTypes_SuccessAsList(string type)
{
var types = (await _serverJars.GetTypes(type)).ToList();
var item = types.FirstOrDefault(t => t.Category == "servers");

Assert.That(item, Is.Not.Null);
}

[TestCase("servers", "spigot", "")]
[TestCase("servers", "spigot", "1.19.2")]
public async Task GetDetails_Success(string type, string category, string version)
Expand Down Expand Up @@ -102,7 +111,6 @@ public void GetJar_InvalidTypeCategoryVersion(string type, string category, stri
Assert.ThrowsAsync<HttpRequestException>(async () => await _serverJars.GetJar(type, category, version));
}


[TestCase("servers", "spigot", "")]
[TestCase("servers", "spigot", "1.19.1")]
public async Task GetJarWithProgress_Success(string type, string category, string version)
Expand Down
1 change: 1 addition & 0 deletions ServerJars/ClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected ClientApi(string baseUri, HttpClient httpClient, bool disposeClient =
_disposeClient = disposeClient;

_jsonOptions.Converters.Add(new UnixEpochDateTimeConverter());
_jsonOptions.PropertyNameCaseInsensitive = true;
}

protected async Task<T> GetAsync<T>(
Expand Down
19 changes: 19 additions & 0 deletions ServerJars/Extensions/JarTypesExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ServerJarsAPI.Models;

namespace ServerJarsAPI.Extensions;

public static class JarTypesExtensions
{
public static List<JarTypeItem> ToList(this JarTypes jarTypes)
{
List<JarTypeItem> list = new();

list.AddRange(jarTypes.Vanilla.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Vanilla).ToLower(), Type = v }));
list.AddRange(jarTypes.Bedrock.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Bedrock).ToLower(), Type = v }));
list.AddRange(jarTypes.Servers.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Servers).ToLower(), Type = v }));
list.AddRange(jarTypes.Modded.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Modded).ToLower(), Type = v }));
list.AddRange(jarTypes.Proxies.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Proxies).ToLower(), Type = v }));

return list;
}
}
5 changes: 1 addition & 4 deletions ServerJars/Models/ApiError.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Text.Json.Serialization;

namespace ServerJarsAPI.Models;
namespace ServerJarsAPI.Models;

internal class ApiError
{
[JsonPropertyName("message")]
public string Message { get; set; } = string.Empty;
}
6 changes: 1 addition & 5 deletions ServerJars/Models/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System.Text.Json.Serialization;

namespace ServerJarsAPI.Models;
namespace ServerJarsAPI.Models;

internal class ApiResponse<T>
{
[JsonPropertyName("status")]
public string Status { get; set; } = string.Empty;

[JsonPropertyName("response")]
public T? Response { get; set; }

public bool IsSuccess => Status == "success" && Response is not null;
Expand Down
13 changes: 1 addition & 12 deletions ServerJars/Models/JarDetails.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
using System.Text.Json.Serialization;

namespace ServerJarsAPI.Models;
namespace ServerJarsAPI.Models;

public class JarDetails
{
[JsonPropertyName("version")]
public string Version { get; set; } = string.Empty;

[JsonPropertyName("file")]
public string File { get; set; } = string.Empty;

[JsonPropertyName("size")]
public Size Size { get; set; } = new();

[JsonPropertyName("md5")]
public string Md5 { get; set; } = string.Empty;

[JsonPropertyName("built")]
//public long Built { get; set; } = 0;
public DateTime Built { get; set; } = DateTime.MinValue;

[JsonPropertyName("stability")]
public string Stability { get; set; } = string.Empty;
}

public class Size
{
[JsonPropertyName("display")]
public string Display { get; set; } = string.Empty;

[JsonPropertyName("bytes")]
public uint Bytes { get; set; } = 0;
}
9 changes: 9 additions & 0 deletions ServerJars/Models/JarTypeItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ServerJarsAPI.Models;

public class JarTypeItem
{
public string Category { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;

public override string ToString() => $"{Category}:{Type}";
}
9 changes: 1 addition & 8 deletions ServerJars/Models/JarTypes.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using System.Text.Json.Serialization;

namespace ServerJarsAPI.Models;
namespace ServerJarsAPI.Models;

public class JarTypes
{
[JsonPropertyName("bedrock")]
public List<string> Bedrock { get; set; } = new();

[JsonPropertyName("modded")]
public List<string> Modded { get; set; } = new();

[JsonPropertyName("proxies")]
public List<string> Proxies { get; set; } = new();

[JsonPropertyName("servers")]
public List<string> Servers { get; set; } = new();

[JsonPropertyName("vanilla")]
public List<string> Vanilla { get; set; } = new();
}

0 comments on commit f61da74

Please sign in to comment.