diff --git a/EstateReportingAPI.Client/EstateReportingApiClient.cs b/EstateReportingAPI.Client/EstateReportingApiClient.cs index b6ae1c9..38a6872 100644 --- a/EstateReportingAPI.Client/EstateReportingApiClient.cs +++ b/EstateReportingAPI.Client/EstateReportingApiClient.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Shared.Results; +using Shared.Results; using SimpleResults; namespace EstateReportingAPI.Client{ @@ -37,13 +34,13 @@ public EstateReportingApiClient(Func baseAddressResolver, #endregion #region Methods - + private const String EstateIdHeaderName = "EstateId"; public async Task>> GetCalendarDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken){ String requestUri = this.BuildRequestUrl($"/api/dimensions/calendar/{year}/dates"); try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result> result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -67,7 +64,7 @@ public async Task>> GetCalendarYears(String accessToke try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result> result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -89,7 +86,7 @@ public async Task>> GetComparisonDates(String access try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result> result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -112,7 +109,7 @@ public async Task> GetLastSettlement(String accessToken, try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -135,7 +132,7 @@ public async Task>> GetResponseCodes(String accessToke try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result> result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -163,7 +160,7 @@ public async Task> GetMerchantPerformance(String accessToken try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -190,7 +187,7 @@ public async Task> GetProductPerformance(String accessToken, try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -215,7 +212,7 @@ public async Task>> GetMerchantsByLastSaleDate(String acce try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -242,7 +239,7 @@ public async Task> GetOperatorPerformance(String accessToken try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result? result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -283,7 +280,7 @@ public async Task>> TransactionSearch(String acce try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpPostRequest>(requestUri, searchRequest, accessToken, additionalHeaders, cancellationToken); @@ -330,7 +327,7 @@ public async Task>> GetUnsettledFees(String accessToke try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -354,7 +351,7 @@ public async Task> GetMerchantKpi(String accessToken, Guid e try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result? result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -377,7 +374,7 @@ public async Task>> GetMerchants(String accessToken, Guid try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -400,7 +397,7 @@ public async Task>> GetOperators(String accessToken, Guid try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -428,7 +425,7 @@ public async Task> GetTodaysFailedSales(String accessToken, try { List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result? result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -455,7 +452,7 @@ public async Task> GetTodaysSales(String accessToken, Guid e try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result? result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -482,7 +479,7 @@ public async Task>> GetTodaysSalesCountByHou try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -509,7 +506,7 @@ public async Task>> GetTodaysSalesValueByHou try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -536,7 +533,7 @@ public async Task> GetTodaysSettlement(String accessTok try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result? result = await this.SendHttpGetRequest(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -558,7 +555,7 @@ public async Task>> GetTopBottomMerchantData( try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -580,7 +577,7 @@ public async Task>> GetTopBottomOperatorData( try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -602,7 +599,7 @@ public async Task>> GetTopBottomProductData(St try{ List<(String headerName, String headerValue)> additionalHeaders = [ - ("EstateId", estateId.ToString()) + (EstateIdHeaderName, estateId.ToString()) ]; Result>? result = await this.SendHttpGetRequest>(requestUri, accessToken, additionalHeaders, cancellationToken); @@ -629,77 +626,4 @@ private String BuildRequestUrl(String route){ #endregion } -} - -public class QueryStringBuilder -{ - private Dictionary parameters = new Dictionary(); - - public QueryStringBuilder AddParameter(string key, object value, Boolean alwaysInclude=false) - { - this.parameters.Add(key, (value, alwaysInclude)); - return this; - } - - static Dictionary FilterDictionary(Dictionary inputDictionary) - { - Dictionary result = new Dictionary(); - - foreach (KeyValuePair entry in inputDictionary) - { - if (entry.Value.value != null && !IsDefaultValue(entry.Value.value, entry.Value.alwaysInclude)) - { - result.Add(entry.Key, entry.Value.value); - } - } - - return result; - } - - static bool IsDefaultValue(T value, Boolean alwaysInclude){ - if (alwaysInclude) - return false; - - Object? defaultValue = GetDefault(value.GetType()); - - if (defaultValue == null && value.GetType() == typeof(String)) - { - defaultValue = String.Empty; - } - return defaultValue.Equals(value); - } - - public static object GetDefault(Type t) - { - Func f = GetDefault; - return f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null); - } - - private static T GetDefault() - { - return default(T); - } - - public string BuildQueryString(){ - Dictionary filtered = FilterDictionary(this.parameters); - - if (filtered.Count == 0) - { - return string.Empty; - } - - StringBuilder queryString = new StringBuilder(); - - foreach (KeyValuePair kvp in filtered) - { - if (queryString.Length > 0) - { - queryString.Append("&"); - } - - queryString.Append($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value.ToString())}"); - } - - return queryString.ToString(); - } -} +} \ No newline at end of file diff --git a/EstateReportingAPI.Client/QueryStringBuilder.cs b/EstateReportingAPI.Client/QueryStringBuilder.cs new file mode 100644 index 0000000..83220e9 --- /dev/null +++ b/EstateReportingAPI.Client/QueryStringBuilder.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Text; + +public class QueryStringBuilder +{ + private Dictionary parameters = new Dictionary(); + + public QueryStringBuilder AddParameter(string key, object value, Boolean alwaysInclude=false) + { + this.parameters.Add(key, (value, alwaysInclude)); + return this; + } + + static Dictionary FilterDictionary(Dictionary inputDictionary) + { + Dictionary result = new Dictionary(); + + foreach (KeyValuePair entry in inputDictionary) + { + if (entry.Value.value != null && !IsDefaultValue(entry.Value.value, entry.Value.alwaysInclude)) + { + result.Add(entry.Key, entry.Value.value); + } + } + + return result; + } + + static bool IsDefaultValue(T value, Boolean alwaysInclude){ + if (alwaysInclude) + return false; + + Object? defaultValue = GetDefault(value.GetType()); + + if (defaultValue == null && value.GetType() == typeof(String)) + { + defaultValue = String.Empty; + } + return defaultValue.Equals(value); + } + + public static object GetDefault(Type t) + { + Func f = GetDefault; + return f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null); + } + + private static T GetDefault() + { + return default(T); + } + + public string BuildQueryString(){ + Dictionary filtered = FilterDictionary(this.parameters); + + if (filtered.Count == 0) + { + return string.Empty; + } + + StringBuilder queryString = new StringBuilder(); + + foreach (KeyValuePair kvp in filtered) + { + if (queryString.Length > 0) + { + queryString.Append("&"); + } + + queryString.Append($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value.ToString())}"); + } + + return queryString.ToString(); + } +} \ No newline at end of file