Files
plexruby/lib/plex_ruby_sdk/search.rb

335 lines
13 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 Search
extend T::Sig
# API Calls that perform search operations with Plex Media Server
#
sig { params(sdk_config: SDKConfiguration).void }
def initialize(sdk_config)
@sdk_configuration = sdk_config
end
sig { params(query: ::String, section_id: T.nilable(::Float), limit: T.nilable(::Float), timeout_ms: T.nilable(Integer)).returns(::PlexRubySDK::Operations::PerformSearchResponse) }
def perform_search(query, section_id = nil, limit = nil, timeout_ms = nil)
# perform_search - Perform a search
# This endpoint performs a search across all library sections, or a single section, and returns matches as hubs, split up by type. It performs spell checking, looks for partial matches, and orders the hubs based on quality of results. In addition, based on matches, it will return other related matches (e.g. for a genre match, it may return movies in that genre, or for an actor match, movies with that actor).
#
# In the response's items, the following extra attributes are returned to further describe or disambiguate the result:
#
# - `reason`: The reason for the result, if not because of a direct search term match; can be either:
# - `section`: There are multiple identical results from different sections.
# - `originalTitle`: There was a search term match from the original title field (sometimes those can be very different or in a foreign language).
# - `<hub identifier>`: If the reason for the result is due to a result in another hub, the source hub identifier is returned. For example, if the search is for "dylan" then Bob Dylan may be returned as an artist result, an a few of his albums returned as album results with a reason code of `artist` (the identifier of that particular hub). Or if the search is for "arnold", there might be movie results returned with a reason of `actor`
# - `reasonTitle`: The string associated with the reason code. For a section reason, it'll be the section name; For a hub identifier, it'll be a string associated with the match (e.g. `Arnold Schwarzenegger` for movies which were returned because the search was for "arnold").
# - `reasonID`: The ID of the item associated with the reason for the result. This might be a section ID, a tag ID, an artist ID, or a show ID.
#
# This request is intended to be very fast, and called as the user types.
#
request = ::PlexRubySDK::Operations::PerformSearchRequest.new(
query: query,
section_id: section_id,
limit: limit
)
url, params = @sdk_configuration.get_server_details
base_url = Utils.template_url(url, params)
url = "#{base_url}/hubs/search"
headers = {}
query_params = Utils.get_query_params(::PlexRubySDK::Operations::PerformSearchRequest, 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: nil,
operation_id: 'performSearch',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
r = T.let(nil, T.nilable(Faraday::Response))
begin
r = 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 r.nil? || Utils.error_status?(r.status)
r = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
else
r = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
end
if r.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
res = ::PlexRubySDK::Operations::PerformSearchResponse.new(
status_code: r.status, content_type: content_type, raw_response: r
)
if r.status == 200
elsif r.status == 400
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::PerformSearchBadRequest)
res.bad_request = out
end
elsif r.status == 401
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::PerformSearchUnauthorized)
res.unauthorized = out
end
end
res
end
sig { params(query: ::String, section_id: T.nilable(::Float), limit: T.nilable(::Float), timeout_ms: T.nilable(Integer)).returns(::PlexRubySDK::Operations::PerformVoiceSearchResponse) }
def perform_voice_search(query, section_id = nil, limit = nil, timeout_ms = nil)
# perform_voice_search - Perform a voice search
# This endpoint performs a search specifically tailored towards voice or other imprecise input which may work badly with the substring and spell-checking heuristics used by the `/hubs/search` endpoint.
# It uses a [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) heuristic to search titles, and as such is much slower than the other search endpoint.
# Whenever possible, clients should limit the search to the appropriate type.
# Results, as well as their containing per-type hubs, contain a `distance` attribute which can be used to judge result quality.
#
request = ::PlexRubySDK::Operations::PerformVoiceSearchRequest.new(
query: query,
section_id: section_id,
limit: limit
)
url, params = @sdk_configuration.get_server_details
base_url = Utils.template_url(url, params)
url = "#{base_url}/hubs/search/voice"
headers = {}
query_params = Utils.get_query_params(::PlexRubySDK::Operations::PerformVoiceSearchRequest, 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: nil,
operation_id: 'performVoiceSearch',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
r = T.let(nil, T.nilable(Faraday::Response))
begin
r = 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 r.nil? || Utils.error_status?(r.status)
r = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
else
r = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
end
if r.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
res = ::PlexRubySDK::Operations::PerformVoiceSearchResponse.new(
status_code: r.status, content_type: content_type, raw_response: r
)
if r.status == 200
elsif r.status == 400
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::PerformVoiceSearchBadRequest)
res.bad_request = out
end
elsif r.status == 401
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::PerformVoiceSearchUnauthorized)
res.unauthorized = out
end
end
res
end
sig { params(query: ::String, timeout_ms: T.nilable(Integer)).returns(::PlexRubySDK::Operations::GetSearchResultsResponse) }
def get_search_results(query, timeout_ms = nil)
# get_search_results - Get Search Results
# This will search the database for the string provided.
request = ::PlexRubySDK::Operations::GetSearchResultsRequest.new(
query: query
)
url, params = @sdk_configuration.get_server_details
base_url = Utils.template_url(url, params)
url = "#{base_url}/search"
headers = {}
query_params = Utils.get_query_params(::PlexRubySDK::Operations::GetSearchResultsRequest, 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: nil,
operation_id: 'getSearchResults',
security_source: @sdk_configuration.security_source
)
error = T.let(nil, T.nilable(StandardError))
r = T.let(nil, T.nilable(Faraday::Response))
begin
r = 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 r.nil? || Utils.error_status?(r.status)
r = @sdk_configuration.hooks.after_error(
error: error,
hook_ctx: SDKHooks::AfterErrorHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
else
r = @sdk_configuration.hooks.after_success(
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
hook_ctx: hook_ctx
),
response: r
)
end
if r.nil?
raise error if !error.nil?
raise 'no response'
end
end
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
res = ::PlexRubySDK::Operations::GetSearchResultsResponse.new(
status_code: r.status, content_type: content_type, raw_response: r
)
if r.status == 200
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::GetSearchResultsResponseBody)
res.object = out
end
elsif r.status == 400
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::GetSearchResultsBadRequest)
res.bad_request = out
end
elsif r.status == 401
if Utils.match_content_type(content_type, 'application/json')
out = Crystalline.unmarshal_json(JSON.parse(r.env.response_body), ::PlexRubySDK::Operations::GetSearchResultsUnauthorized)
res.unauthorized = out
end
end
res
end
end
end