49 lines
2.4 KiB
C#
49 lines
2.4 KiB
C#
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;
|
|
|
|
public class YSetStationFeedbackBuilder : YMusicRequestBuilder<string?, (YStationFeedbackType type, YStation station, YTrack? track, string batchId, double totalPlayedSeconds)>
|
|
{
|
|
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<string, string> 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);
|
|
}
|
|
} |