Files
plexruby/lib/plex_ruby_sdk/authentication.rb

558 lines
24 KiB
Ruby

# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
# typed: true
# frozen_string_literal: true
require 'faraday'
require 'faraday/multipart'
require 'faraday/retry'
require 'sorbet-runtime'
require_relative 'sdk_hooks/hooks'
require_relative 'utils/retries'
module PlexRubySDK
extend T::Sig
class Authentication
extend T::Sig
# GET_TOKEN_DETAILS_SERVERS contains the list of server urls available to the SDK.
GET_TOKEN_DETAILS_SERVERS = [
'https://plex.tv/api/v2',
].freeze
# POST_USERS_SIGN_IN_DATA_SERVERS contains the list of server urls available to the SDK.
POST_USERS_SIGN_IN_DATA_SERVERS = [
'https://plex.tv/api/v2',
].freeze
# API Calls regarding authentication for Plex Media Server
#
sig { params(sdk_config: SDKConfiguration).void }
def initialize(sdk_config)
@sdk_configuration = sdk_config
end
sig { params(type: Models::Operations::GetTransientTokenQueryParamType, scope: Models::Operations::Scope, timeout_ms: T.nilable(Integer)).returns(Models::Operations::GetTransientTokenResponse) }
def get_transient_token(type, scope, timeout_ms = nil)
# get_transient_token - Get a Transient Token
# This endpoint provides the caller with a temporary token with the same access level as the caller's token. These tokens are valid for up to 48 hours and are destroyed if the server instance is restarted.
#
request = Models::Operations::GetTransientTokenRequest.new(
type: type,
scope: scope
)
url, params = @sdk_configuration.get_server_details
base_url = Utils.template_url(url, params)
url = "#{base_url}/security/token"
headers = {}
query_params = Utils.get_query_params(Models::Operations::GetTransientTokenRequest, request)
headers['Accept'] = 'application/json'
headers['user-agent'] = @sdk_configuration.user_agent
security = !@sdk_configuration.nil? && !@sdk_configuration.security_source.nil? ? @sdk_configuration.security_source.call : nil
timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
timeout ||= @sdk_configuration.timeout
connection = @sdk_configuration.client
hook_ctx = SDKHooks::HookContext.new(
base_url: base_url,
oauth2_scopes: [],
operation_id: 'getTransientToken',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
http_response = T.let(nil, T.nilable(Faraday::Response))
begin
http_response = connection.get(url) do |req|
req.headers.merge!(headers)
req.options.timeout = timeout unless timeout.nil?
req.params = query_params
Utils.configure_request_security(req, security)
@sdk_configuration.hooks.before_request(
hook_ctx: SDKHooks::BeforeRequestHookContext.new(
hook_ctx: hook_ctx
),
request: req
)
end
rescue StandardError => e
error = e
ensure
if http_response.nil? || Utils.error_status?(http_response.status)
http_response = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
else
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
end
if http_response.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = http_response.headers.fetch('Content-Type', 'application/octet-stream')
if Utils.match_status_code(http_response.status, ['200'])
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
return Models::Operations::GetTransientTokenResponse.new(
status_code: http_response.status,
content_type: content_type,
raw_response: http_response
)
elsif Utils.match_status_code(http_response.status, ['400'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetTransientTokenBadRequest)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['401'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetTransientTokenUnauthorized)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['4XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
elsif Utils.match_status_code(http_response.status, ['5XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown status code received'
end
end
sig { params(source: ::String, timeout_ms: T.nilable(Integer)).returns(Models::Operations::GetSourceConnectionInformationResponse) }
def get_source_connection_information(source, timeout_ms = nil)
# get_source_connection_information - Get Source Connection Information
# If a caller requires connection details and a transient token for a source that is known to the server, for example a cloud media provider or shared PMS, then this endpoint can be called. This endpoint is only accessible with either an admin token or a valid transient token generated from an admin token.
# Note: requires Plex Media Server >= 1.15.4.
#
request = Models::Operations::GetSourceConnectionInformationRequest.new(
source: source
)
url, params = @sdk_configuration.get_server_details
base_url = Utils.template_url(url, params)
url = "#{base_url}/security/resources"
headers = {}
query_params = Utils.get_query_params(Models::Operations::GetSourceConnectionInformationRequest, request)
headers['Accept'] = 'application/json'
headers['user-agent'] = @sdk_configuration.user_agent
security = !@sdk_configuration.nil? && !@sdk_configuration.security_source.nil? ? @sdk_configuration.security_source.call : nil
timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
timeout ||= @sdk_configuration.timeout
connection = @sdk_configuration.client
hook_ctx = SDKHooks::HookContext.new(
base_url: base_url,
oauth2_scopes: [],
operation_id: 'getSourceConnectionInformation',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
http_response = T.let(nil, T.nilable(Faraday::Response))
begin
http_response = connection.get(url) do |req|
req.headers.merge!(headers)
req.options.timeout = timeout unless timeout.nil?
req.params = query_params
Utils.configure_request_security(req, security)
@sdk_configuration.hooks.before_request(
hook_ctx: SDKHooks::BeforeRequestHookContext.new(
hook_ctx: hook_ctx
),
request: req
)
end
rescue StandardError => e
error = e
ensure
if http_response.nil? || Utils.error_status?(http_response.status)
http_response = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
else
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
end
if http_response.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = http_response.headers.fetch('Content-Type', 'application/octet-stream')
if Utils.match_status_code(http_response.status, ['200'])
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
return Models::Operations::GetSourceConnectionInformationResponse.new(
status_code: http_response.status,
content_type: content_type,
raw_response: http_response
)
elsif Utils.match_status_code(http_response.status, ['400'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetSourceConnectionInformationBadRequest)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['401'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetSourceConnectionInformationUnauthorized)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['4XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
elsif Utils.match_status_code(http_response.status, ['5XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown status code received'
end
end
sig { params(server_url: T.nilable(String), timeout_ms: T.nilable(Integer)).returns(Models::Operations::GetTokenDetailsResponse) }
def get_token_details(server_url = nil, timeout_ms = nil)
# get_token_details - Get Token Details
# Get the User data from the provided X-Plex-Token
base_url = Utils.template_url(GET_TOKEN_DETAILS_SERVERS[0], {
})
base_url = server_url if !server_url.nil?
url = "#{base_url}/user"
headers = {}
headers['Accept'] = 'application/json'
headers['user-agent'] = @sdk_configuration.user_agent
security = !@sdk_configuration.nil? && !@sdk_configuration.security_source.nil? ? @sdk_configuration.security_source.call : nil
timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
timeout ||= @sdk_configuration.timeout
connection = @sdk_configuration.client
hook_ctx = SDKHooks::HookContext.new(
base_url: base_url,
oauth2_scopes: [],
operation_id: 'getTokenDetails',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
http_response = T.let(nil, T.nilable(Faraday::Response))
begin
http_response = connection.get(url) do |req|
req.headers.merge!(headers)
req.options.timeout = timeout unless timeout.nil?
Utils.configure_request_security(req, security)
@sdk_configuration.hooks.before_request(
hook_ctx: SDKHooks::BeforeRequestHookContext.new(
hook_ctx: hook_ctx
),
request: req
)
end
rescue StandardError => e
error = e
ensure
if http_response.nil? || Utils.error_status?(http_response.status)
http_response = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
else
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
end
if http_response.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = http_response.headers.fetch('Content-Type', 'application/octet-stream')
if Utils.match_status_code(http_response.status, ['200'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Operations::GetTokenDetailsUserPlexAccount)
response = Models::Operations::GetTokenDetailsResponse.new(
status_code: http_response.status,
content_type: content_type,
raw_response: http_response,
user_plex_account: obj
)
return response
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['400'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetTokenDetailsBadRequest)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['401'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::GetTokenDetailsUnauthorized)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['4XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
elsif Utils.match_status_code(http_response.status, ['5XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown status code received'
end
end
sig { params(request: T.nilable(Models::Operations::PostUsersSignInDataRequest), server_url: T.nilable(String), timeout_ms: T.nilable(Integer)).returns(Models::Operations::PostUsersSignInDataResponse) }
def post_users_sign_in_data(request, server_url = nil, timeout_ms = nil)
# post_users_sign_in_data - Get User Sign In Data
# Sign in user with username and password and return user data with Plex authentication token
base_url = Utils.template_url(POST_USERS_SIGN_IN_DATA_SERVERS[0], {
})
base_url = server_url if !server_url.nil?
url = "#{base_url}/users/signin"
headers = Utils.get_headers(request)
req_content_type, data, form = Utils.serialize_request_body(request, :request_body, :form)
headers['content-type'] = req_content_type
if form
body = Utils.encode_form(form)
elsif Utils.match_content_type(req_content_type, 'application/x-www-form-urlencoded')
body = URI.encode_www_form(data)
else
body = data
end
headers['Accept'] = 'application/json'
headers['user-agent'] = @sdk_configuration.user_agent
timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
timeout ||= @sdk_configuration.timeout
connection = @sdk_configuration.client
hook_ctx = SDKHooks::HookContext.new(
base_url: base_url,
oauth2_scopes: [],
operation_id: 'post-users-sign-in-data',
security_source: nil
)
error = T.let(nil, T.nilable(StandardError))
http_response = T.let(nil, T.nilable(Faraday::Response))
begin
http_response = connection.post(url) do |req|
req.body = body
req.headers.merge!(headers)
req.options.timeout = timeout unless timeout.nil?
@sdk_configuration.hooks.before_request(
hook_ctx: SDKHooks::BeforeRequestHookContext.new(
hook_ctx: hook_ctx
),
request: req
)
end
rescue StandardError => e
error = e
ensure
if http_response.nil? || Utils.error_status?(http_response.status)
http_response = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
else
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
end
if http_response.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = http_response.headers.fetch('Content-Type', 'application/octet-stream')
if Utils.match_status_code(http_response.status, ['201'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Operations::PostUsersSignInDataUserPlexAccount)
response = Models::Operations::PostUsersSignInDataResponse.new(
status_code: http_response.status,
content_type: content_type,
raw_response: http_response,
user_plex_account: obj
)
return response
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['400'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::PostUsersSignInDataBadRequest)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['401'])
if Utils.match_content_type(content_type, 'application/json')
http_response = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: http_response
)
obj = Crystalline.unmarshal_json(JSON.parse(http_response.env.response_body), Models::Errors::PostUsersSignInDataUnauthorized)
obj.raw_response = http_response
throw obj
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown content type received'
end
elsif Utils.match_status_code(http_response.status, ['4XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
elsif Utils.match_status_code(http_response.status, ['5XX'])
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'API error occurred'
else
raise ::PlexRubySDK::Models::Errors::APIError.new(status_code: http_response.status, body: http_response.env.response_body, raw_response: http_response), 'Unknown status code received'
end
end
end
end