forked from stepanbenes/api-for-nopcommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
116 lines (94 loc) · 3.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using ApiBindings.NopApi;
using ApiBindings.NopApi.DTOs;
namespace ClientApp
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Types in this assembly:");
foreach (Type t in typeof(Program).Assembly.GetTypes())
{
Console.WriteLine(t.FullName);
}
// Create api client
var cookieContainer = new CookieContainer();
using var handler = new HttpClientHandler() { CookieContainer = cookieContainer, UseCookies = true };
using var httpClient = new HttpClient(handler) { BaseAddress = new Uri(args[0]) };
INopApiClient nopApiClient = new NopApiClient(httpClient);
try
{
Console.WriteLine("Authenticating...");
var tokenResponse = await nopApiClient.RequestToken(new TokenRequest { Username = args[1], Password = args[2], RememberMe = true });
if (tokenResponse is { AccessToken: string token, TokenType: var type })
{
nopApiClient.SetAuthorizationHeader(type ?? "Bearer", token);
//nopApiClient.RemoveAuthorizationHeader();
}
Console.WriteLine("Requesting categories...");
var categories = await nopApiClient.GetCategories();
if (categories?.Categories is not null)
{
foreach (var category in categories.Categories)
{
Console.WriteLine(category.ToString());
}
}
Console.WriteLine("Requesting countries...");
var countries = await nopApiClient.GetCountries();
if (countries?.Countries is not null)
{
foreach (var country in countries.Countries)
{
Console.WriteLine(country.ToString());
}
}
Console.WriteLine("Adding product...");
var createProductResult = await nopApiClient.CreateProduct(new ProductDtoDelta { Product = new ProductDto { Name = "Test product", ShortDescription = "The best product" } });
var newProduct = createProductResult?.Products?.SingleOrDefault();
Console.WriteLine("Creating shopping cart item...");
var result = await nopApiClient.CreateShoppingCartItem(new ShoppingCartItemDtoDelta
{
ShoppingCartItem = new ShoppingCartItemDto(ShoppingCartType.ShoppingCart)
{
CustomerId = tokenResponse?.CustomerId,
ProductId = newProduct?.Id,
Quantity = 2
}
});
var newShoppingCartItem = result?.ShoppingCarts?.SingleOrDefault();
result = await nopApiClient.GetShoppingCartItems(CustomerId: tokenResponse?.CustomerId);
if (newShoppingCartItem is not null)
{
Console.WriteLine("Updating shopping cart item...");
_ = await nopApiClient.UpdateShoppingCartItem(new ShoppingCartItemDtoDelta { ShoppingCartItem = newShoppingCartItem with { Quantity = 3 }, }, newShoppingCartItem.Id.ToString());
}
result = await nopApiClient.GetShoppingCartItems(CustomerId: tokenResponse?.CustomerId);
if (newShoppingCartItem is not null)
{
Console.WriteLine("Deleting shopping cart item...");
await nopApiClient.DeleteShoppingCartItem(newShoppingCartItem.Id);
//await nopApiClient.DeleteShoppingCartItems(Ids: new[] { newShoppingCartItem.Id });
}
result = await nopApiClient.GetShoppingCartItems(CustomerId: tokenResponse?.CustomerId);
if (newProduct is not null)
{
Console.WriteLine("Deleting product...");
await nopApiClient.DeleteProduct(newProduct.Id);
}
//var invoiceDocument = await nopApiClient.GetPdfInvoice(orderId: 1);
}
catch (ApiBindings.ApiException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
}