Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,36 @@ AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential()

### 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.***

The `Proxy` property accepts any `IWebProxy` implementation:
```csharp
AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential()
{
ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
RefreshToken= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
MarketPlaceID = "A2VIGQ35RCS4UG",
Proxy = new System.Net.WebProxy("http://xxx.xxx.xxx.xxx:xxxx"),
});
```

For an authenticated proxy, assign any `IWebProxy` implementation -- for example, a `WebProxy` with credentials:
```csharp
AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential()
{
ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
RefreshToken= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
MarketPlaceID = "A2VIGQ35RCS4UG",
Proxy = new System.Net.WebProxy("http://xxx.xxx.xxx.xxx:xxxx")
{
Credentials = new System.Net.NetworkCredential("username", "password")
},
});
```
> ***Proxy is not required and will operate normally without it being set.***

---

### Order List
For more order samples, please check [Here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Source/FikaAmazonAPI.SampleCode/OrdersSample.cs).
Expand Down
18 changes: 15 additions & 3 deletions Source/FikaAmazonAPI/AmazonCredential.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
using FikaAmazonAPI.Utils;
using System;
using System.Collections.Generic;
using System.Net;
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
using static FikaAmazonAPI.Utils.Constants;

Expand All @@ -23,21 +25,31 @@
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; }
Comment thread
ProNotion marked this conversation as resolved.

[Obsolete("Use the Proxy property instead.")]

Check warning on line 30 in Source/FikaAmazonAPI/AmazonCredential.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=abuzuhri_Amazon-SP-API-CSharp&issues=AZ2pam5Dj_AFMRFd1A82&open=AZ2pam5Dj_AFMRFd1A82&pullRequest=925
public string ProxyAddress
{
get => (Proxy as WebProxy)?.Address?.ToString();
set => Proxy = string.IsNullOrWhiteSpace(value) ? null : new WebProxy(value);
}

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, string ProxyAddress = null)
{
this.AccessKey = AccessKey;
this.SecretKey = SecretKey;
this.RoleArn = RoleArn;
this.ClientId = ClientId;
this.ClientSecret = ClientSecret;
this.RefreshToken = RefreshToken;
this.ProxyAddress = ProxyAddress;
this.Proxy = string.IsNullOrWhiteSpace(ProxyAddress) ? null : new WebProxy(ProxyAddress);
CacheTokenData = new CacheTokenData();
}

Expand Down
25 changes: 6 additions & 19 deletions Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
using FikaAmazonAPI.RestSharp;
using Newtonsoft.Json;
using FikaAmazonAPI.RestSharp;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
Expand All @@ -20,29 +20,16 @@ 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))
{
var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
RestClient = new RestClient(options);
}
else
var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority))
{
var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority))
{
Proxy = new System.Net.WebProxy()
{
Address = new Uri(proxyAddress)
}
};

RestClient = new RestClient(options);
}
Proxy = proxy
};
RestClient = new RestClient(options);
}


Expand Down
23 changes: 5 additions & 18 deletions Source/FikaAmazonAPI/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,12 @@

private void CreateRequest(string url, RestSharp.Method method)
{
if (string.IsNullOrWhiteSpace(AmazonCredential.ProxyAddress))
var options = new RestClientOptions(ApiBaseUrl)
{
var options = new RestClientOptions(ApiBaseUrl);
RequestClient = new RestClient(options,
configureSerialization: s => s.UseNewtonsoftJson());
}
else
{
var options = new RestClientOptions(ApiBaseUrl)
{
Proxy = new System.Net.WebProxy()
{
Address = new Uri(AmazonCredential.ProxyAddress)
}
};

RequestClient = new RestClient(options,
configureSerialization: s => s.UseNewtonsoftJson());
}
Proxy = AmazonCredential.Proxy
};
RequestClient = new RestClient(options,
configureSerialization: s => s.UseNewtonsoftJson());

Request = new RestRequest(url, method);
}
Expand Down Expand Up @@ -224,7 +211,7 @@
{
return await ExecuteRequestTry<T>(rateLimitType, cancellationToken);
}
catch (AmazonQuotaExceededException ex)

Check warning on line 214 in Source/FikaAmazonAPI/Services/RequestService.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unused local variable 'ex'.

See more on https://sonarcloud.io/project/issues?id=abuzuhri_Amazon-SP-API-CSharp&issues=AZ2pYWvXs4jV0PpjMGLl&open=AZ2pYWvXs4jV0PpjMGLl&pullRequest=925
{
if (tryCount >= AmazonCredential.MaxThrottledRetryCount)
{
Expand Down Expand Up @@ -276,7 +263,7 @@
}
}
}
catch (Exception e)

Check warning on line 266 in Source/FikaAmazonAPI/Services/RequestService.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unused local variable 'e'.

See more on https://sonarcloud.io/project/issues?id=abuzuhri_Amazon-SP-API-CSharp&issues=AZ2pYWvXs4jV0PpjMGLk&open=AZ2pYWvXs4jV0PpjMGLk&pullRequest=925
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/FikaAmazonAPI/Services/TokenGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static async Task<TokenResponse> RefreshAccessTokenAsync(AmazonCredential
if (tokenDataType == TokenDataType.Grantless)
lwaCredentials.Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI };

var Client = new LWAClient(lwaCredentials, credentials.ProxyAddress);
var accessToken = await Client.GetAccessTokenAsync(cancellationToken);
var client = new LWAClient(lwaCredentials, credentials.Proxy);
var accessToken = await client.GetAccessTokenAsync(cancellationToken);

return accessToken;
}
Expand Down
Loading