-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
147 lines (125 loc) · 4.11 KB
/
Program.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
var httpClient = new HttpClient();
var hostBuilder = Host.CreateApplicationBuilder(args);
hostBuilder.Configuration.AddUserSecrets<Program>();
using var host = hostBuilder.Build();
using var scope = host.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
User? user;
try
{
user = await AuthorizeAsync();
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to authorize.");
return -1;
}
if (user == null)
return -1;
if (args.Length == 0)
{
logger.LogError("No operations specified.");
return -1;
}
foreach (var arg in args)
{
try
{
switch (arg)
{
case "ucp":
await UpdateCollectionPricesAsync(user.CollectionId);
break;
}
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to execute operation '{arg}'.", arg);
}
}
return 0;
async Task<User?> AuthorizeAsync()
{
var refreshToken = hostBuilder.Configuration["RefreshToken"];
if (string.IsNullOrEmpty(refreshToken) || refreshToken == "SECRET")
{
logger.LogError("No refresh token secret provided.");
return null;
}
var googleApiKey = hostBuilder.Configuration["GoogleApiKey"];
if (string.IsNullOrEmpty(googleApiKey) || googleApiKey == "SECRET")
{
logger.LogError("No Google API key secret provided.");
return null;
}
using HttpResponseMessage tokenResponse = await httpClient.PostAsync(
$"https://securetoken.googleapis.com/v1/token?key={googleApiKey}",
new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "refresh_token",
["refresh_token"] = refreshToken
}));
tokenResponse.EnsureSuccessStatusCode();
var auth = await tokenResponse.Content.ReadFromJsonAsync<FirebaseAuthResponse>();
if (auth == null || auth.AccessToken == null || auth.UserId == null)
{
return null;
}
httpClient.DefaultRequestHeaders.Authorization = new(auth.AccessToken);
using HttpResponseMessage profileResponse = await httpClient.GetAsync(
$"https://api.invintorywines.com/v2/profiles/{auth.UserId}");
profileResponse.EnsureSuccessStatusCode();
var profile = await profileResponse.Content.ReadFromJsonAsync<ProfileResponse>();
if (profile == null || profile.CollectionId == null)
{
return null;
}
return new User
{
AccessToken = auth.AccessToken,
CollectionId = profile.CollectionId.Value
};
}
async Task UpdateCollectionPricesAsync(int collectionId)
{
using HttpResponseMessage collectionResponse = await httpClient.GetAsync(
$"https://api.invintorywines.com/v2/collections/{collectionId}?list_type=in_collection");
collectionResponse.EnsureSuccessStatusCode();
var collection = await collectionResponse.Content.ReadFromJsonAsync<CollectionResponse>();
if (collection == null || collection.Labels == null || !collection.Labels.Any())
{
logger.LogInformation("Nothing in collection to update.");
return;
}
foreach (var label in collection.Labels)
{
if (label.Bottles == null || !label.Bottles.Any())
{
continue;
}
foreach (var bottle in label.Bottles)
{
if (!bottle.PurchasePrice.HasValue && label.PriceAverageConverted.HasValue)
{
using HttpResponseMessage bottleUpdateResponse = await httpClient.PatchAsJsonAsync(
$"https://api.invintorywines.com/v2/collections/{collectionId}/bottles",
new
{
bottle_ids = new[] { bottle.Id },
purchase_price = label.PriceAverageConverted
});
bottleUpdateResponse.EnsureSuccessStatusCode();
}
}
}
}
class User
{
public string? AccessToken { get; set; }
public int CollectionId { get; set; }
}