|
1 | | -using Microsoft.Extensions.Configuration; |
| 1 | +using LoadTester; |
| 2 | +using Microsoft.Extensions.Configuration; |
2 | 3 | using Microsoft.Extensions.DependencyInjection; |
3 | | -using static TrainingApi.Controllers.TrainingsController; |
4 | 4 |
|
5 | 5 | var configuration = new ConfigurationBuilder() |
6 | 6 | .AddJsonFile("appsettings.json") |
|
9 | 9 |
|
10 | 10 | var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); |
11 | 11 |
|
12 | | -var usernames = await GetUsernames(configuration["AdminApi:Group"]!); |
| 12 | +var admin = new AdminClient(configuration.GetSection("AdminApi").Get<AdminClient.Config>()!); |
| 13 | +var usernames = await admin.GetUsernames(configuration["AdminApi:Group"]!); |
13 | 14 | //usernames = usernames.Take(1); |
14 | 15 |
|
15 | 16 | var clientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); |
@@ -41,52 +42,3 @@ int GetTargetDay(int index, int maxIndex) |
41 | 42 | var frac = 1f * index / maxIndex; |
42 | 43 | return (int)(frac * 30) + 1; |
43 | 44 | } |
44 | | - |
45 | | -async Task<IEnumerable<string>> GetUsernames(string group) |
46 | | -{ |
47 | | - using var client = new HttpClient(); |
48 | | - |
49 | | - var summaries = await GetGroupTrainings(group); |
50 | | - |
51 | | - foreach (var trainingId in summaries.Select(o => o.Id)) |
52 | | - { |
53 | | - var req = new HttpRequestMessage(HttpMethod.Delete, GetUri($"trainings?id={trainingId}")); |
54 | | - AddAuth(req); |
55 | | - var response = await client.SendAsync(req); |
56 | | - response.EnsureSuccessStatusCode(); |
57 | | - } |
58 | | - return summaries.Select(o => o.Username); |
59 | | - |
60 | | - async Task<List<TrainingSummaryWithDaysDto>> GetGroupTrainings(string group) |
61 | | - { |
62 | | - var req = new HttpRequestMessage(HttpMethod.Get, GetUri($"Trainings/summaries?group={System.Net.WebUtility.UrlEncode(group)}")); |
63 | | - AddAuth(req); |
64 | | - var response = await client.SendAsync(req); |
65 | | - response.EnsureSuccessStatusCode(); |
66 | | - var asString = await response.Content.ReadAsStringAsync(); |
67 | | - List<TrainingSummaryWithDaysDto>? result; |
68 | | - try |
69 | | - { |
70 | | - result = System.Text.Json.JsonSerializer.Deserialize<List<TrainingSummaryWithDaysDto>>(asString, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true }); // await response.Content.ReadFromJsonAsync<List<TrainingSummary>>(); |
71 | | - } |
72 | | - catch (Exception ex) |
73 | | - { |
74 | | - throw ex; |
75 | | - } |
76 | | - if (result == null) |
77 | | - throw new NullReferenceException(); |
78 | | - |
79 | | - return result; |
80 | | - } |
81 | | - |
82 | | - Uri GetUri(string path) |
83 | | - { |
84 | | - return new Uri(new Uri(configuration["AdminApi:BaseUrl"]!), path); |
85 | | - } |
86 | | - void AddAuth(HttpRequestMessage m) |
87 | | - { |
88 | | - var cookie = configuration["AdminApi:AuthCookie"]; |
89 | | - if (!string.IsNullOrEmpty(cookie)) |
90 | | - m.Headers.Add("Cookie", $".AspNetCore.Cookies={cookie}"); |
91 | | - } |
92 | | -} |
0 commit comments