From 60f09ab597bad9de6c2fcdb030b8e7669752990a Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 21 Jan 2025 22:44:31 +0100 Subject: [PATCH] More comprehensive implementation of order API --- BrickOwlSharp.Client/BrickOwlClient.cs | 42 +++++++++++++++- .../BrickOwlSharp.Client.csproj | 2 +- BrickOwlSharp.Client/IBrickOwlClient.cs | 8 +++- BrickOwlSharp.Client/OrderSortType.cs | 36 ++++++++++++++ BrickOwlSharp.Client/OrderType.cs | 36 ++++++++++++++ BrickOwlSharp.Demos/OrderDemo.cs | 48 +++++++++++++++++++ BrickOwlSharp.Demos/Program.cs | 13 ++--- 7 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 BrickOwlSharp.Client/OrderSortType.cs create mode 100644 BrickOwlSharp.Client/OrderType.cs create mode 100644 BrickOwlSharp.Demos/OrderDemo.cs diff --git a/BrickOwlSharp.Client/BrickOwlClient.cs b/BrickOwlSharp.Client/BrickOwlClient.cs index 37bd380..5298918 100644 --- a/BrickOwlSharp.Client/BrickOwlClient.cs +++ b/BrickOwlSharp.Client/BrickOwlClient.cs @@ -93,9 +93,47 @@ private void Dispose(bool disposing) public async Task> GetOrdersAsync( + OrderStatus? orderStatusFilter = null, + DateTime? minOrderTime = null, + int? limit = null, + OrderType? orderType = null, + OrderSortType? orderSortType = null, CancellationToken cancellationToken = default) { var url = new Uri(_baseUri, $"order/list").ToString(); + + if (orderStatusFilter.HasValue) + { + url = AppendOptionalParam(url, "status", (int)orderStatusFilter); + } + + if (minOrderTime.HasValue) + { + url = AppendOptionalParam(url, "order_time", ((DateTimeOffset)minOrderTime.Value).ToUnixTimeSeconds()); + } + + if (limit.HasValue) + { + url = AppendOptionalParam(url, "limit", limit); + } + + if (orderType.HasValue) + { + if (orderType == OrderType.Placed) + { + url = AppendOptionalParam(url, "list_type", "customer"); + } + else if (orderType == OrderType.Received) + { + url = AppendOptionalParam(url, "list_type", "store"); + } + } + + if (orderSortType.HasValue) + { + url = AppendOptionalParam(url, "sort_by", orderSortType.Value.ToString().ToLower()); + } + List result = await ExecuteGet>(url, cancellationToken); _measureRequest(ResourceType.Order, cancellationToken); return result; @@ -385,7 +423,7 @@ private async Task ExecutePost(string url, Dictionary _ObjectToFormData(object o, bool addKey = true) + private Dictionary _ObjectToFormData(object o, bool addKey = true) { Dictionary result = new Dictionary(); @@ -453,4 +491,4 @@ private async void _measureRequest(ResourceType resourceType, CancellationToken } } } -} \ No newline at end of file +} diff --git a/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj b/BrickOwlSharp.Client/BrickOwlSharp.Client.csproj index 87f28cf..ca37ef1 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/IBrickOwlClient.cs b/BrickOwlSharp.Client/IBrickOwlClient.cs index 75f206a..a0ef0ed 100644 --- a/BrickOwlSharp.Client/IBrickOwlClient.cs +++ b/BrickOwlSharp.Client/IBrickOwlClient.cs @@ -33,7 +33,13 @@ namespace BrickOwlSharp.Client { public interface IBrickOwlClient { - Task> GetOrdersAsync(CancellationToken cancellationToken = default); + Task> GetOrdersAsync( + OrderStatus? orderStatusFilter = null, + DateTime? minOrderTime = null, + int? limit = null, + OrderType? orderType = null, + OrderSortType? orderSortType = null, + CancellationToken cancellationToken = default); Task GetOrderAsync(int orderId, CancellationToken cancellationToken = default); Task UpdateOrderStatusAsync(int orderId, OrderStatus status, CancellationToken cancellationToken = default); Task> GetWishlistsAsync(CancellationToken cancellationToken = default); diff --git a/BrickOwlSharp.Client/OrderSortType.cs b/BrickOwlSharp.Client/OrderSortType.cs new file mode 100644 index 0000000..3faa0e7 --- /dev/null +++ b/BrickOwlSharp.Client/OrderSortType.cs @@ -0,0 +1,36 @@ +#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; + +namespace BrickOwlSharp.Client +{ + public enum OrderSortType + { + Created, + Updated + } +} diff --git a/BrickOwlSharp.Client/OrderType.cs b/BrickOwlSharp.Client/OrderType.cs new file mode 100644 index 0000000..5727844 --- /dev/null +++ b/BrickOwlSharp.Client/OrderType.cs @@ -0,0 +1,36 @@ +#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; + +namespace BrickOwlSharp.Client +{ + public enum OrderType + { + Placed, + Received + } +} diff --git a/BrickOwlSharp.Demos/OrderDemo.cs b/BrickOwlSharp.Demos/OrderDemo.cs new file mode 100644 index 0000000..e000424 --- /dev/null +++ b/BrickOwlSharp.Demos/OrderDemo.cs @@ -0,0 +1,48 @@ +#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 OrderDemo +{ + + internal async Task RunAsync() + { + IBrickOwlClient client = BrickOwlClientFactory.Build(); + List allOrders = await client.GetOrdersAsync(orderSortType: OrderSortType.Updated); + + var table = new Table(); + table.AddColumn("Id"); + table.AddColumn("Date"); + table.AddColumn("State"); + + foreach (BrickOwlSharp.Client.Order order in allOrders) + { + table.AddRow(order.Id.ToString(), order.OrderDate.ToShortDateString(), order.Status); + } + + AnsiConsole.Write(table); + } +} diff --git a/BrickOwlSharp.Demos/Program.cs b/BrickOwlSharp.Demos/Program.cs index 6e1b6fb..dc117cc 100644 --- a/BrickOwlSharp.Demos/Program.cs +++ b/BrickOwlSharp.Demos/Program.cs @@ -27,27 +27,28 @@ internal static class Program { - static async Task Main() + private static async Task Main() { BrickOwlClientConfiguration.Instance.ApiKey = System.IO.File.ReadAllText("apikey.txt"); /* WishlistDemo demo = new WishlistDemo(); demo.Run(); - */ - InventoryDemo demo = new InventoryDemo(); await demo.RunAsync(); - /* CatalogDemo catalogDemo = new CatalogDemo(); - catalogDemo.Run(); - */ + catalogDemo.Run(); ColorDemo colorDemo = new ColorDemo(); await colorDemo.RunAsync(); + */ + + OrderDemo orderDemo = new OrderDemo(); + await orderDemo.RunAsync(); + return 0; } }