Skip to content
Merged
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
126 changes: 25 additions & 101 deletions EstateReportingAPI.Client/EstateReportingApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Shared.Results;
using Shared.Results;
using SimpleResults;

namespace EstateReportingAPI.Client{
Expand Down Expand Up @@ -37,13 +34,13 @@ public EstateReportingApiClient(Func<String, String> baseAddressResolver,
#endregion

#region Methods

private const String EstateIdHeaderName = "EstateId";
public async Task<Result<List<CalendarDate>>> 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<List<CalendarDate>> result = await this.SendHttpGetRequest<List<CalendarDate>>(requestUri, accessToken, additionalHeaders, cancellationToken);
Expand All @@ -67,7 +64,7 @@ public async Task<Result<List<CalendarYear>>> GetCalendarYears(String accessToke

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<CalendarYear>> result = await this.SendHttpGetRequest<List<CalendarYear>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -89,7 +86,7 @@ public async Task<Result<List<ComparisonDate>>> GetComparisonDates(String access

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];

Result<List<ComparisonDate>> result = await this.SendHttpGetRequest<List<ComparisonDate>>(requestUri, accessToken, additionalHeaders, cancellationToken);
Expand All @@ -112,7 +109,7 @@ public async Task<Result<LastSettlement>> GetLastSettlement(String accessToken,

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<LastSettlement> result = await this.SendHttpGetRequest<LastSettlement>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -135,7 +132,7 @@ public async Task<Result<List<ResponseCode>>> GetResponseCodes(String accessToke
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<ResponseCode>> result = await this.SendHttpGetRequest<List<ResponseCode>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand Down Expand Up @@ -163,7 +160,7 @@ public async Task<Result<TodaysSales>> GetMerchantPerformance(String accessToken
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSales> result = await this.SendHttpGetRequest<TodaysSales>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -190,7 +187,7 @@ public async Task<Result<TodaysSales>> GetProductPerformance(String accessToken,
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSales> result = await this.SendHttpGetRequest<TodaysSales>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -215,7 +212,7 @@ public async Task<Result<List<Merchant>>> GetMerchantsByLastSaleDate(String acce
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<Merchant>>? result = await this.SendHttpGetRequest<List<Merchant>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -242,7 +239,7 @@ public async Task<Result<TodaysSales>> GetOperatorPerformance(String accessToken
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSales>? result = await this.SendHttpGetRequest<TodaysSales>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand Down Expand Up @@ -283,7 +280,7 @@ public async Task<Result<List<TransactionResult>>> TransactionSearch(String acce
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];

Result<List<TransactionResult>>? result = await this.SendHttpPostRequest<TransactionSearchRequest, List<TransactionResult>>(requestUri, searchRequest, accessToken, additionalHeaders, cancellationToken);
Expand Down Expand Up @@ -330,7 +327,7 @@ public async Task<Result<List<UnsettledFee>>> GetUnsettledFees(String accessToke
try
{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<UnsettledFee>>? result = await this.SendHttpGetRequest<List<UnsettledFee>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -354,7 +351,7 @@ public async Task<Result<MerchantKpi>> GetMerchantKpi(String accessToken, Guid e

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<MerchantKpi>? result = await this.SendHttpGetRequest<MerchantKpi>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -377,7 +374,7 @@ public async Task<Result<List<Merchant>>> GetMerchants(String accessToken, Guid

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<Merchant>>? result = await this.SendHttpGetRequest<List<Merchant>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -400,7 +397,7 @@ public async Task<Result<List<Operator>>> GetOperators(String accessToken, Guid

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<Operator>>? result = await this.SendHttpGetRequest<List<Operator>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand Down Expand Up @@ -428,7 +425,7 @@ public async Task<Result<TodaysSales>> GetTodaysFailedSales(String accessToken,

try {
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSales>? result = await this.SendHttpGetRequest<TodaysSales>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -455,7 +452,7 @@ public async Task<Result<TodaysSales>> GetTodaysSales(String accessToken, Guid e

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSales>? result = await this.SendHttpGetRequest<TodaysSales>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -482,7 +479,7 @@ public async Task<Result<List<TodaysSalesCountByHour>>> GetTodaysSalesCountByHou

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<TodaysSalesCountByHour>>? result = await this.SendHttpGetRequest<List<TodaysSalesCountByHour>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -509,7 +506,7 @@ public async Task<Result<List<TodaysSalesValueByHour>>> GetTodaysSalesValueByHou

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<TodaysSalesValueByHour>>? result = await this.SendHttpGetRequest<List<TodaysSalesValueByHour>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -536,7 +533,7 @@ public async Task<Result<TodaysSettlement>> GetTodaysSettlement(String accessTok

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<TodaysSettlement>? result = await this.SendHttpGetRequest<TodaysSettlement>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -558,7 +555,7 @@ public async Task<Result<List<TopBottomMerchantData>>> GetTopBottomMerchantData(

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<TopBottomMerchantData>>? result = await this.SendHttpGetRequest<List<TopBottomMerchantData>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -580,7 +577,7 @@ public async Task<Result<List<TopBottomOperatorData>>> GetTopBottomOperatorData(

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<TopBottomOperatorData>>? result = await this.SendHttpGetRequest<List<TopBottomOperatorData>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -602,7 +599,7 @@ public async Task<Result<List<TopBottomProductData>>> GetTopBottomProductData(St

try{
List<(String headerName, String headerValue)> additionalHeaders = [
("EstateId", estateId.ToString())
(EstateIdHeaderName, estateId.ToString())
];
Result<List<TopBottomProductData>>? result = await this.SendHttpGetRequest<List<TopBottomProductData>>(requestUri, accessToken, additionalHeaders, cancellationToken);

Expand All @@ -629,77 +626,4 @@ private String BuildRequestUrl(String route){

#endregion
}
}

public class QueryStringBuilder
{
private Dictionary<string, (object value, Boolean alwaysInclude)> parameters = new Dictionary<String, (Object value, Boolean alwaysInclude)>();

public QueryStringBuilder AddParameter(string key, object value, Boolean alwaysInclude=false)
{
this.parameters.Add(key, (value, alwaysInclude));
return this;
}

static Dictionary<string, object> FilterDictionary(Dictionary<string, (object value, Boolean alwaysInclude)> inputDictionary)
{
Dictionary<string, object> result = new Dictionary<string, object>();

foreach (KeyValuePair<String, (object value, Boolean alwaysInclude)> 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>(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<object> f = GetDefault<object>;
return f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null);
}

private static T GetDefault<T>()
{
return default(T);
}

public string BuildQueryString(){
Dictionary<String, Object> filtered = FilterDictionary(this.parameters);

if (filtered.Count == 0)
{
return string.Empty;
}

StringBuilder queryString = new StringBuilder();

foreach (KeyValuePair<String, Object> kvp in filtered)
{
if (queryString.Length > 0)
{
queryString.Append("&");
}

queryString.Append($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value.ToString())}");
}

return queryString.ToString();
}
}
}
76 changes: 76 additions & 0 deletions EstateReportingAPI.Client/QueryStringBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text;

public class QueryStringBuilder
{
private Dictionary<string, (object value, Boolean alwaysInclude)> parameters = new Dictionary<String, (Object value, Boolean alwaysInclude)>();

public QueryStringBuilder AddParameter(string key, object value, Boolean alwaysInclude=false)

Check warning on line 9 in EstateReportingAPI.Client/QueryStringBuilder.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

EstateReportingAPI.Client/QueryStringBuilder.cs#L9

Use the overloading mechanism instead of the optional parameters.
{
this.parameters.Add(key, (value, alwaysInclude));
return this;
}

static Dictionary<string, object> FilterDictionary(Dictionary<string, (object value, Boolean alwaysInclude)> inputDictionary)
{
Dictionary<string, object> result = new Dictionary<string, object>();

foreach (KeyValuePair<String, (object value, Boolean alwaysInclude)> 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>(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<object> f = GetDefault<object>;
return f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null);
}

private static T GetDefault<T>()
{
return default(T);
}

public string BuildQueryString(){
Dictionary<String, Object> filtered = FilterDictionary(this.parameters);

if (filtered.Count == 0)
{
return string.Empty;
}

StringBuilder queryString = new StringBuilder();

foreach (KeyValuePair<String, Object> kvp in filtered)
{
if (queryString.Length > 0)
{
queryString.Append("&");
}

queryString.Append($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value.ToString())}");
}

return queryString.ToString();
}
}
Loading