"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.""" import requests as requests_http from .sdkconfiguration import SDKConfiguration from plex_api import utils from plex_api._hooks import HookContext from plex_api.models import errors, operations from typing import Optional class Search: r"""API Calls that perform search operations with Plex Media Server""" sdk_configuration: SDKConfiguration def __init__(self, sdk_config: SDKConfiguration) -> None: self.sdk_configuration = sdk_config def perform_search(self, query: str, section_id: Optional[float] = None, limit: Optional[float] = None) -> operations.PerformSearchResponse: r"""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). - ``: 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. """ hook_ctx = HookContext(operation_id='performSearch', oauth2_scopes=[], security_source=self.sdk_configuration.security) request = operations.PerformSearchRequest( query=query, section_id=section_id, limit=limit, ) base_url = utils.template_url(*self.sdk_configuration.get_server_details()) url = base_url + '/hubs/search' if callable(self.sdk_configuration.security): headers, query_params = utils.get_security(self.sdk_configuration.security()) else: headers, query_params = utils.get_security(self.sdk_configuration.security) query_params = { **utils.get_query_params(operations.PerformSearchRequest, request), **query_params } headers['Accept'] = 'application/json' headers['user-agent'] = self.sdk_configuration.user_agent client = self.sdk_configuration.client try: req = self.sdk_configuration.get_hooks().before_request( hook_ctx, requests_http.Request('GET', url, params=query_params, headers=headers).prepare(), ) http_res = client.send(req) except Exception as e: _, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, None, e) raise e if utils.match_status_codes(['400','401','4XX','5XX'], http_res.status_code): http_res, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, http_res, None) if e: raise e else: result = self.sdk_configuration.get_hooks().after_success(hook_ctx, http_res) if isinstance(result, Exception): raise result http_res = result res = operations.PerformSearchResponse(status_code=http_res.status_code, content_type=http_res.headers.get('Content-Type'), raw_response=http_res) if http_res.status_code == 200: pass elif http_res.status_code == 400 or http_res.status_code >= 400 and http_res.status_code < 500 or http_res.status_code >= 500 and http_res.status_code < 600: raise errors.SDKError('API error occurred', http_res.status_code, http_res.text, http_res) elif http_res.status_code == 401: if utils.match_content_type(http_res.headers.get('Content-Type'), 'application/json'): out = utils.unmarshal_json(http_res.text, errors.PerformSearchResponseBody) out.raw_response = http_res raise out else: content_type = http_res.headers.get('Content-Type') raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res) else: raise errors.SDKError('unknown status code received', http_res.status_code, http_res.text, http_res) return res def perform_voice_search(self, query: str, section_id: Optional[float] = None, limit: Optional[float] = None) -> operations.PerformVoiceSearchResponse: r"""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. """ hook_ctx = HookContext(operation_id='performVoiceSearch', oauth2_scopes=[], security_source=self.sdk_configuration.security) request = operations.PerformVoiceSearchRequest( query=query, section_id=section_id, limit=limit, ) base_url = utils.template_url(*self.sdk_configuration.get_server_details()) url = base_url + '/hubs/search/voice' if callable(self.sdk_configuration.security): headers, query_params = utils.get_security(self.sdk_configuration.security()) else: headers, query_params = utils.get_security(self.sdk_configuration.security) query_params = { **utils.get_query_params(operations.PerformVoiceSearchRequest, request), **query_params } headers['Accept'] = 'application/json' headers['user-agent'] = self.sdk_configuration.user_agent client = self.sdk_configuration.client try: req = self.sdk_configuration.get_hooks().before_request( hook_ctx, requests_http.Request('GET', url, params=query_params, headers=headers).prepare(), ) http_res = client.send(req) except Exception as e: _, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, None, e) raise e if utils.match_status_codes(['400','401','4XX','5XX'], http_res.status_code): http_res, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, http_res, None) if e: raise e else: result = self.sdk_configuration.get_hooks().after_success(hook_ctx, http_res) if isinstance(result, Exception): raise result http_res = result res = operations.PerformVoiceSearchResponse(status_code=http_res.status_code, content_type=http_res.headers.get('Content-Type'), raw_response=http_res) if http_res.status_code == 200: pass elif http_res.status_code == 400 or http_res.status_code >= 400 and http_res.status_code < 500 or http_res.status_code >= 500 and http_res.status_code < 600: raise errors.SDKError('API error occurred', http_res.status_code, http_res.text, http_res) elif http_res.status_code == 401: if utils.match_content_type(http_res.headers.get('Content-Type'), 'application/json'): out = utils.unmarshal_json(http_res.text, errors.PerformVoiceSearchResponseBody) out.raw_response = http_res raise out else: content_type = http_res.headers.get('Content-Type') raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res) else: raise errors.SDKError('unknown status code received', http_res.status_code, http_res.text, http_res) return res def get_search_results(self, query: str) -> operations.GetSearchResultsResponse: r"""Get Search Results This will search the database for the string provided. """ hook_ctx = HookContext(operation_id='getSearchResults', oauth2_scopes=[], security_source=self.sdk_configuration.security) request = operations.GetSearchResultsRequest( query=query, ) base_url = utils.template_url(*self.sdk_configuration.get_server_details()) url = base_url + '/search' if callable(self.sdk_configuration.security): headers, query_params = utils.get_security(self.sdk_configuration.security()) else: headers, query_params = utils.get_security(self.sdk_configuration.security) query_params = { **utils.get_query_params(operations.GetSearchResultsRequest, request), **query_params } headers['Accept'] = 'application/json' headers['user-agent'] = self.sdk_configuration.user_agent client = self.sdk_configuration.client try: req = self.sdk_configuration.get_hooks().before_request( hook_ctx, requests_http.Request('GET', url, params=query_params, headers=headers).prepare(), ) http_res = client.send(req) except Exception as e: _, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, None, e) raise e if utils.match_status_codes(['400','401','4XX','5XX'], http_res.status_code): http_res, e = self.sdk_configuration.get_hooks().after_error(hook_ctx, http_res, None) if e: raise e else: result = self.sdk_configuration.get_hooks().after_success(hook_ctx, http_res) if isinstance(result, Exception): raise result http_res = result res = operations.GetSearchResultsResponse(status_code=http_res.status_code, content_type=http_res.headers.get('Content-Type'), raw_response=http_res) if http_res.status_code == 200: if utils.match_content_type(http_res.headers.get('Content-Type'), 'application/json'): out = utils.unmarshal_json(http_res.text, Optional[operations.GetSearchResultsResponseBody]) res.object = out else: content_type = http_res.headers.get('Content-Type') raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res) elif http_res.status_code == 400 or http_res.status_code >= 400 and http_res.status_code < 500 or http_res.status_code >= 500 and http_res.status_code < 600: raise errors.SDKError('API error occurred', http_res.status_code, http_res.text, http_res) elif http_res.status_code == 401: if utils.match_content_type(http_res.headers.get('Content-Type'), 'application/json'): out = utils.unmarshal_json(http_res.text, errors.GetSearchResultsResponseBody) out.raw_response = http_res raise out else: content_type = http_res.headers.get('Content-Type') raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res) else: raise errors.SDKError('unknown status code received', http_res.status_code, http_res.text, http_res) return res