Skip to content

Commit 17564b0

Browse files
Merge pull request #29 from TransactionProcessing/task/#25_clientforapi
First cut of Api Client
2 parents ae86697 + 4905fe2 commit 17564b0

13 files changed

+375
-24
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
5+
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeP2PAssets</TargetsForTfmSpecificBuildOutput>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="ClientProxyBase" Version="2023.8.1" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\EstateReportingAPI.DataTrasferObjects\EstateReportingAPI.DataTransferObjects.csproj" PrivateAssets="All" />
16+
</ItemGroup>
17+
18+
<Target Name="IncludeP2PAssets">
19+
<ItemGroup>
20+
<BuildOutputInPackage Include="$(OutputPath)EstateManagement.DataTransferObjects.dll" />
21+
</ItemGroup>
22+
</Target>
23+
24+
</Project>
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
namespace EstateReportingAPI.Client{
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using ClientProxyBase;
9+
using DataTransferObjects;
10+
using DataTrasferObjects;
11+
using Newtonsoft.Json;
12+
13+
public class EstateReportingApiClient : ClientProxyBase, IEstateReportingApiClient{
14+
#region Fields
15+
16+
private readonly Func<String, String> BaseAddressResolver;
17+
18+
#endregion
19+
20+
#region Constructors
21+
22+
public EstateReportingApiClient(Func<String, String> baseAddressResolver,
23+
HttpClient httpClient) : base(httpClient){
24+
this.BaseAddressResolver = baseAddressResolver;
25+
26+
// Add the API version header
27+
this.HttpClient.DefaultRequestHeaders.Add("api-version", "1.0");
28+
}
29+
30+
#endregion
31+
32+
#region Methods
33+
34+
public async Task<List<CalendarDate>> GetCalendarDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken){
35+
List<CalendarDate> response = null;
36+
37+
String requestUri = this.BuildRequestUrl($"/api/dimensions/calendar/{year}/dates");
38+
39+
try{
40+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
41+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
42+
request.Headers.Add("EstateId", estateId.ToString());
43+
44+
// Make the Http Call here
45+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
46+
47+
// Process the response
48+
String content = await this.HandleResponse(httpResponse, cancellationToken);
49+
50+
// call was successful so now deserialise the body to the response object
51+
response = JsonConvert.DeserializeObject<List<CalendarDate>>(content);
52+
}
53+
catch(Exception ex){
54+
// An exception has occurred, add some additional information to the message
55+
Exception exception = new Exception($"Error getting calendar dates for year {year} for estate {{estateId}}.", ex);
56+
57+
throw exception;
58+
}
59+
60+
return response;
61+
}
62+
63+
public async Task<List<CalendarYear>> GetCalendarYears(String accessToken, Guid estateId, CancellationToken cancellationToken){
64+
List<CalendarYear> response = null;
65+
66+
String requestUri = this.BuildRequestUrl("/api/dimensions/calendar/years");
67+
68+
try{
69+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
70+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
71+
request.Headers.Add("EstateId", estateId.ToString());
72+
73+
// Make the Http Call here
74+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
75+
76+
// Process the response
77+
String content = await this.HandleResponse(httpResponse, cancellationToken);
78+
79+
// call was successful so now deserialise the body to the response object
80+
response = JsonConvert.DeserializeObject<List<CalendarYear>>(content);
81+
}
82+
catch(Exception ex){
83+
// An exception has occurred, add some additional information to the message
84+
Exception exception = new Exception("Error getting calendar years for estate {estateId}.", ex);
85+
86+
throw exception;
87+
}
88+
89+
return response;
90+
}
91+
92+
public async Task<List<ComparisonDate>> GetComparisonDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken){
93+
List<ComparisonDate> response = null;
94+
95+
String requestUri = this.BuildRequestUrl("/api/dimensions/calendar/calendar/comparisondates");
96+
97+
try{
98+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
99+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
100+
request.Headers.Add("EstateId", estateId.ToString());
101+
102+
// Make the Http Call here
103+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
104+
105+
// Process the response
106+
String content = await this.HandleResponse(httpResponse, cancellationToken);
107+
108+
// call was successful so now deserialise the body to the response object
109+
response = JsonConvert.DeserializeObject<List<ComparisonDate>>(content);
110+
}
111+
catch(Exception ex){
112+
// An exception has occurred, add some additional information to the message
113+
Exception exception = new Exception("Error getting comparison dates for estate {estateId}.", ex);
114+
115+
throw exception;
116+
}
117+
118+
return response;
119+
}
120+
121+
public async Task<TodaysSales> GetTodaysSales(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){
122+
TodaysSales response = null;
123+
124+
String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales?comparisonDate={comparisonDate.Date:yyyy-MM-dd}");
125+
126+
try{
127+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
128+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
129+
request.Headers.Add("EstateId", estateId.ToString());
130+
131+
// Make the Http Call here
132+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
133+
134+
// Process the response
135+
String content = await this.HandleResponse(httpResponse, cancellationToken);
136+
137+
// call was successful so now deserialise the body to the response object
138+
response = JsonConvert.DeserializeObject<TodaysSales>(content);
139+
}
140+
catch(Exception ex){
141+
// An exception has occurred, add some additional information to the message
142+
Exception exception = new Exception("Error getting todays sales for estate {estateId}.", ex);
143+
144+
throw exception;
145+
}
146+
147+
return response;
148+
}
149+
150+
public async Task<List<TodaysSalesCountByHour>> GetTodaysSalesCountByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){
151+
List<TodaysSalesCountByHour> response = null;
152+
153+
String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales/countbyhour?comparisonDate={comparisonDate.Date:yyyy-MM-dd}");
154+
155+
try{
156+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
157+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
158+
request.Headers.Add("EstateId", estateId.ToString());
159+
160+
// Make the Http Call here
161+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
162+
163+
// Process the response
164+
String content = await this.HandleResponse(httpResponse, cancellationToken);
165+
166+
// call was successful so now deserialise the body to the response object
167+
response = JsonConvert.DeserializeObject<List<TodaysSalesCountByHour>>(content);
168+
}
169+
catch(Exception ex){
170+
// An exception has occurred, add some additional information to the message
171+
Exception exception = new Exception("Error getting todays sales count by hour for estate {estateId}.", ex);
172+
173+
throw exception;
174+
}
175+
176+
return response;
177+
}
178+
179+
public async Task<List<TodaysSalesValueByHour>> GetTodaysSalesValueByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){
180+
List<TodaysSalesValueByHour> response = null;
181+
182+
String requestUri = this.BuildRequestUrl($"/api/facts/transactions/todayssales/valuebyhour?comparisonDate={comparisonDate.Date:yyyy-MM-dd}");
183+
184+
try{
185+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
186+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
187+
request.Headers.Add("EstateId", estateId.ToString());
188+
189+
// Make the Http Call here
190+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
191+
192+
// Process the response
193+
String content = await this.HandleResponse(httpResponse, cancellationToken);
194+
195+
// call was successful so now deserialise the body to the response object
196+
response = JsonConvert.DeserializeObject<List<TodaysSalesValueByHour>>(content);
197+
}
198+
catch(Exception ex){
199+
// An exception has occurred, add some additional information to the message
200+
Exception exception = new Exception("Error getting todays sales value by hour for estate {estateId}.", ex);
201+
202+
throw exception;
203+
}
204+
205+
return response;
206+
}
207+
208+
public async Task<TodaysSettlement> GetTodaysSettlement(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken){
209+
TodaysSettlement response = null;
210+
211+
String requestUri = this.BuildRequestUrl($"/api/facts/settlements/todayssettlement?comparisonDate={comparisonDate.Date:yyyy-MM-dd}");
212+
213+
try{
214+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
215+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
216+
request.Headers.Add("EstateId", estateId.ToString());
217+
218+
// Make the Http Call here
219+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(request, cancellationToken);
220+
221+
// Process the response
222+
String content = await this.HandleResponse(httpResponse, cancellationToken);
223+
224+
// call was successful so now deserialise the body to the response object
225+
response = JsonConvert.DeserializeObject<TodaysSettlement>(content);
226+
}
227+
catch(Exception ex){
228+
// An exception has occurred, add some additional information to the message
229+
Exception exception = new Exception("Error getting todays settlement for estate {estateId}.", ex);
230+
231+
throw exception;
232+
}
233+
234+
return response;
235+
}
236+
237+
private String BuildRequestUrl(String route){
238+
String baseAddress = this.BaseAddressResolver("EstateReportingApi");
239+
240+
String requestUri = $"{baseAddress}{route}";
241+
242+
return requestUri;
243+
}
244+
245+
#endregion
246+
}
247+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace EstateReportingAPI.Client{
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using DataTransferObjects;
7+
using DataTrasferObjects;
8+
9+
public interface IEstateReportingApiClient{
10+
#region Methods
11+
12+
Task<List<CalendarDate>> GetCalendarDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken);
13+
Task<List<CalendarYear>> GetCalendarYears(String accessToken, Guid estateId, CancellationToken cancellationToken);
14+
Task<List<ComparisonDate>> GetComparisonDates(String accessToken, Guid estateId, Int32 year, CancellationToken cancellationToken);
15+
Task<TodaysSales> GetTodaysSales(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken);
16+
Task<List<TodaysSalesCountByHour>> GetTodaysSalesCountByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken);
17+
Task<List<TodaysSalesValueByHour>> GetTodaysSalesValueByHour(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken);
18+
Task<TodaysSettlement> GetTodaysSettlement(String accessToken, Guid estateId, DateTime comparisonDate, CancellationToken cancellationToken);
19+
20+
#endregion
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace EstateReportingAPI.DataTrasferObjects{
2+
using System;
3+
4+
public class CalendarDate
5+
{
6+
public DateTime Date { get; set; }
7+
public String DayOfWeek{ get; set; }
8+
public Int32 DayOfWeekNumber{ get; set; }
9+
public String DayOfWeekShort{ get; set; }
10+
public String MonthName{ get; set; }
11+
public String MonthNameShort { get; set; }
12+
public Int32 MonthNumber { get; set; }
13+
public Int32 WeekNumber { get; set; }
14+
public String WeekNumberString { get; set; }
15+
public Int32 Year { get; set; }
16+
public String YearWeekNumber { get; set; }
17+
}
18+
}

