|
| 1 | +using System.Net.Http; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using TrophyApi; |
| 6 | +using TrophyApi.Core; |
| 7 | + |
| 8 | +namespace TrophyApi.Admin.Streaks; |
| 9 | + |
| 10 | +public partial class FreezesClient |
| 11 | +{ |
| 12 | + private RawClient _client; |
| 13 | + |
| 14 | + internal FreezesClient(RawClient client) |
| 15 | + { |
| 16 | + _client = client; |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Create streak freezes for multiple users. |
| 21 | + /// </summary> |
| 22 | + /// <example> |
| 23 | + /// <code> |
| 24 | + /// await client.Admin.Streaks.Freezes.CreateAsync( |
| 25 | + /// new CreateStreakFreezesRequest |
| 26 | + /// { |
| 27 | + /// Freezes = new List<CreateStreakFreezesRequestFreezesItem>() |
| 28 | + /// { |
| 29 | + /// new CreateStreakFreezesRequestFreezesItem { UserId = "user-123" }, |
| 30 | + /// new CreateStreakFreezesRequestFreezesItem { UserId = "user-456" }, |
| 31 | + /// new CreateStreakFreezesRequestFreezesItem { UserId = "user-123" }, |
| 32 | + /// }, |
| 33 | + /// } |
| 34 | + /// ); |
| 35 | + /// </code> |
| 36 | + /// </example> |
| 37 | + public async Task<CreateStreakFreezesResponse> CreateAsync( |
| 38 | + CreateStreakFreezesRequest request, |
| 39 | + RequestOptions? options = null, |
| 40 | + CancellationToken cancellationToken = default |
| 41 | + ) |
| 42 | + { |
| 43 | + var response = await _client |
| 44 | + .MakeRequestAsync( |
| 45 | + new RawClient.JsonApiRequest |
| 46 | + { |
| 47 | + BaseUrl = _client.Options.Environment.Admin, |
| 48 | + Method = HttpMethod.Post, |
| 49 | + Path = "streaks/freezes", |
| 50 | + Body = request, |
| 51 | + ContentType = "application/json", |
| 52 | + Options = options, |
| 53 | + }, |
| 54 | + cancellationToken |
| 55 | + ) |
| 56 | + .ConfigureAwait(false); |
| 57 | + var responseBody = await response.Raw.Content.ReadAsStringAsync(); |
| 58 | + if (response.StatusCode is >= 200 and < 400) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + return JsonUtils.Deserialize<CreateStreakFreezesResponse>(responseBody)!; |
| 63 | + } |
| 64 | + catch (JsonException e) |
| 65 | + { |
| 66 | + throw new TrophyApiException("Failed to deserialize response", e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + switch (response.StatusCode) |
| 73 | + { |
| 74 | + case 400: |
| 75 | + throw new BadRequestError(JsonUtils.Deserialize<ErrorBody>(responseBody)); |
| 76 | + case 401: |
| 77 | + throw new UnauthorizedError(JsonUtils.Deserialize<ErrorBody>(responseBody)); |
| 78 | + case 422: |
| 79 | + throw new UnprocessableEntityError( |
| 80 | + JsonUtils.Deserialize<ErrorBody>(responseBody) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + catch (JsonException) |
| 85 | + { |
| 86 | + // unable to map error response, throwing generic error |
| 87 | + } |
| 88 | + throw new TrophyApiApiException( |
| 89 | + $"Error with status code {response.StatusCode}", |
| 90 | + response.StatusCode, |
| 91 | + responseBody |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments