Clientside RateLimit configuration
This commit is contained in:
parent
dd129a54cc
commit
e5898b4344
|
@ -63,6 +63,21 @@ restRequest = new AWSSigV4Signer(awsAuthenticationCredentials)
|
||||||
```
|
```
|
||||||
Note the IRestRequest reference is treated as **mutable** when signed.
|
Note the IRestRequest reference is treated as **mutable** when signed.
|
||||||
|
|
||||||
|
## RateLimitConfiguration
|
||||||
|
|
||||||
|
Interface to set and get rateLimit configurations that are used with RateLimiter. RateLimiter is used on client side to restrict the rate at which requests are made. RateLimiter Configuration takes Permit, rate which requests are made and TimeOut
|
||||||
|
|
||||||
|
|
||||||
|
*Example*
|
||||||
|
```
|
||||||
|
RateLimitConfiguration rateLimitConfig = new RateLimitConfigurationOnRequests
|
||||||
|
{
|
||||||
|
RateLimitPermit = ..,
|
||||||
|
WaitTimeOutInMilliSeconds = ...
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## Resources
|
## Resources
|
||||||
This package features Mustache templates designed for use with [swagger codegen](https://swagger.io/tools/swagger-codegen/).
|
This package features Mustache templates designed for use with [swagger codegen](https://swagger.io/tools/swagger-codegen/).
|
||||||
When you build Selling Partner API Swagger models with these templates, they help generate a rich SDK with functionality to invoke Selling Partner APIs built in. The templates are located in *resources/swagger-codegen*.
|
When you build Selling Partner API Swagger models with these templates, they help generate a rich SDK with functionality to invoke Selling Partner APIs built in. The templates are located in *resources/swagger-codegen*.
|
||||||
|
@ -72,7 +87,7 @@ This package is built as a .NET Standard Library via a Visual Studio Solution wi
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
All dependencies can be installed via NuGet
|
All dependencies can be installed via NuGet
|
||||||
- RestSharp - 105.1.0
|
- RestSharp - 106.12.0
|
||||||
- Newtonsoft.Json 12.0.3
|
- Newtonsoft.Json 12.0.3
|
||||||
- NETStandard.Library 2.0.3 (platform-specific implementation requirements are documented on the [Microsoft .NET Guide](https://docs.microsoft.com/en-us/dotnet/standard/net-standard))
|
- NETStandard.Library 2.0.3 (platform-specific implementation requirements are documented on the [Microsoft .NET Guide](https://docs.microsoft.com/en-us/dotnet/standard/net-standard))
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Amazon.SellingPartnerAPIAA
|
||||||
|
{
|
||||||
|
public interface RateLimitConfiguration
|
||||||
|
{
|
||||||
|
int getRateLimitPermit();
|
||||||
|
int getTimeOut();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Amazon.SellingPartnerAPIAA
|
||||||
|
{
|
||||||
|
public class RateLimitConfigurationOnRequests : RateLimitConfiguration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* RateLimiter Permit
|
||||||
|
*/
|
||||||
|
public int RateLimitPermit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timeout for RateLimiter
|
||||||
|
*/
|
||||||
|
public int WaitTimeOutInMilliSeconds;
|
||||||
|
|
||||||
|
public int getRateLimitPermit()
|
||||||
|
{
|
||||||
|
return RateLimitPermit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeOut()
|
||||||
|
{
|
||||||
|
return WaitTimeOutInMilliSeconds;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,8 @@ using RestSharp.Portable.HttpClient;
|
||||||
using RestSharp;
|
using RestSharp;
|
||||||
{{/netStandard}}
|
{{/netStandard}}
|
||||||
using Amazon.SellingPartnerAPIAA;
|
using Amazon.SellingPartnerAPIAA;
|
||||||
|
using RateLimiter;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace {{packageName}}.Client
|
namespace {{packageName}}.Client
|
||||||
{
|
{
|
||||||
|
@ -32,6 +34,8 @@ namespace {{packageName}}.Client
|
||||||
{
|
{
|
||||||
private LWAAuthorizationSigner lwaAuthorizationSigner;
|
private LWAAuthorizationSigner lwaAuthorizationSigner;
|
||||||
private AWSSigV4Signer awsSigV4Signer;
|
private AWSSigV4Signer awsSigV4Signer;
|
||||||
|
private RateLimitConfiguration rateLimitConfig;
|
||||||
|
private TimeLimiter rateLimiter;
|
||||||
|
|
||||||
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
|
private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
|
@ -71,6 +75,11 @@ namespace {{packageName}}.Client
|
||||||
|
|
||||||
lwaAuthorizationSigner = new LWAAuthorizationSigner(Configuration.AuthorizationCredentials);
|
lwaAuthorizationSigner = new LWAAuthorizationSigner(Configuration.AuthorizationCredentials);
|
||||||
awsSigV4Signer = new AWSSigV4Signer(Configuration.AuthenticationCredentials);
|
awsSigV4Signer = new AWSSigV4Signer(Configuration.AuthenticationCredentials);
|
||||||
|
rateLimitConfig = Configuration.RateLimitConfig;
|
||||||
|
if(rateLimitConfig != null)
|
||||||
|
{
|
||||||
|
rateLimiter = TimeLimiter.GetFromMaxCountByInterval(rateLimitConfig.getRateLimitPermit(), TimeSpan.FromSeconds(1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -134,7 +143,7 @@ namespace {{packageName}}.Client
|
||||||
{{/netStandard}}
|
{{/netStandard}}
|
||||||
{{^netStandard}}
|
{{^netStandard}}
|
||||||
{{^supportsUWP}}
|
{{^supportsUWP}}
|
||||||
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
|
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentLength, param.Value.ContentType);
|
||||||
{{/supportsUWP}}
|
{{/supportsUWP}}
|
||||||
{{#supportsUWP}}
|
{{#supportsUWP}}
|
||||||
byte[] paramWriter = null;
|
byte[] paramWriter = null;
|
||||||
|
@ -187,6 +196,22 @@ namespace {{packageName}}.Client
|
||||||
RestClient.UserAgent = Configuration.UserAgent;
|
RestClient.UserAgent = Configuration.UserAgent;
|
||||||
|
|
||||||
InterceptRequest(request);
|
InterceptRequest(request);
|
||||||
|
if(rateLimitConfig != null)
|
||||||
|
{
|
||||||
|
var cancellationSource = new CancellationTokenSource(rateLimitConfig.getTimeOut());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = rateLimiter.Enqueue<IRestResponse>(() => RestClient.Execute(request), cancellationSource.Token);
|
||||||
|
InterceptResponse(request, response.Result);
|
||||||
|
return response.Result;
|
||||||
|
}
|
||||||
|
catch (AggregateException e)
|
||||||
|
{
|
||||||
|
throw new ApiException(429, "Throttled at client");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
{{#netStandard}}
|
{{#netStandard}}
|
||||||
var response = RestClient.Execute(request).Result;
|
var response = RestClient.Execute(request).Result;
|
||||||
{{/netStandard}}
|
{{/netStandard}}
|
||||||
|
@ -203,6 +228,7 @@ namespace {{packageName}}.Client
|
||||||
|
|
||||||
return (Object) response;
|
return (Object) response;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
{{#supportsAsync}}
|
{{#supportsAsync}}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Makes the asynchronous HTTP request.
|
/// Makes the asynchronous HTTP request.
|
||||||
|
@ -227,9 +253,19 @@ namespace {{packageName}}.Client
|
||||||
path, method, queryParams, postBody, headerParams, formParams, fileParams,
|
path, method, queryParams, postBody, headerParams, formParams, fileParams,
|
||||||
pathParams, contentType);
|
pathParams, contentType);
|
||||||
InterceptRequest(request);
|
InterceptRequest(request);
|
||||||
|
if (rateLimitConfig != null)
|
||||||
|
{
|
||||||
|
var cancellationSource = new CancellationTokenSource(rateLimitConfig.getTimeOut());
|
||||||
|
var response = await rateLimiter.Enqueue<IRestResponse>(() => RestClient.ExecuteTaskAsync(request), cancellationSource.Token);
|
||||||
|
InterceptResponse(request, response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var response = await RestClient.Execute{{^netStandard}}TaskAsync{{/netStandard}}(request);
|
var response = await RestClient.Execute{{^netStandard}}TaskAsync{{/netStandard}}(request);
|
||||||
InterceptResponse(request, response);
|
InterceptResponse(request, response);
|
||||||
return (Object)response;
|
return (Object)response;
|
||||||
|
}
|
||||||
}{{/supportsAsync}}
|
}{{/supportsAsync}}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -574,3 +610,4 @@ namespace {{packageName}}.Client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -275,6 +275,12 @@ namespace {{packageName}}.Client
|
||||||
/// <value>The AWSAuthenticationCredentials</value>
|
/// <value>The AWSAuthenticationCredentials</value>
|
||||||
public virtual AWSAuthenticationCredentials AuthenticationCredentials { get; set; }
|
public virtual AWSAuthenticationCredentials AuthenticationCredentials { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the RateLimitConfiguration for Amazon Selling Partner API Authentication
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The RateLimitConfiguration</value>
|
||||||
|
public virtual RateLimitConfiguration RateLimitConfig { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the API key with prefix.
|
/// Gets the API key with prefix.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -94,5 +94,11 @@ namespace {{packageName}}.Client
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>AuthenticationCredentials</value>
|
/// <value>AuthenticationCredentials</value>
|
||||||
AWSAuthenticationCredentials AuthenticationCredentials { get; }
|
AWSAuthenticationCredentials AuthenticationCredentials { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the RateLimitConfigurationOnRequests for Amazon Selling Partner API RateLimit
|
||||||
|
/// </summary>
|
||||||
|
/// <value>RateLimitConfiguration</value>
|
||||||
|
RateLimitConfiguration RateLimitConfig { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -436,6 +436,7 @@ namespace {{packageName}}.{{apiPackage}}
|
||||||
{
|
{
|
||||||
private LWAAuthorizationCredentials lwaAuthorizationCredentials;
|
private LWAAuthorizationCredentials lwaAuthorizationCredentials;
|
||||||
private AWSAuthenticationCredentials awsAuthenticationCredentials;
|
private AWSAuthenticationCredentials awsAuthenticationCredentials;
|
||||||
|
private RateLimitConfiguration rateLimitConfiguration;
|
||||||
|
|
||||||
public Builder SetLWAAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials)
|
public Builder SetLWAAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials)
|
||||||
{
|
{
|
||||||
|
@ -449,6 +450,13 @@ namespace {{packageName}}.{{apiPackage}}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Builder SetRateLimitConfiguration(RateLimitConfiguration rateLimitConfiguration)
|
||||||
|
{
|
||||||
|
this.rateLimitConfiguration = rateLimitConfiguration;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public {{classname}} Build()
|
public {{classname}} Build()
|
||||||
{
|
{
|
||||||
if (lwaAuthorizationCredentials == null)
|
if (lwaAuthorizationCredentials == null)
|
||||||
|
@ -464,7 +472,8 @@ namespace {{packageName}}.{{apiPackage}}
|
||||||
{{packageName}}.Client.Configuration configuration = new {{packageName}}.Client.Configuration()
|
{{packageName}}.Client.Configuration configuration = new {{packageName}}.Client.Configuration()
|
||||||
{
|
{
|
||||||
AuthorizationCredentials = lwaAuthorizationCredentials,
|
AuthorizationCredentials = lwaAuthorizationCredentials,
|
||||||
AuthenticationCredentials = awsAuthenticationCredentials
|
AuthenticationCredentials = awsAuthenticationCredentials,
|
||||||
|
RateLimitConfig = rateLimitConfiguration
|
||||||
};
|
};
|
||||||
|
|
||||||
// default HTTP connection timeout (in milliseconds)
|
// default HTTP connection timeout (in milliseconds)
|
||||||
|
|
Loading…
Reference in New Issue