-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTooGoodToGoClient.cs
121 lines (105 loc) · 4.12 KB
/
TooGoodToGoClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TGTG.Api.Core.Internal.Common;
using TGTG.Api.Core.Internal.Requests;
using TGTG.Api.Core.Internal.Responses;
using TGTG.Api.Core.Utils;
namespace TGTG.Api.Core
{
public class TooGoodToGoClient : IDisposable
{
private readonly InternalHttpClient _httpClient;
private readonly TokenProvider _tokenProvider;
public TimeSpan Timeout
{
get => _httpClient.Timeout;
set => _httpClient.Timeout = value;
}
public TooGoodToGoClient(string email, string password)
{
_httpClient = new InternalHttpClient();
_tokenProvider = new TokenProvider(_httpClient, email, password);
}
public async Task SetFavourite(string itemId)
{
await SetFavourite(itemId, true);
}
public async Task UnsetFavourite(string itemId)
{
await SetFavourite(itemId, false);
}
public async Task<List<Location>> FindLocationsAsync(string location)
{
var response = await PostAsync<LocationResponse, LocationRequest>(Urls.Locations, new LocationRequest(location));
return response.Locations
.Select(ConvertToLocation)
.ToList();
}
public async Task<List<Store>> FindStoresAsync(Coordinates geoLocation, double radius)
{
var token = await _tokenProvider.GetToken();
var storeRequest = new StoreRequest
{
UserId = token.UserId,
LocationCoordinates = new LocationCoordinates(geoLocation.Latitude, geoLocation.Longitude),
Radius = radius
};
var response = await PostAsync<StoreResponse, StoreRequest>(Urls.Stores, storeRequest);
return response.Containers
.Select(ConvertToStore)
.ToList();
}
private async Task SetFavourite(string itemId, bool isFavourite)
{
await PostAsync(Urls.SetFavourite(itemId), isFavourite);
}
private static Location ConvertToLocation(LocationResult location)
{
return new Location
{
Name = location.Name,
Coordinates = new Coordinates(location.Coordinates.Latitude, location.Coordinates.Longitude)
};
}
private static Store ConvertToStore(StoreContainer container)
{
var storePickupLocation = container.PickupLocation;
return new Store
{
Id = container.Store.Id,
ItemId = container.Item.Id,
Name = container.Store.Name,
Description = container.Item.Description,
Website = container.Store.Website,
Distance = container.Distance,
Favourited = container.Favorited,
NumberOfItemsAvailable = container.ItemsAvailable,
InSalesWindow = container.InSalesWindow,
Address = new Address
{
AddressLine = storePickupLocation.Address.AddressLine,
City = storePickupLocation.Address.City,
Postcode = storePickupLocation.Address.Postcode,
GeoLocation = new Coordinates(storePickupLocation.LocationCoordinates.Latitude, storePickupLocation.LocationCoordinates.Longitude)
}
};
}
private async Task<TResponse> PostAsync<TResponse, TRequest>(string url, TRequest request)
{
var token = await _tokenProvider.GetToken();
return await _httpClient.PostAsync<TResponse, TRequest>(url, request, token.AccessToken);
}
private async Task PostAsync<TRequest>(string url, TRequest request)
{
var token = await _tokenProvider.GetToken();
await _httpClient.PostAsync(url, request, token.AccessToken);
}
public void Dispose()
{
_httpClient?.Dispose();
_tokenProvider?.Dispose();
}
}
}