From 9fc43fac71dc0199a4e28d0a8c6e2b76a4ae40b6 Mon Sep 17 00:00:00 2001 From: Stephan Date: Sun, 4 Aug 2024 10:20:08 +0200 Subject: [PATCH] added color list, added spectre.console tables for nicer display in the demos closes #4 --- BrickOwlSharp.Client/BrickOwlClient.cs | 19 +++++- .../BrickOwlSharp.Client.csproj | 2 +- BrickOwlSharp.Client/Color.cs | 58 +++++++++++++++++++ BrickOwlSharp.Client/IBrickOwlClient.cs | 2 + BrickOwlSharp.Client/LegoColor.cs | 40 +++++++++++++ .../BrickOwlSharp.Demos.csproj | 4 ++ BrickOwlSharp.Demos/ColorDemo.cs | 47 +++++++++++++++ BrickOwlSharp.Demos/InventoryDemo.cs | 18 +++++- BrickOwlSharp.Demos/Program.cs | 8 ++- 9 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 BrickOwlSharp.Client/Color.cs create mode 100644 BrickOwlSharp.Client/LegoColor.cs create mode 100644 BrickOwlSharp.Demos/ColorDemo.cs diff --git a/BrickOwlSharp.Client/BrickOwlClient.cs b/BrickOwlSharp.Client/BrickOwlClient.cs index 95b4561..37bd380 100644 --- a/BrickOwlSharp.Client/BrickOwlClient.cs +++ b/BrickOwlSharp.Client/BrickOwlClient.cs @@ -25,10 +25,12 @@ using BrickOwlSharp.Client.Json; using System; using System.Collections.Generic; +using System.Data; using System.Globalization; using System.Linq; using System.Net.Http; using System.Reflection; +using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; @@ -208,7 +210,7 @@ public async Task> CatalogIdLookupAsync(string boid, ItemType type, CatalogItemIds result = await ExecuteGet(url, cancellationToken); _measureRequest(ResourceType.Catalog, cancellationToken); return result.BOIDs; - } + } // !CatalogIdLookupAsync() public async Task CreateInventoryAsync( @@ -226,7 +228,7 @@ public async Task CreateInventoryAsync( NewInventoryResult result = await ExecutePost(url, formData, cancellationToken: cancellationToken); _measureRequest(ResourceType.Inventory, cancellationToken); return result; - } + } // !CreateInventoryAsync() public async Task UpdateInventoryAsync( @@ -239,7 +241,7 @@ public async Task UpdateInventoryAsync( BrickOwlResult result = await ExecutePost(url, formData, cancellationToken: cancellationToken); _measureRequest(ResourceType.Inventory, cancellationToken); return (result?.Status == "success"); - } + } // !UpdateInventoryAsync() public async Task> GetInventoryAsync( @@ -277,6 +279,17 @@ public async Task DeleteInventoryAsync( } // !GetInventoryAsync() + public async Task> GetColorListAsyn(CancellationToken cancellationToken = default) + { + var url = new Uri(_baseUri, $"catalog/color_list").ToString(); + + Dictionary result = await ExecuteGet>(url, cancellationToken); + _measureRequest(ResourceType.Catalog, cancellationToken); + return result.Values.ToList(); + } // !GetColorListAsyn() + + + private static string AppendApiKey(string url) { BrickOwlClientConfiguration.Instance.ValidateThrowException(); diff --git a/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj b/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj index af0da4e..87f28cf 100644 --- a/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj +++ b/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj @@ -24,7 +24,7 @@ With BrickOwlSharp, developers can easily integrate BrickOwl into their applicat - + diff --git a/BrickOwlSharp.Client/Color.cs b/BrickOwlSharp.Client/Color.cs new file mode 100644 index 0000000..1e503e5 --- /dev/null +++ b/BrickOwlSharp.Client/Color.cs @@ -0,0 +1,58 @@ +#region License +// Copyright (c) 2024 Stephan Stapel +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +# endregion +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; + +namespace BrickOwlSharp.Client +{ + public class Color + { + [JsonPropertyName("id")] + public string Id { get; set; } + + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("hex")] + public string Hex { get; set; } + + [JsonPropertyName("peeron_names")] + public List PeeronNames { get; set; } + + [JsonPropertyName("ldraw_ids")] + public List LdrawIds { get; set; } + + [JsonPropertyName("bl_ids")] + public List BlIds { get; set; } + + [JsonPropertyName("bl_names")] + public List BlNames { get; set; } + + [JsonPropertyName("lego_colors")] + public List LegoColors { get; set; } + } +} diff --git a/BrickOwlSharp.Client/IBrickOwlClient.cs b/BrickOwlSharp.Client/IBrickOwlClient.cs index 350ae7d..75f206a 100644 --- a/BrickOwlSharp.Client/IBrickOwlClient.cs +++ b/BrickOwlSharp.Client/IBrickOwlClient.cs @@ -53,5 +53,7 @@ Task> GetInventoryAsync( Task DeleteInventoryAsync( DeleteInventory deleteInventory, CancellationToken cancellationToken = default); + + Task> GetColorListAsyn(CancellationToken cancellationToken = default); } } diff --git a/BrickOwlSharp.Client/LegoColor.cs b/BrickOwlSharp.Client/LegoColor.cs new file mode 100644 index 0000000..4afa8d8 --- /dev/null +++ b/BrickOwlSharp.Client/LegoColor.cs @@ -0,0 +1,40 @@ +#region License +// Copyright (c) 2024 Stephan Stapel +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +# endregion +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; + +namespace BrickOwlSharp.Client +{ + public class LegoColor + { + [JsonPropertyName("lego_id")] + public string LegoId { get; set; } + + [JsonPropertyName("raw_name")] + public string RawName { get; set; } + } +} diff --git a/BrickOwlSharp.Demos/BrickOwlSharp.Demos.csproj b/BrickOwlSharp.Demos/BrickOwlSharp.Demos.csproj index 42571f1..449cda4 100644 --- a/BrickOwlSharp.Demos/BrickOwlSharp.Demos.csproj +++ b/BrickOwlSharp.Demos/BrickOwlSharp.Demos.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/BrickOwlSharp.Demos/ColorDemo.cs b/BrickOwlSharp.Demos/ColorDemo.cs new file mode 100644 index 0000000..d478368 --- /dev/null +++ b/BrickOwlSharp.Demos/ColorDemo.cs @@ -0,0 +1,47 @@ +#region License +// Copyright (c) 2024 Stephan Stapel +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +# endregion +using BrickOwlSharp.Client; +using Spectre.Console; + +internal class ColorDemo +{ + internal async Task RunAsync() + { + IBrickOwlClient client = BrickOwlClientFactory.Build(); + List allColors = await client.GetColorListAsyn(); + + var table = new Table(); + table.AddColumn("Id"); + table.AddColumn("Name"); + table.AddColumn("Hex"); + + foreach (BrickOwlSharp.Client.Color color in allColors) + { + table.AddRow(color.Id, color.Name, color.Hex); + } + + AnsiConsole.Write(table); + } +} \ No newline at end of file diff --git a/BrickOwlSharp.Demos/InventoryDemo.cs b/BrickOwlSharp.Demos/InventoryDemo.cs index b191885..c1f7f0c 100644 --- a/BrickOwlSharp.Demos/InventoryDemo.cs +++ b/BrickOwlSharp.Demos/InventoryDemo.cs @@ -23,8 +23,10 @@ // OTHER DEALINGS IN THE SOFTWARE. # endregion using BrickOwlSharp.Client; +using Spectre.Console; using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -37,6 +39,8 @@ internal async Task RunAsync() { IBrickOwlClient client = BrickOwlClientFactory.Build(); + /* + NewInventoryResult newInventoryResult = await client.CreateInventoryAsync(new NewInventory() { Id = "414759", // Bracket 1 x 2 - 1 x 2 Inverted @@ -51,12 +55,22 @@ internal async Task RunAsync() AbsoluteQuantity = 23 }); + */ + + var table = new Table(); + table.AddColumn("Id"); + table.AddColumn("Lot Id"); + table.AddColumn("Quantity"); + table.AddColumn("Type"); + foreach (Inventory inventory in await client.GetInventoryAsync()) { - Console.WriteLine($"{inventory.Id}: quantity {inventory.Quantity}, lot id: {inventory.LotId}, type: {inventory.Type}"); + table.AddRow(inventory.Id, inventory.LotId.HasValue ? inventory.LotId.Value.ToString() : "", inventory.Quantity.HasValue ? inventory.Quantity.Value.ToString() : "", inventory.Type.ToString()); } - bool result = await client.DeleteInventoryAsync(new DeleteInventory() { LotId = newInventoryResult.LotId.Value }); + AnsiConsole.Write(table); + + // bool result = await client.DeleteInventoryAsync(new DeleteInventory() { LotId = newInventoryResult.LotId.Value }); } } } diff --git a/BrickOwlSharp.Demos/Program.cs b/BrickOwlSharp.Demos/Program.cs index 63fabb9..6e1b6fb 100644 --- a/BrickOwlSharp.Demos/Program.cs +++ b/BrickOwlSharp.Demos/Program.cs @@ -36,13 +36,17 @@ static async Task Main() demo.Run(); */ - /* + InventoryDemo demo = new InventoryDemo(); await demo.RunAsync(); - */ + /* CatalogDemo catalogDemo = new CatalogDemo(); catalogDemo.Run(); + */ + + ColorDemo colorDemo = new ColorDemo(); + await colorDemo.RunAsync(); return 0; }