//------------------------------------------------------------------------------
//
// This code was generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
//
// Changes to this file may cause incorrect behavior and will be lost when
// the code is regenerated.
//
//------------------------------------------------------------------------------
#nullable enable
namespace PlexAPI
{
using Newtonsoft.Json;
using PlexAPI.Models.Components;
using PlexAPI.Models.Requests;
using PlexAPI.Utils;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using System;
///
/// API Calls interacting with Plex Media Server Media
///
///
///
///
///
public interface IMedia
{
///
/// Mark Media Played
///
///
/// This will mark the provided media key as Played.
///
///
Task MarkPlayedAsync(double key);
///
/// Mark Media Unplayed
///
///
/// This will mark the provided media key as Unplayed.
///
///
Task MarkUnplayedAsync(double key);
///
/// Update Media Play Progress
///
///
/// This API command can be used to update the play progress of a media item.
///
///
///
Task UpdatePlayProgressAsync(string key, double time, string state);
}
///
/// API Calls interacting with Plex Media Server Media
///
///
///
///
///
public class Media: IMedia
{
public SDKConfig SDKConfiguration { get; private set; }
private const string _language = "csharp";
private const string _sdkVersion = "0.2.0";
private const string _sdkGenVersion = "2.279.1";
private const string _openapiDocVersion = "0.0.3";
private const string _userAgent = "speakeasy-sdk/csharp 0.2.0 2.279.1 0.0.3 Plex-API";
private string _serverUrl = "";
private ISpeakeasyHttpClient _defaultClient;
private Func? _securitySource;
public Media(ISpeakeasyHttpClient defaultClient, Func? securitySource, string serverUrl, SDKConfig config)
{
_defaultClient = defaultClient;
_securitySource = securitySource;
_serverUrl = serverUrl;
SDKConfiguration = config;
}
public async Task MarkPlayedAsync(double key)
{
var request = new MarkPlayedRequest()
{
Key = key,
};
string baseUrl = this.SDKConfiguration.GetTemplatedServerDetails();
var urlString = URLBuilder.Build(baseUrl, "/:/scrobble", request);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
httpRequest.Headers.Add("user-agent", _userAgent);
var client = _defaultClient;
if (_securitySource != null)
{
client = SecuritySerializer.Apply(_defaultClient, _securitySource);
}
var httpResponse = await client.SendAsync(httpRequest);
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
var response = new MarkPlayedResponse
{
StatusCode = (int)httpResponse.StatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
if((response.StatusCode == 200) || (response.StatusCode == 400))
{
return response;
}
if((response.StatusCode == 401))
{
if(Utilities.IsContentTypeMatch("application/json",response.ContentType))
{
response.Object = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, Converters = new JsonConverter[] { new FlexibleObjectDeserializer(), new EnumSerializer() }});
}
return response;
}
return response;
}
public async Task MarkUnplayedAsync(double key)
{
var request = new MarkUnplayedRequest()
{
Key = key,
};
string baseUrl = this.SDKConfiguration.GetTemplatedServerDetails();
var urlString = URLBuilder.Build(baseUrl, "/:/unscrobble", request);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString);
httpRequest.Headers.Add("user-agent", _userAgent);
var client = _defaultClient;
if (_securitySource != null)
{
client = SecuritySerializer.Apply(_defaultClient, _securitySource);
}
var httpResponse = await client.SendAsync(httpRequest);
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
var response = new MarkUnplayedResponse
{
StatusCode = (int)httpResponse.StatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
if((response.StatusCode == 200) || (response.StatusCode == 400))
{
return response;
}
if((response.StatusCode == 401))
{
if(Utilities.IsContentTypeMatch("application/json",response.ContentType))
{
response.Object = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, Converters = new JsonConverter[] { new FlexibleObjectDeserializer(), new EnumSerializer() }});
}
return response;
}
return response;
}
public async Task UpdatePlayProgressAsync(string key, double time, string state)
{
var request = new UpdatePlayProgressRequest()
{
Key = key,
Time = time,
State = state,
};
string baseUrl = this.SDKConfiguration.GetTemplatedServerDetails();
var urlString = URLBuilder.Build(baseUrl, "/:/progress", request);
var httpRequest = new HttpRequestMessage(HttpMethod.Post, urlString);
httpRequest.Headers.Add("user-agent", _userAgent);
var client = _defaultClient;
if (_securitySource != null)
{
client = SecuritySerializer.Apply(_defaultClient, _securitySource);
}
var httpResponse = await client.SendAsync(httpRequest);
var contentType = httpResponse.Content.Headers.ContentType?.MediaType;
var response = new UpdatePlayProgressResponse
{
StatusCode = (int)httpResponse.StatusCode,
ContentType = contentType,
RawResponse = httpResponse
};
if((response.StatusCode == 200) || (response.StatusCode == 400))
{
return response;
}
if((response.StatusCode == 401))
{
if(Utilities.IsContentTypeMatch("application/json",response.ContentType))
{
response.Object = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, Converters = new JsonConverter[] { new FlexibleObjectDeserializer(), new EnumSerializer() }});
}
return response;
}
return response;
}
}
}