EstateReportingAPI.DataTrasferObjects/CalendarYear.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,4 @@ public class CalendarYear
88
{
99
public Int32 Year{ get; set; }
1010
}
11-
12-
public class ComparisonDate{
13-
public Int32 OrderValue{ get; set; }
14-
public DateTime Date { get; set; }
15-
public String Description { get; set; }
16-
17-
}
18-
19-
public class CalendarDate
20-
{
21-
public DateTime Date { get; set; }
22-
public String DayOfWeek{ get; set; }
23-
public Int32 DayOfWeekNumber{ get; set; }
24-
public String DayOfWeekShort{ get; set; }
25-
public String MonthName{ get; set; }
26-
public String MonthNameShort { get; set; }
27-
public Int32 MonthNumber { get; set; }
28-
public Int32 WeekNumber { get; set; }
29-
public String WeekNumberString { get; set; }
30-
public Int32 Year { get; set; }
31-
public String YearWeekNumber { get; set; }
32-
}
3311
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace EstateReportingAPI.DataTrasferObjects{
2+
using System;
3+
4+
public class ComparisonDate{
5+
public Int32 OrderValue{ get; set; }
6+
public DateTime Date { get; set; }
7+
public String Description { get; set; }
8+
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace EstateReportingAPI.DataTransferObjects
6+
{
7+
public class TodaysSales
8+
{
9+
public Decimal TodaysSalesValue { get; set; }
10+
public Int32 TodaysSalesCount { get; set; }
11+
public Decimal ComparisonSalesValue { get; set; }
12+
public Int32 ComparisonSalesCount { get; set; }
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace EstateReportingAPI.DataTransferObjects{
2+
using System;
3+
4+
public class TodaysSalesCountByHour
5+
{
6+
public Int32 Hour { get; set; }
7+
public Int32 TodaysSalesCount { get; set; }
8+
public Decimal ComparisonSalesValue { get; set; }
9+
public Int32 ComparisonSalesCount { get; set; }
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace EstateReportingAPI.DataTransferObjects{
2+
using System;
3+
4+
public class TodaysSalesValueByHour{
5+
public Int32 Hour{ get; set; }
6+
public Decimal TodaysSalesValue { get; set; }
7+
public Decimal ComparisonSalesValue { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)