using System.Collections.Specialized; using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; using YandexMusic.API.Models.Radio; using YandexMusic.API.Models.Track; namespace YandexMusic.API.Requests.Radio; internal class YSetStationFeedbackBuilder : YMusicRequestBuilder { public YSetStationFeedbackBuilder(YandexMusicApi api) : base(api) { } protected override string Method => WebRequestMethods.Http.Post; protected override string PathTemplate => "rotor/station/{type}:{tag}/feedback"; protected override Dictionary GetSubstitutions((YStationFeedbackType type, YStation station, YTrack? track, string batchId, double totalPlayedSeconds) tuple) => new() { { "type", tuple.station.Station.Id.Type }, { "tag", tuple.station.Station.Id.Tag } }; protected override NameValueCollection GetQueryParams((YStationFeedbackType type, YStation station, YTrack? track, string batchId, double totalPlayedSeconds) tuple) { var query = new NameValueCollection(); if (!string.IsNullOrWhiteSpace(tuple.batchId)) query.Add("batch-id", tuple.batchId); return query; } protected override HttpContent? GetContent((YStationFeedbackType type, YStation station, YTrack? track, string batchId, double totalPlayedSeconds) tuple) { var feedback = new YStationFeedback { Type = tuple.type, From = tuple.station.Station.IdForFrom, Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds() }; if (tuple.track != null) feedback.TrackId = tuple.track.Id; if (tuple.totalPlayedSeconds > 0) feedback.TotalPlayedSeconds = tuple.totalPlayedSeconds; var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; return JsonContent.Create(feedback, new MediaTypeHeaderValue("application/json"), options); } }