//------------------------------------------------------------------------------ // // This code was generated by Speakeasy (https://speakeasy.com). 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.Hooks; using PlexAPI.Models.Components; using PlexAPI.Models.Errors; using PlexAPI.Models.Requests; using PlexAPI.Utils.Retries; using PlexAPI.Utils; using System.Collections.Generic; using System.Net.Http.Headers; using System.Net.Http; using System.Threading.Tasks; using System; /// /// API Calls that perform operations with Plex Media Server Users
/// /// /// /// ///
public interface IUser { /// /// Get User SignIn Data /// /// /// Sign in user with username and password and return user data with Plex authentication token /// /// Task PostSignInAsync(string? xPlexClientIdentifier = null, PostSignInRequestBody? requestBody = null, string? serverUrl = null); } /// /// API Calls that perform operations with Plex Media Server Users
/// /// /// /// ///
public class User: IUser { /// /// List of server URLs available for the post-sign-in operation. /// public static readonly string[] PostSignInServerList = { "https://plex.tv/api/v2", }; public SDKConfig SDKConfiguration { get; private set; } private const string _language = "csharp"; private const string _sdkVersion = "0.4.1"; private const string _sdkGenVersion = "2.404.3"; private const string _openapiDocVersion = "0.0.3"; private const string _userAgent = "speakeasy-sdk/csharp 0.4.1 2.404.3 0.0.3 PlexAPI"; private string _serverUrl = ""; private ISpeakeasyHttpClient _client; private Func? _securitySource; public User(ISpeakeasyHttpClient client, Func? securitySource, string serverUrl, SDKConfig config) { _client = client; _securitySource = securitySource; _serverUrl = serverUrl; SDKConfiguration = config; } public async Task PostSignInAsync(string? xPlexClientIdentifier = null, PostSignInRequestBody? requestBody = null, string? serverUrl = null) { var request = new PostSignInRequest() { XPlexClientIdentifier = xPlexClientIdentifier, RequestBody = requestBody, }; request.XPlexClientIdentifier ??= SDKConfiguration.XPlexClientIdentifier; string baseUrl = Utilities.TemplateUrl(PostSignInServerList[0], new Dictionary(){ }); if (serverUrl != null) { baseUrl = serverUrl; } var urlString = baseUrl + "/users/signin"; var httpRequest = new HttpRequestMessage(HttpMethod.Post, urlString); httpRequest.Headers.Add("user-agent", _userAgent); HeaderSerializer.PopulateHeaders(ref httpRequest, request); var serializedBody = RequestBodySerializer.Serialize(request, "RequestBody", "form", false, true); if (serializedBody != null) { httpRequest.Content = serializedBody; } var hookCtx = new HookContext("post-sign-in", null, null); httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest); HttpResponseMessage httpResponse; try { httpResponse = await _client.SendAsync(httpRequest); int _statusCode = (int)httpResponse.StatusCode; if (_statusCode == 400 || _statusCode == 401 || _statusCode >= 400 && _statusCode < 500 || _statusCode >= 500 && _statusCode < 600) { var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), httpResponse, null); if (_httpResponse != null) { httpResponse = _httpResponse; } } } catch (Exception error) { var _httpResponse = await this.SDKConfiguration.Hooks.AfterErrorAsync(new AfterErrorContext(hookCtx), null, error); if (_httpResponse != null) { httpResponse = _httpResponse; } else { throw; } } httpResponse = await this.SDKConfiguration.Hooks.AfterSuccessAsync(new AfterSuccessContext(hookCtx), httpResponse); var contentType = httpResponse.Content.Headers.ContentType?.MediaType; int responseStatusCode = (int)httpResponse.StatusCode; if(responseStatusCode == 201) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var obj = ResponseBodyDeserializer.Deserialize(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore); var response = new PostSignInResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; response.UserPlexAccount = obj; return response; } else { throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse); } } else if(responseStatusCode == 400 || responseStatusCode >= 400 && responseStatusCode < 500 || responseStatusCode >= 500 && responseStatusCode < 600) { throw new SDKException("API error occurred", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse); } else if(responseStatusCode == 401) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var obj = ResponseBodyDeserializer.Deserialize(await httpResponse.Content.ReadAsStringAsync(), NullValueHandling.Ignore); obj!.RawResponse = httpResponse; throw obj!; } else { throw new SDKException("Unknown content type received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse); } } else { throw new SDKException("Unknown status code received", responseStatusCode, await httpResponse.Content.ReadAsStringAsync(), httpResponse); } } } }