diff --git a/README.md b/README.md index cd2b7731..88846a00 100644 --- a/README.md +++ b/README.md @@ -158,19 +158,36 @@ var amazonConnection = connectionFactory.RequestScopedConnection( ### Configuration using a proxy Please see [here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Source/FikaAmazonAPI.SampleCode/Program.cs) for the relevant code file. ->```csharp ->AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential() ->{ -> ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXXXXXXX", -> ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", -> RefreshToken= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", -> MarketPlaceID = "A2VIGQ35RCS4UG", -> ProxyAddress = "http(s)://xxx.xxx.xxx.xxx:xxxx", ->}); ->``` ->> * Assign your proxy address to the ProxyAddress Property and you'll be able to use a proxy account. ->> ->> ***This is not required and will operate normally without the ProxyAddress being set.*** +```csharp +AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential() +{ + ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXXXXXXX", + ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + RefreshToken= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + MarketPlaceID = "A2VIGQ35RCS4UG", + ProxyAddress = "http(s)://xxx.xxx.xxx.xxx:xxxx", +}); +``` +> * Assign your proxy address to the ProxyAddress Property and you'll be able to use a proxy account. + + +Alternatively, you may provide an instance of `IWebProxy` giving you more control over the proxy configuration including the ability to configure credentials: +```csharp +AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential() +{ + ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXXXXXXX", + ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + RefreshToken= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + MarketPlaceID = "A2VIGQ35RCS4UG", + Proxy = new WebProxy("http(s)://xxx.xxx.xxx.xxx", xxxx) + { + Credentials = new NetworkCredential("username", "password") + }; +}); +``` +A proxy is not required and things will operate normally without a `ProxyAddress` or `Proxy` being set. + +--- ### Order List, For more orders sample please check [Here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Source/FikaAmazonAPI.SampleCode/ReportsSample.cs). ```CSharp diff --git a/Source/FikaAmazonAPI/AmazonCredential.cs b/Source/FikaAmazonAPI/AmazonCredential.cs index 5ac49fb2..bec47903 100644 --- a/Source/FikaAmazonAPI/AmazonCredential.cs +++ b/Source/FikaAmazonAPI/AmazonCredential.cs @@ -1,5 +1,7 @@ using FikaAmazonAPI.AmazonSpApiSDK.Models.Token; using FikaAmazonAPI.Utils; +using System.Collections.Generic; +using System.Net; using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData; using static FikaAmazonAPI.Utils.Constants; @@ -22,13 +24,13 @@ public class AmazonCredential public bool IsDebugMode { get; set; } public string MarketPlaceID { get; set; } public string SellerID { get; set; } - public string ProxyAddress { get; set; } + public IWebProxy Proxy { get; set; } public static bool DebugMode { get; set; } public AmazonCredential() { CacheTokenData = new CacheTokenData(); } - public AmazonCredential(string AccessKey, string SecretKey, string RoleArn, string ClientId, string ClientSecret, string RefreshToken, string ProxyAddress = null) + public AmazonCredential(string AccessKey, string SecretKey, string RoleArn, string ClientId, string ClientSecret, string RefreshToken, IWebProxy proxy) { this.AccessKey = AccessKey; this.SecretKey = SecretKey; @@ -36,10 +38,15 @@ public AmazonCredential(string AccessKey, string SecretKey, string RoleArn, stri this.ClientId = ClientId; this.ClientSecret = ClientSecret; this.RefreshToken = RefreshToken; - this.ProxyAddress = ProxyAddress; + this.Proxy = proxy; CacheTokenData = new CacheTokenData(); } + public AmazonCredential(string AccessKey, string SecretKey, string RoleArn, string ClientId, + string ClientSecret, string RefreshToken, string proxyAddress = null) + : this(AccessKey, SecretKey, RoleArn, ClientId, ClientSecret, RefreshToken, + string.IsNullOrEmpty(proxyAddress) ? null : new WebProxy(proxyAddress)) { } + public TokenResponse GetToken(TokenDataType tokenDataType) { return CacheTokenData.GetToken(tokenDataType); diff --git a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs index 5356990d..59d41397 100644 --- a/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs +++ b/Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs @@ -3,6 +3,7 @@ using RestSharp; using System; using System.IO; +using System.Net; using System.Threading; using System.Threading.Tasks; @@ -18,23 +19,20 @@ public class LWAClient public LWAAuthorizationCredentials LWAAuthorizationCredentials { get; private set; } - public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials, string proxyAddress = null) + public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials, IWebProxy proxy = null) { LWAAuthorizationCredentials = lwaAuthorizationCredentials; LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder(); // RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); - if (string.IsNullOrWhiteSpace(proxyAddress)) + if (proxy == null) { RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)); }else { var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority)) { - Proxy = new System.Net.WebProxy() - { - Address = new Uri(proxyAddress) - } + Proxy = proxy }; RestClient = new RestClient(options); diff --git a/Source/FikaAmazonAPI/Services/RequestService.cs b/Source/FikaAmazonAPI/Services/RequestService.cs index 1339ef1c..02ffddd5 100644 --- a/Source/FikaAmazonAPI/Services/RequestService.cs +++ b/Source/FikaAmazonAPI/Services/RequestService.cs @@ -62,7 +62,7 @@ public RequestService(AmazonCredential amazonCredential,ILoggerFactory? loggerFa private void CreateRequest(string url, RestSharp.Method method) { - if (string.IsNullOrWhiteSpace(AmazonCredential.ProxyAddress)) + if (AmazonCredential.Proxy == null) { var options = new RestClientOptions(ApiBaseUrl); RequestClient = new RestClient(options, @@ -72,10 +72,7 @@ private void CreateRequest(string url, RestSharp.Method method) { var options = new RestClientOptions(ApiBaseUrl) { - Proxy = new System.Net.WebProxy() - { - Address = new Uri(AmazonCredential.ProxyAddress) - } + Proxy = AmazonCredential.Proxy }; RequestClient = new RestClient(options, diff --git a/Source/FikaAmazonAPI/Services/TokenGeneration.cs b/Source/FikaAmazonAPI/Services/TokenGeneration.cs index 1b2c6406..e5e57260 100644 --- a/Source/FikaAmazonAPI/Services/TokenGeneration.cs +++ b/Source/FikaAmazonAPI/Services/TokenGeneration.cs @@ -29,7 +29,7 @@ public static async Task RefreshAccessTokenAsync(AmazonCredential if (tokenDataType == TokenDataType.Grantless) lwaCredentials.Scopes = new List() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI }; - var Client = new LWAClient(lwaCredentials, credentials.ProxyAddress); + var Client = new LWAClient(lwaCredentials, credentials.Proxy); var accessToken = await Client.GetAccessTokenAsync(cancellationToken); return accessToken;