From 4905fe2a7161b9aa1e326e291b6860d77f44b848 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 7 Sep 2023 11:43:11 +0100 Subject: [PATCH] First cut of Api Client --- .../EstateReportingAPI.Client.csproj | 24 ++ .../EstateReportingApiClient.cs | 247 ++++++++++++++++++ .../IEstateReportingApiClient.cs | 22 ++ .../CalendarDate.cs | 18 ++ .../CalendarYear.cs | 22 -- .../ComparisonDate.cs | 10 + ...teReportingAPI.DataTransferObjects.csproj} | 0 .../TodaysSales.cs | 14 + .../TodaysSalesCountByHour.cs | 11 + .../TodaysSalesValueByHour.cs | 9 + .../TodaysSettlement.cs | 11 + EstateReportingAPI.sln | 9 +- EstateReportingAPI/EstateReportingAPI.csproj | 2 +- 13 files changed, 375 insertions(+), 24 deletions(-) create mode 100644 EstateReportingAPI.Client/EstateReportingAPI.Client.csproj create mode 100644 EstateReportingAPI.Client/EstateReportingApiClient.cs create mode 100644 EstateReportingAPI.Client/IEstateReportingApiClient.cs create mode 100644 EstateReportingAPI.DataTrasferObjects/CalendarDate.cs create mode 100644 EstateReportingAPI.DataTrasferObjects/ComparisonDate.cs rename EstateReportingAPI.DataTrasferObjects/{EstateReportingAPI.DataTrasferObjects.csproj => EstateReportingAPI.DataTransferObjects.csproj} (100%) create mode 100644 EstateReportingAPI.DataTrasferObjects/TodaysSales.cs create mode 100644 EstateReportingAPI.DataTrasferObjects/TodaysSalesCountByHour.cs create mode 100644 EstateReportingAPI.DataTrasferObjects/TodaysSalesValueByHour.cs create mode 100644 EstateReportingAPI.DataTrasferObjects/TodaysSettlement.cs diff --git a/EstateReportingAPI.Client/EstateReportingAPI.Client.csproj b/EstateReportingAPI.Client/EstateReportingAPI.Client.csproj new file mode 100644 index 0000000..4bb845a --- /dev/null +++ b/EstateReportingAPI.Client/EstateReportingAPI.Client.csproj @@ -0,0 +1,24 @@ + + + + net7.0;netstandard2.1 + $(TargetsForTfmSpecificBuildOutput);IncludeP2PAssets + enable + + + + + + + + + + + + + + + + + + diff --git a/EstateReportingAPI.Client/EstateReportingApiClient.cs b/EstateReportingAPI.Client/EstateReportingApiClient.cs new file mode 100644 index 0000000..9d2b3a6 --- /dev/null +++ b/EstateReportingAPI.Client/EstateReportingApiClient.cs @@ -0,0 +1,247 @@ +namespace EstateReportingAPI.Client{ + using System; + using System.Collections.Generic; + using System.Net.Http; + using System.Net.Http.Headers; + using System.Threading; + using System.Threading.Tasks; + using ClientProxyBase; + using DataTransferObjects; + using DataTrasferObjects; + using Newtonsoft.Json; + + public class EstateReportingApiClient : ClientProxyBase, IEstateReportingApiClient{ + #region Fields + + private readonly Func BaseAddressResolver; + + #endregion + + #region Constructors + + public EstateReportingApiClient(Func baseAddressResolver, + HttpClient httpClient) : base(httpClient){ + this.BaseAddressResolver = baseAddressResolver; + + // Add the API version header + this.HttpClient.DefaultRequestHeaders.Add("api-version", "1.0"); + } + + #endregion + + #region Methods + + public async Task> GetCalendarDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken){ + List response = null; + + String requestUri = this.BuildRequestUrl($"/api/dimensions/calendar/{year}/dates"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject>(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception($"Error getting calendar dates for year {year} for estate {{estateId}}.", ex); + + throw exception; + } + + return response; + } + + public async Task> GetCalendarYears(String accessToken, Guid estateId, CancellationToken cancellationToken){ + List response = null; + + String requestUri = this.BuildRequestUrl("/api/dimensions/calendar/years"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject>(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting calendar years for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + public async Task> GetComparisonDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken){ + List response = null; + + String requestUri = this.BuildRequestUrl("/api/dimensions/calendar/calendar/comparisondates"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject>(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting comparison dates for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + public async Task GetTodaysSales(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){ + TodaysSales response = null; + + String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales?comparisonDate={comparisonDate.Date:yyyy-MM-dd}"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting todays sales for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + public async Task> GetTodaysSalesCountByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){ + List response = null; + + String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales/countbyhour?comparisonDate={comparisonDate.Date:yyyy-MM-dd}"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject>(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting todays sales count by hour for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + public async Task> GetTodaysSalesValueByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){ + List response = null; + + String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales/valuebyhour?comparisonDate={comparisonDate.Date:yyyy-MM-dd}"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject>(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting todays sales value by hour for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + public async Task GetTodaysSettlement(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){ + TodaysSettlement response = null; + + String requestUri = this.BuildRequestUrl($"/api/facts/settlements/todayssettlement?comparisonDate={comparisonDate.Date:yyyy-MM-dd}"); + + try{ + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + request.Headers.Add("EstateId", estateId.ToString()); + + // Make the Http Call here + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken); + + // Process the response + String content = await this.HandleResponse(httpResponse, cancellationToken); + + // call was successful so now deserialise the body to the response object + response = JsonConvert.DeserializeObject(content); + } + catch(Exception ex){ + // An exception has occurred, add some additional information to the message + Exception exception = new Exception("Error getting todays settlement for estate {estateId}.", ex); + + throw exception; + } + + return response; + } + + private String BuildRequestUrl(String route){ + String baseAddress = this.BaseAddressResolver("EstateReportingApi"); + + String requestUri = $"{baseAddress}{route}"; + + return requestUri; + } + + #endregion + } +} \ No newline at end of file diff --git a/EstateReportingAPI.Client/IEstateReportingApiClient.cs b/EstateReportingAPI.Client/IEstateReportingApiClient.cs new file mode 100644 index 0000000..0ad55e8 --- /dev/null +++ b/EstateReportingAPI.Client/IEstateReportingApiClient.cs @@ -0,0 +1,22 @@ +namespace EstateReportingAPI.Client{ + using System; + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + using DataTransferObjects; + using DataTrasferObjects; + + public interface IEstateReportingApiClient{ + #region Methods + + Task> GetCalendarDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken); + Task> GetCalendarYears(String accessToken, Guid estateId, CancellationToken cancellationToken); + Task> GetComparisonDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken); + Task GetTodaysSales(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken); + Task> GetTodaysSalesCountByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken); + Task> GetTodaysSalesValueByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken); + Task GetTodaysSettlement(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken); + + #endregion + } +} \ No newline at end of file diff --git a/EstateReportingAPI.DataTrasferObjects/CalendarDate.cs b/EstateReportingAPI.DataTrasferObjects/CalendarDate.cs new file mode 100644 index 0000000..f012652 --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/CalendarDate.cs @@ -0,0 +1,18 @@ +namespace EstateReportingAPI.DataTrasferObjects{ + using System; + + public class CalendarDate + { + public DateTime Date { get; set; } + public String DayOfWeek{ get; set; } + public Int32 DayOfWeekNumber{ get; set; } + public String DayOfWeekShort{ get; set; } + public String MonthName{ get; set; } + public String MonthNameShort { get; set; } + public Int32 MonthNumber { get; set; } + public Int32 WeekNumber { get; set; } + public String WeekNumberString { get; set; } + public Int32 Year { get; set; } + public String YearWeekNumber { get; set; } + } +} \ No newline at end of file diff --git a/EstateReportingAPI.DataTrasferObjects/CalendarYear.cs b/EstateReportingAPI.DataTrasferObjects/CalendarYear.cs index b5bb6c5..76ad317 100644 --- a/EstateReportingAPI.DataTrasferObjects/CalendarYear.cs +++ b/EstateReportingAPI.DataTrasferObjects/CalendarYear.cs @@ -8,26 +8,4 @@ public class CalendarYear { public Int32 Year{ get; set; } } - - public class ComparisonDate{ - public Int32 OrderValue{ get; set; } - public DateTime Date { get; set; } - public String Description { get; set; } - - } - - public class CalendarDate - { - public DateTime Date { get; set; } - public String DayOfWeek{ get; set; } - public Int32 DayOfWeekNumber{ get; set; } - public String DayOfWeekShort{ get; set; } - public String MonthName{ get; set; } - public String MonthNameShort { get; set; } - public Int32 MonthNumber { get; set; } - public Int32 WeekNumber { get; set; } - public String WeekNumberString { get; set; } - public Int32 Year { get; set; } - public String YearWeekNumber { get; set; } - } } diff --git a/EstateReportingAPI.DataTrasferObjects/ComparisonDate.cs b/EstateReportingAPI.DataTrasferObjects/ComparisonDate.cs new file mode 100644 index 0000000..21f7cdb --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/ComparisonDate.cs @@ -0,0 +1,10 @@ +namespace EstateReportingAPI.DataTrasferObjects{ + using System; + + public class ComparisonDate{ + public Int32 OrderValue{ get; set; } + public DateTime Date { get; set; } + public String Description { get; set; } + + } +} \ No newline at end of file diff --git a/EstateReportingAPI.DataTrasferObjects/EstateReportingAPI.DataTrasferObjects.csproj b/EstateReportingAPI.DataTrasferObjects/EstateReportingAPI.DataTransferObjects.csproj similarity index 100% rename from EstateReportingAPI.DataTrasferObjects/EstateReportingAPI.DataTrasferObjects.csproj rename to EstateReportingAPI.DataTrasferObjects/EstateReportingAPI.DataTransferObjects.csproj diff --git a/EstateReportingAPI.DataTrasferObjects/TodaysSales.cs b/EstateReportingAPI.DataTrasferObjects/TodaysSales.cs new file mode 100644 index 0000000..3af1117 --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/TodaysSales.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EstateReportingAPI.DataTransferObjects +{ + public class TodaysSales + { + public Decimal TodaysSalesValue { get; set; } + public Int32 TodaysSalesCount { get; set; } + public Decimal ComparisonSalesValue { get; set; } + public Int32 ComparisonSalesCount { get; set; } + } +} diff --git a/EstateReportingAPI.DataTrasferObjects/TodaysSalesCountByHour.cs b/EstateReportingAPI.DataTrasferObjects/TodaysSalesCountByHour.cs new file mode 100644 index 0000000..bc3fc3b --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/TodaysSalesCountByHour.cs @@ -0,0 +1,11 @@ +namespace EstateReportingAPI.DataTransferObjects{ + using System; + + public class TodaysSalesCountByHour + { + public Int32 Hour { get; set; } + public Int32 TodaysSalesCount { get; set; } + public Decimal ComparisonSalesValue { get; set; } + public Int32 ComparisonSalesCount { get; set; } + } +} \ No newline at end of file diff --git a/EstateReportingAPI.DataTrasferObjects/TodaysSalesValueByHour.cs b/EstateReportingAPI.DataTrasferObjects/TodaysSalesValueByHour.cs new file mode 100644 index 0000000..8515e5c --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/TodaysSalesValueByHour.cs @@ -0,0 +1,9 @@ +namespace EstateReportingAPI.DataTransferObjects{ + using System; + + public class TodaysSalesValueByHour{ + public Int32 Hour{ get; set; } + public Decimal TodaysSalesValue { get; set; } + public Decimal ComparisonSalesValue { get; set; } + } +} \ No newline at end of file diff --git a/EstateReportingAPI.DataTrasferObjects/TodaysSettlement.cs b/EstateReportingAPI.DataTrasferObjects/TodaysSettlement.cs new file mode 100644 index 0000000..66f0b52 --- /dev/null +++ b/EstateReportingAPI.DataTrasferObjects/TodaysSettlement.cs @@ -0,0 +1,11 @@ +namespace EstateReportingAPI.DataTransferObjects{ + using System; + + public class TodaysSettlement + { + public Decimal TodaysSettlementValue { get; set; } + public Int32 TodaysSettlementCount { get; set; } + public Decimal ComparisonSettlementValue { get; set; } + public Int32 ComparisonSettlementCount { get; set; } + } +} \ No newline at end of file diff --git a/EstateReportingAPI.sln b/EstateReportingAPI.sln index 5f726bd..a5d343e 100644 --- a/EstateReportingAPI.sln +++ b/EstateReportingAPI.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EstateReportingAPI", "Estat EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EstateReportingAPI.Tests", "EstateReportingAPI.Tests\EstateReportingAPI.Tests.csproj", "{1124CAC0-A2B3-44FF-8B16-C8722D7042A2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EstateReportingAPI.DataTrasferObjects", "EstateReportingAPI.DataTrasferObjects\EstateReportingAPI.DataTrasferObjects.csproj", "{4BEC708B-2A02-486F-8BEF-AD9D6220DD74}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EstateReportingAPI.DataTransferObjects", "EstateReportingAPI.DataTrasferObjects\EstateReportingAPI.DataTransferObjects.csproj", "{4BEC708B-2A02-486F-8BEF-AD9D6220DD74}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EstateReportingAPI.Client", "EstateReportingAPI.Client\EstateReportingAPI.Client.csproj", "{8C38147F-7556-44BE-BF60-5DF40BC57EC8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -37,6 +39,10 @@ Global {4BEC708B-2A02-486F-8BEF-AD9D6220DD74}.Debug|Any CPU.Build.0 = Debug|Any CPU {4BEC708B-2A02-486F-8BEF-AD9D6220DD74}.Release|Any CPU.ActiveCfg = Release|Any CPU {4BEC708B-2A02-486F-8BEF-AD9D6220DD74}.Release|Any CPU.Build.0 = Release|Any CPU + {8C38147F-7556-44BE-BF60-5DF40BC57EC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C38147F-7556-44BE-BF60-5DF40BC57EC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C38147F-7556-44BE-BF60-5DF40BC57EC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C38147F-7556-44BE-BF60-5DF40BC57EC8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -46,6 +52,7 @@ Global {DC3AEC92-739F-45E6-8513-CFAEA826984A} = {C48C544B-7C3B-4AA2-8A37-E49296A86499} {1124CAC0-A2B3-44FF-8B16-C8722D7042A2} = {C48C544B-7C3B-4AA2-8A37-E49296A86499} {4BEC708B-2A02-486F-8BEF-AD9D6220DD74} = {713556C8-BFC0-4AB6-8F13-7631C1C82D07} + {8C38147F-7556-44BE-BF60-5DF40BC57EC8} = {713556C8-BFC0-4AB6-8F13-7631C1C82D07} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B2BE269B-F142-45A3-9B75-483227C765A6} diff --git a/EstateReportingAPI/EstateReportingAPI.csproj b/EstateReportingAPI/EstateReportingAPI.csproj index fec31be..5e95280 100644 --- a/EstateReportingAPI/EstateReportingAPI.csproj +++ b/EstateReportingAPI/EstateReportingAPI.csproj @@ -37,7 +37,7 @@ - +