From e34b3f35d67187bc72c330f9c7c6b69af4a35dcb Mon Sep 17 00:00:00 2001 From: Maxim Yakovlev Date: Mon, 15 Jun 2020 11:46:37 +0300 Subject: [PATCH 1/2] expose API calls limit info --- src/SalesforceSharp/SalesforceClient.cs | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/SalesforceSharp/SalesforceClient.cs b/src/SalesforceSharp/SalesforceClient.cs index f5f93da..8438326 100644 --- a/src/SalesforceSharp/SalesforceClient.cs +++ b/src/SalesforceSharp/SalesforceClient.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Text.RegularExpressions; using HelperSharp; using Newtonsoft.Json.Linq; using RestSharp; @@ -23,6 +24,7 @@ public class SalesforceClient private IRestClient m_restClient; private GenericJsonDeserializer genericJsonDeserializer; private GenericJsonSerializer updateJsonSerializer; + private static readonly Regex apiUsageRegexp = new Regex(@"api-usage=(\d+)/(\d+)", RegexOptions.Compiled); #endregion #region Constructors @@ -75,6 +77,22 @@ protected internal SalesforceClient(IRestClient restClient) /// The instance URL. /// public string InstanceUrl { get; private set; } + + /// + /// Get current API calls number + /// + /// + /// current API calls number + /// + public int ApiCallsUsed { get; private set; } + + /// + /// Get total API calls limit + /// + /// + /// API calls limit + /// + public int ApiCallsLimit { get; private set; } #endregion #region Methods @@ -461,6 +479,7 @@ protected IRestResponse RequestRaw(string baseUrl, string objectName = null, obj var response = m_restClient.Execute(request); CheckApiException(response); + ExtractLimitsInfo(response); return response; } @@ -522,6 +541,20 @@ private void CheckApiException(IRestResponse response) throw ex; } } + + private void ExtractLimitsInfo(IRestResponse response) + { + var limitHeader = response.Headers?.FirstOrDefault(h => h.Name == "Sforce-Limit-Info"); + if (limitHeader?.Value != null) + { + var match = apiUsageRegexp.Match(limitHeader.Value.ToString()); + if (match.Success) + { + ApiCallsUsed = int.Parse(match.Groups[1].Value); + ApiCallsLimit = int.Parse(match.Groups[2].Value); + } + } + } #endregion #region Helpers From 729edd178c95a03d2dad01a32745cab564277778 Mon Sep 17 00:00:00 2001 From: giacomelli Date: Thu, 24 Sep 2020 07:06:53 -0300 Subject: [PATCH 2/2] Added assert for ApiCallsUsed and ApiCallsLimit --- src/SalesforceSharp.FunctionalTests/SalesforceClientTest.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SalesforceSharp.FunctionalTests/SalesforceClientTest.cs b/src/SalesforceSharp.FunctionalTests/SalesforceClientTest.cs index e18971b..b9f28fa 100644 --- a/src/SalesforceSharp.FunctionalTests/SalesforceClientTest.cs +++ b/src/SalesforceSharp.FunctionalTests/SalesforceClientTest.cs @@ -250,6 +250,8 @@ public void GetRawBytes_ValidRecord() Assert.IsNotNull(actual); Assert.That(actual.Contains(string.Format("\"FirstName\":\"{0}\"", record.FirstName))); Assert.That(actual.Contains(string.Format("\"LastName\":\"{0}\"", record.LastName))); + Assert.AreNotEqual(target.ApiCallsUsed, 0); + Assert.AreEqual(target.ApiCallsLimit, 15000); } #endregion