//------------------------------------------------------------------------------ // // 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 LukeHagar.PlexAPI.SDK { using LukeHagar.PlexAPI.SDK.Hooks; using LukeHagar.PlexAPI.SDK.Models.Components; using LukeHagar.PlexAPI.SDK.Models.Errors; using LukeHagar.PlexAPI.SDK.Models.Requests; using LukeHagar.PlexAPI.SDK.Utils; using LukeHagar.PlexAPI.SDK.Utils.Retries; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public interface IUsers { /// /// Get list of all connected users /// /// /// Get list of all users that are friends and have library access with the provided Plex authentication token /// /// Task GetUsersAsync(GetUsersRequest? request = null, string? serverUrl = null); } public class Users: IUsers { /// /// List of server URLs available for the get-users operation. /// public static readonly string[] GetUsersServerList = { "https://plex.tv/api", }; public SDKConfig SDKConfiguration { get; private set; } private const string _language = Constants.Language; private const string _sdkVersion = Constants.SdkVersion; private const string _sdkGenVersion = Constants.SdkGenVersion; private const string _openapiDocVersion = Constants.OpenApiDocVersion; public Users(SDKConfig config) { SDKConfiguration = config; } public async Task GetUsersAsync(GetUsersRequest? request = null, string? serverUrl = null) { request.Accepts ??= SDKConfiguration.Accepts; request.ClientIdentifier ??= SDKConfiguration.ClientIdentifier; request.Product ??= SDKConfiguration.Product; request.Version ??= SDKConfiguration.Version; request.Platform ??= SDKConfiguration.Platform; request.PlatformVersion ??= SDKConfiguration.PlatformVersion; request.Device ??= SDKConfiguration.Device; request.Model ??= SDKConfiguration.Model; request.DeviceVendor ??= SDKConfiguration.DeviceVendor; request.DeviceName ??= SDKConfiguration.DeviceName; request.Marketplace ??= SDKConfiguration.Marketplace; string baseUrl = Utilities.TemplateUrl(GetUsersServerList[0], new Dictionary(){ }); if (serverUrl != null) { baseUrl = serverUrl; } var urlString = baseUrl + "/users"; var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlString); httpRequest.Headers.Add("user-agent", SDKConfiguration.UserAgent); HeaderSerializer.PopulateHeaders(ref httpRequest, request); if (SDKConfiguration.SecuritySource != null) { httpRequest = new SecurityMetadata(SDKConfiguration.SecuritySource).Apply(httpRequest); } var hookCtx = new HookContext(SDKConfiguration, baseUrl, "get-users", null, SDKConfiguration.SecuritySource); httpRequest = await this.SDKConfiguration.Hooks.BeforeRequestAsync(new BeforeRequestContext(hookCtx), httpRequest); HttpResponseMessage httpResponse; try { httpResponse = await SDKConfiguration.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 == 200) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); GetUsersResponseBody obj; try { obj = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Include); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetUsersResponseBody.", httpResponse, httpResponseBody, ex); } var response = new GetUsersResponse() { StatusCode = responseStatusCode, ContentType = contentType, RawResponse = httpResponse }; response.Object = obj; return response; } throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode == 400) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); GetUsersBadRequestPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Include); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetUsersBadRequestPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new GetUsersBadRequest(payload, httpResponse, httpResponseBody); } throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode == 401) { if(Utilities.IsContentTypeMatch("application/json", contentType)) { var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); GetUsersUnauthorizedPayload payload; try { payload = ResponseBodyDeserializer.DeserializeNotNull(httpResponseBody, NullValueHandling.Include); } catch (Exception ex) { throw new ResponseValidationException("Failed to deserialize response body into GetUsersUnauthorizedPayload.", httpResponse, httpResponseBody, ex); } payload.RawResponse = httpResponse; throw new GetUsersUnauthorized(payload, httpResponse, httpResponseBody); } throw new Models.Errors.SDKException("Unknown content type received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode >= 400 && responseStatusCode < 500) { throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } else if(responseStatusCode >= 500 && responseStatusCode < 600) { throw new Models.Errors.SDKException("API error occurred", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } throw new Models.Errors.SDKException("Unknown status code received", httpResponse, await httpResponse.Content.ReadAsStringAsync()); } } }