Skip to content

Commit 49f90d5

Browse files
committed
2 parents 6d7c753 + 938cef3 commit 49f90d5

14 files changed

+140
-87
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential()
158158
});
159159

160160
var orders = amazonConnection.Orders.GetOrders
161-
(
162-
new FikaAmazonAPI.Parameter.Order.ParameterOrderList()
163-
{
164-
TestCase = Constants.TestCase200
165-
}
166-
);
161+
(
162+
new FikaAmazonAPI.Parameter.Order.ParameterOrderList
163+
{
164+
TestCase = Constants.TestCase200
165+
}
166+
);
167167
```
168168

169169
### Report List, For more report sample please check [Here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Source/FikaAmazonAPI.Test/Orders.cs).

Source/FikaAmazonAPI.SampleCode/SandboxOrderSample.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public void GetOrderTestCase200()
1919
{
2020
var orders = amazonConnection.Orders.GetOrders
2121
(
22-
new FikaAmazonAPI.Parameter.Order.ParameterOrderList(Constants.TestCase200)
22+
new FikaAmazonAPI.Parameter.Order.ParameterOrderList
23+
{
24+
TestCase = Constants.TestCase200
25+
}
2326
);
2427
}
2528
}

Source/FikaAmazonAPI/Parameter/IHasParameterizedTestCase.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

Source/FikaAmazonAPI/Parameter/ListingsItems/ParameterDeleteListingItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FikaAmazonAPI.Search;
2+
using FikaAmazonAPI.Utils;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -9,9 +10,10 @@ namespace FikaAmazonAPI.Parameter.ListingItem
910
{
1011
public class ParameterDeleteListingItem: ParameterBased
1112
{
12-
1313
public bool Check()
1414
{
15+
if (TestCase == Constants.TestCase400)
16+
sku = "BadSKU";
1517
if (string.IsNullOrWhiteSpace(this.sellerId))
1618
{
1719
throw new InvalidDataException("SellerId is a required property for ParameterDeleteListingItem and cannot be null");

Source/FikaAmazonAPI/Parameter/ListingsItems/ParameterGetListingsItem.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,49 @@
44
using Newtonsoft.Json.Converters;
55
using System;
66
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
79
using System.Text;
10+
using static FikaAmazonAPI.Utils.Constants;
811

9-
namespace FikaAmazonAPI.Parameter.ListingsItems
12+
namespace FikaAmazonAPI.Parameter.ListingItem
1013
{
11-
public class ParameterGetListingsItem : ParameterBased, IHasParameterizedTestCase
14+
public class ParameterGetListingsItem : ParameterBased
1215
{
13-
public ParameterGetListingsItem(string testCase = "")
16+
public ParameterGetListingsItem()
1417
{
15-
if (!string.IsNullOrEmpty(testCase))
18+
}
19+
20+
public bool Check()
21+
{
22+
if (TestCase == TestCase400)
23+
sku = "BadSKU";
24+
if (string.IsNullOrWhiteSpace(sellerId))
25+
{
26+
throw new InvalidDataException("SellerId is a required property for ParameterPutListingItem and cannot be null");
27+
}
28+
if (string.IsNullOrWhiteSpace(sku))
29+
{
30+
throw new InvalidDataException("Sku is a required property for ParameterPutListingItem and cannot be null");
31+
}
32+
if (marketplaceIds == null || !marketplaceIds.Any())
1633
{
17-
TestCase = testCase;
18-
SandboxQueryParameters = Sandbox.SandboxQueryParameters<ParameterGetListingsItem>(TestCase);
34+
throw new InvalidDataException("MarketplaceIds is a required property for ParameterPutListingItem and cannot be null");
1935
}
36+
if (includedData is null)
37+
{
38+
includedData = new List<ListingsIncludedData>();
39+
includedData.Add(ListingsIncludedData.Summaries);
40+
}
41+
if (includedData.Count == 0)
42+
includedData.Add(ListingsIncludedData.Summaries);
43+
return true;
2044
}
2145

46+
public string sellerId { get; set; }
47+
48+
public string sku { get; set; }
49+
2250
/// <summary>
2351
/// A list of MarketplaceId values. Used to select orders that were placed in the specified marketplaces. Max count : 50
2452
/// </summary>
@@ -34,10 +62,5 @@ public ParameterGetListingsItem(string testCase = "")
3462
/// A comma-delimited list of data sets to include in the response. Default: summaries.
3563
/// </summary>
3664
public IList<Constants.ListingsIncludedData> includedData { get; set; }
37-
38-
[IgnoreToAddParameter]
39-
public string TestCase { get; set; }
40-
41-
public Dictionary<string, List<KeyValuePair<string, string>>> SandboxQueryParameters { get; }
4265
}
4366
}

Source/FikaAmazonAPI/Parameter/ListingsItems/ParameterPatchListingItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
22
using FikaAmazonAPI.Search;
3+
using FikaAmazonAPI.Utils;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
@@ -12,6 +13,8 @@ public class ParameterPatchListingItem : ParameterBased
1213
{
1314
public bool Check()
1415
{
16+
if (TestCase == Constants.TestCase400)
17+
sku = "BadSKU";
1518
if (string.IsNullOrWhiteSpace(this.sellerId))
1619
{
1720
throw new InvalidDataException("SellerId is a required property for ParameterPatchListingItem and cannot be null");

Source/FikaAmazonAPI/Parameter/ListingsItems/ParameterPutListingItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
22
using FikaAmazonAPI.Search;
3+
using FikaAmazonAPI.Utils;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
@@ -12,6 +13,8 @@ public class ParameterPutListingItem : ParameterBased
1213
{
1314
public bool Check()
1415
{
16+
if (TestCase == Constants.TestCase400)
17+
sku = "BadSKU";
1518
if (string.IsNullOrWhiteSpace(this.sellerId))
1619
{
1720
throw new InvalidDataException("SellerId is a required property for ParameterPutListingItem and cannot be null");

Source/FikaAmazonAPI/Parameter/Order/ParameterOrderList.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@
99

1010
namespace FikaAmazonAPI.Parameter.Order
1111
{
12-
public class ParameterOrderList : ParameterBased, IParameterBasedPII, IHasParameterizedTestCase
12+
public class ParameterOrderList : ParameterBased, IParameterBasedPII
1313
{
14-
public ParameterOrderList(string testCase = "")
14+
public ParameterOrderList()
1515
{
16-
if (!string.IsNullOrEmpty(testCase))
17-
{
18-
TestCase = testCase;
19-
SandboxQueryParameters = Sandbox.SandboxQueryParameters<ParameterOrderList>(TestCase);
20-
}
2116
}
2217

2318
/// <summary>
@@ -93,8 +88,5 @@ public ParameterOrderList(string testCase = "")
9388
public string StoreChainStoreId { get; set; }
9489
public bool IsNeedRestrictedDataToken { get; set; }
9590
public CreateRestrictedDataTokenRequest RestrictedDataTokenRequest { get; set; }
96-
public string TestCase { get; set; }
97-
98-
public Dictionary<string, List<KeyValuePair<string, string>>> SandboxQueryParameters { get; }
9991
}
10092
}

Source/FikaAmazonAPI/Parameter/ParameterBased.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,21 @@ namespace FikaAmazonAPI.Search
1212
{
1313
public class ParameterBased
1414
{
15-
public virtual List<KeyValuePair<string, string>> getParameters(bool isSandbox = false)
15+
[IgnoreToAddParameter]
16+
public string TestCase { get; set; }
17+
18+
[IgnoreToAddParameter]
19+
internal Dictionary<string, List<KeyValuePair<string, string>>> SandboxQueryParameters { get; private set; }
20+
21+
public virtual List<KeyValuePair<string, string>> getParameters()
1622
{
1723
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
18-
Type t = this.GetType(); // Where obj is object whose properties you need.
19-
if (isSandbox)
24+
if (!string.IsNullOrEmpty(TestCase))
2025
{
21-
if (typeof(IHasParameterizedTestCase).IsAssignableFrom(t))
22-
{
23-
var queryParametersProperties = t.GetInterfaces().FirstOrDefault(x => x == typeof(IHasParameterizedTestCase)).GetProperties();
24-
var testCasePropertyValue = queryParametersProperties.FirstOrDefault(x => x.Name == nameof(IHasParameterizedTestCase.TestCase))?.GetValue(this);
25-
if (testCasePropertyValue != null && testCasePropertyValue is string testCase)
26-
{
27-
var sandboxQueryParametersPropertyValue = queryParametersProperties.FirstOrDefault(x => x.Name == nameof(IHasParameterizedTestCase.SandboxQueryParameters))?.GetValue(this);
28-
if (sandboxQueryParametersPropertyValue != null && sandboxQueryParametersPropertyValue is Dictionary<string, List<KeyValuePair<string, string>>> sandboxQueryParameters)
29-
{
30-
if (sandboxQueryParameters.ContainsKey(testCase))
31-
return sandboxQueryParameters[testCase];
32-
}
33-
34-
}
35-
}
26+
SandboxQueryParameters = Sandbox.SandboxQueryParameters(this, TestCase);
27+
return SandboxQueryParameters[TestCase];
3628
}
37-
PropertyInfo[] pi = t.GetProperties();
29+
PropertyInfo[] pi = this.GetType().GetProperties();
3830
foreach (PropertyInfo p in pi)
3931
{
4032
if (p.CustomAttributes.Any(x => x.AttributeType == typeof(IgnoreToAddParameterAttribute))) continue;

Source/FikaAmazonAPI/Services/ApiUrls.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ protected class ListingsItemsApiUrls
470470
//https://stackoverflow.com/questions/575440/url-encoding-using-c-sharp/21771206#21771206
471471
public static string GetListingItem(string seller, string sku) => $"{_resourceBaseUrl}/items/{seller}/{WebUtility.UrlEncode(sku)}";
472472

473-
474473
public static string PutListingItem(string seller, string sku) => $"{_resourceBaseUrl}/items/{seller}/{WebUtility.UrlEncode(sku)}";
475474

476475
public static string DeleteListingItem(string seller, string sku) => $"{_resourceBaseUrl}/items/{seller}/{WebUtility.UrlEncode(sku)}";

Source/FikaAmazonAPI/Services/ListingsItemService.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Text;
44
using FikaAmazonAPI.AmazonSpApiSDK.Models.ListingsItems;
55
using FikaAmazonAPI.Parameter.ListingItem;
6-
using FikaAmazonAPI.Parameter.ListingsItems;
76
using FikaAmazonAPI.Utils;
87
using static FikaAmazonAPI.Utils.Constants;
98

@@ -15,26 +14,12 @@ public ListingsItemService(AmazonCredential amazonCredential) : base(amazonCrede
1514
{
1615
}
1716

18-
public Item GetListingsItem(string sellerId, string sku, ParameterGetListingsItem listingsItemParameters)
17+
public Item GetListingsItem(ParameterGetListingsItem listingsItemParameters)
1918
{
20-
if (string.IsNullOrEmpty(sellerId))
21-
throw new ArgumentException($"'{nameof(sellerId)}' cannot be null or empty.", nameof(sellerId));
22-
23-
if (string.IsNullOrEmpty(sku))
24-
throw new ArgumentException($"'{nameof(sku)}' cannot be null or empty.", nameof(sku));
25-
26-
if (listingsItemParameters.includedData is null)
27-
{
28-
listingsItemParameters.includedData = new List<ListingsIncludedData>();
29-
listingsItemParameters.includedData.Add(ListingsIncludedData.Summaries);
30-
}
31-
if (listingsItemParameters.includedData.Count == 0)
32-
listingsItemParameters.includedData.Add(ListingsIncludedData.Summaries);
33-
var queryParameters = listingsItemParameters.getParameters(AmazonCredential.Environment == Environments.Sandbox);
34-
CreateAuthorizedRequest(ListingsItemsApiUrls.GetListingItem(sellerId, sku), RestSharp.Method.GET, queryParameters, parameter: listingsItemParameters);
35-
var response = ExecuteRequest<Item>();
36-
37-
return response;
19+
listingsItemParameters.Check();
20+
var queryParameters = listingsItemParameters.getParameters();
21+
CreateAuthorizedRequest(ListingsItemsApiUrls.GetListingItem(listingsItemParameters.sellerId, listingsItemParameters.sku), RestSharp.Method.GET, queryParameters, parameter: listingsItemParameters);
22+
return ExecuteRequest<Item>();
3823
}
3924

4025
public ListingsItemSubmissionResponse PutListingsItem(ParameterPutListingItem parameterPutListingItem)

Source/FikaAmazonAPI/Services/OrderService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public OrderList GetOrders(ParameterOrderList searchOrderList)
3030
searchOrderList.MarketplaceIds=new List<string>();
3131
searchOrderList.MarketplaceIds.Add(AmazonCredential.MarketPlace.ID);
3232
}
33-
var queryParameters = searchOrderList.getParameters(this.AmazonCredential.Environment == Constants.Environments.Sandbox);
33+
var queryParameters = searchOrderList.getParameters();
3434

3535
CreateAuthorizedRequest(OrdersApiUrls.Orders, RestSharp.Method.GET, queryParameters,parameter: searchOrderList);
3636
var response = ExecuteRequest<GetOrdersResponse>();

Source/FikaAmazonAPI/Utils/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Constants
1212
public readonly static string AmazonToeknEndPoint = "https://api.amazon.com/auth/o2/token";
1313
public readonly static string DateISO8601Format = "yyyy-MM-ddTHH:mm:ss.fffZ";
1414
public readonly static string TestCase200 = "TEST_CASE_200";
15+
public readonly static string TestCase400 = "TEST_CASE_400";
1516

1617
[JsonConverter(typeof(StringEnumConverter))]
1718
public enum Environments

Source/FikaAmazonAPI/Utils/Sandbox.cs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FikaAmazonAPI.Parameter;
2-
using FikaAmazonAPI.Parameter.ListingsItems;
2+
using FikaAmazonAPI.Parameter.ListingItem;
33
using FikaAmazonAPI.Parameter.Order;
4+
using FikaAmazonAPI.Search;
45
using System;
56
using System.Collections.Generic;
67
using System.Text;
@@ -9,10 +10,10 @@ namespace FikaAmazonAPI.Utils
910
{
1011
internal static class Sandbox
1112
{
12-
public static Dictionary<string, List<KeyValuePair<string, string>>> SandboxQueryParameters<T>(string testCase) where T : IHasParameterizedTestCase
13+
public static Dictionary<string, List<KeyValuePair<string, string>>> SandboxQueryParameters<T>(T instance, string testCase) where T : ParameterBased
1314
{
1415
var queryParameters = new Dictionary<string, List<KeyValuePair<string, string>>>();
15-
if (typeof(T) == typeof(ParameterOrderList))
16+
if (instance is ParameterOrderList parameterOrderList)
1617
{
1718
if (testCase.Equals(Constants.TestCase200))
1819
{
@@ -23,11 +24,72 @@ public static Dictionary<string, List<KeyValuePair<string, string>>> SandboxQuer
2324
});
2425
}
2526
}
26-
else if (typeof(T) == typeof(ParameterGetListingsItem))
27+
else if (instance is ParameterGetListingsItem parameterGetListingsItem)
2728
{
2829
if (testCase.Equals(Constants.TestCase200))
2930
{
30-
31+
queryParameters.Add(Constants.TestCase200, new List<KeyValuePair<string, string>>()
32+
{
33+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterGetListingsItem.marketplaceIds)),
34+
});
35+
}
36+
else if (testCase.Equals(Constants.TestCase400))
37+
{
38+
queryParameters.Add(Constants.TestCase400, new List<KeyValuePair<string, string>>()
39+
{
40+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterGetListingsItem.marketplaceIds))
41+
});
42+
}
43+
}
44+
else if (instance is ParameterPutListingItem parameterPutListingsItem)
45+
{
46+
if (testCase.Equals(Constants.TestCase200))
47+
{
48+
queryParameters.Add(Constants.TestCase200, new List<KeyValuePair<string, string>>()
49+
{
50+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterPutListingsItem.marketplaceIds)),
51+
});
52+
}
53+
else if (testCase.Equals(Constants.TestCase400))
54+
{
55+
queryParameters.Add(Constants.TestCase400, new List<KeyValuePair<string, string>>()
56+
{
57+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterPutListingsItem.marketplaceIds))
58+
});
59+
}
60+
}
61+
else if (instance is ParameterDeleteListingItem parameterDeleteListingsItem)
62+
{
63+
if (testCase.Equals(Constants.TestCase200))
64+
{
65+
queryParameters.Add(Constants.TestCase200, new List<KeyValuePair<string, string>>()
66+
{
67+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterDeleteListingsItem.marketplaceIds)),
68+
});
69+
}
70+
else if (testCase.Equals(Constants.TestCase400))
71+
{
72+
queryParameters.Add(Constants.TestCase400, new List<KeyValuePair<string, string>>()
73+
{
74+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterDeleteListingsItem.marketplaceIds))
75+
});
76+
}
77+
}
78+
else if (instance is ParameterPatchListingItem parameterPatchListingItem)
79+
{
80+
if (testCase.Equals(Constants.TestCase200))
81+
{
82+
queryParameters.Add(Constants.TestCase200, new List<KeyValuePair<string, string>>()
83+
{
84+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterPatchListingItem.marketplaceIds)),
85+
});
86+
}
87+
else if (testCase.Equals(Constants.TestCase400))
88+
{
89+
queryParameters.Add(Constants.TestCase400, new List<KeyValuePair<string, string>>()
90+
{
91+
new KeyValuePair<string, string>("marketplaceIds", string.Join(",", parameterPatchListingItem.marketplaceIds))
92+
});
3193
}
3294
}
3395

0 commit comments

Comments
 (0)