Skip to content

[#661] Refactors support for proxy configuration for greater flexibility #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -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
Expand Down
13 changes: 10 additions & 3 deletions Source/FikaAmazonAPI/AmazonCredential.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,24 +24,29 @@ 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;
this.RoleArn = RoleArn;
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);
Expand Down
10 changes: 4 additions & 6 deletions Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using RestSharp;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions Source/FikaAmazonAPI/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Source/FikaAmazonAPI/Services/TokenGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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 Client = new LWAClient(lwaCredentials, credentials.Proxy);
var accessToken = await Client.GetAccessTokenAsync(cancellationToken);

return accessToken;
Expand Down
Loading