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+ }
0 commit comments