/// <summary>
        /// Make a call to an API with the given details, and apply the specified self-throttling method
        /// </summary>
        /// <typeparam name="T">Type to desearalize the data returned into</typeparam>
        /// <param name="timeout">Optional time to give up after</param>
        /// <param name="webApiHelper">WebApiSerializerHelper of a given type</param>
        /// <param name="apiQueryUrl">URL of the API</param>
        /// <param name="urlParameters">Optional paramaters for the API call</param>
        /// <param name="apiKey">Optional APIKey for the API</param>
        /// <param name="apiName">Optional Name of the API for logging</param>
        /// <returns>Results of the API call, desearilized into the given object type</returns>
        public T ExecuteThrottledApiCallWithBearerAuthToken <T>(
            TimeSpan?timeout,
            WebApiSerializerHelper <T> webApiHelper,
            string apiQueryUrl, string urlParameters, string bearerAuthToken = null, string apiName = null)
        {
            ExecuteSelfThrottlingPolicy(bearerAuthToken, apiName);

            var response = webApiHelper.GetHttpResponseContentAsType <T>(apiQueryUrl, urlParameters, bearerAuthToken, timeout).Result;

            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Make a call to an API with the given details, and apply the specified self-throttling method
        /// </summary>
        /// <typeparam name="T">Type to desearalize the data returned into</typeparam>
        /// <param name="timeout">Optional time to give up after</param>
        /// <param name="webApiHelper">WebApiSerializerHelper of a given type</param>
        /// <param name="apiQueryUrl">URL of the API</param>
        /// <param name="urlParameters">Optional paramaters for the API call</param>
        /// <param name="apiKey">Optional APIKey for the API</param>
        /// <param name="apiName">Optional Name of the API for logging</param>
        /// <returns>Results of the API call, desearilized into the given object type</returns>
        public T ExecuteThrottledApiCall <T>(
            TimeSpan?timeout,
            WebApiSerializerHelper <T> webApiHelper,
            string apiQueryUrl, string urlParameters, string apiKey = null, string apiName = null)
        {
            switch (_selfThrottlingMethod)
            {
            case SelfThrottlingMethod.InMemoryCallRecollection:
                // Check if we have hit our limit for number of calls to the API
                while (!this.VerifyInMemoryThrottledCallCanProceed())
                {
                    Logger.Information($"Delaying issueing call to {apiName} API to ensure API throttling isn't exceeded at {DateTime.UtcNow}", "ApiInteractionHelper: ExecuteThrottledApiCall()");
                    Thread.Sleep(500);
                }

                // Add this call to the call tracker
                this.recentApiCalls.Add(DateTime.UtcNow);
                break;

            case SelfThrottlingMethod.AzureTableStorageCallRecollection:
                // Check if we have hit our limit for number of calls to the API
                while (!this.VerifyAzureTableStorageThrottledCallCanProceed(apiKey, apiName))
                {
                    Logger.Information($"Delaying issueing call to {apiName} API to ensure API throttling isn't exceeded at {DateTime.UtcNow}", "ApiInteractionHelper: ExecuteThrottledApiCall()");
                    Thread.Sleep(500);
                }

                // Add this call to the call tracker
                AzureTableStorageHelper.LogApiCallToTableStorage(new ApiCallRecordTableEntity(apiKey, apiName));
                break;

            case SelfThrottlingMethod.None:
            default:
                // Apply no throttling - proceed with call
                break;
            }

            var response = webApiHelper.GetHttpResponseContentAsType <T>(apiQueryUrl, urlParameters, timeout, apiKey).Result;

            return(response);
        }