Skip to content

Commit

Permalink
More comprehensive implementation of order API
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanstapel committed Jan 21, 2025
1 parent 9fc43fa commit 60f09ab
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 10 deletions.
42 changes: 40 additions & 2 deletions BrickOwlSharp.Client/BrickOwlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,47 @@ private void Dispose(bool disposing)


public async Task<List<Order>> 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<Order> result = await ExecuteGet<List<Order>>(url, cancellationToken);
_measureRequest(ResourceType.Order, cancellationToken);
return result;
Expand Down Expand Up @@ -385,7 +423,7 @@ private async Task<TResponse> ExecutePost<TResponse>(string url, Dictionary<stri
} // !ExecutePost()


Dictionary<string, string> _ObjectToFormData(object o, bool addKey = true)
private Dictionary<string, string> _ObjectToFormData(object o, bool addKey = true)
{
Dictionary<string, string> result = new Dictionary<string, string>();

Expand Down Expand Up @@ -453,4 +491,4 @@ private async void _measureRequest(ResourceType resourceType, CancellationToken
}
}
}
}
}
2 changes: 1 addition & 1 deletion BrickOwlSharp.Client/BrickOwlSharp.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ With BrickOwlSharp, developers can easily integrate BrickOwl into their applicat
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="9.0.1" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion BrickOwlSharp.Client/IBrickOwlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ namespace BrickOwlSharp.Client
{
public interface IBrickOwlClient
{
Task<List<Order>> GetOrdersAsync(CancellationToken cancellationToken = default);
Task<List<Order>> GetOrdersAsync(
OrderStatus? orderStatusFilter = null,
DateTime? minOrderTime = null,
int? limit = null,
OrderType? orderType = null,
OrderSortType? orderSortType = null,
CancellationToken cancellationToken = default);
Task<OrderDetails> GetOrderAsync(int orderId, CancellationToken cancellationToken = default);
Task<bool> UpdateOrderStatusAsync(int orderId, OrderStatus status, CancellationToken cancellationToken = default);
Task<List<Wishlist>> GetWishlistsAsync(CancellationToken cancellationToken = default);
Expand Down
36 changes: 36 additions & 0 deletions BrickOwlSharp.Client/OrderSortType.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
36 changes: 36 additions & 0 deletions BrickOwlSharp.Client/OrderType.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
48 changes: 48 additions & 0 deletions BrickOwlSharp.Demos/OrderDemo.cs
Original file line number Diff line number Diff line change
@@ -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<BrickOwlSharp.Client.Order> 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);
}
}
13 changes: 7 additions & 6 deletions BrickOwlSharp.Demos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,28 @@

internal static class Program
{
static async Task<int> Main()
private static async Task<int> 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;
}
}

0 comments on commit 60f09ab

Please sign in to comment.