@@ -12,6 +12,7 @@ use rs_plugin_common_interfaces::{
1212 lookup:: { RsLookupMatchType , RsLookupMovie } ,
1313} ;
1414use serde:: de:: DeserializeOwned ;
15+ use std:: env;
1516use tower:: Service ;
1617use trakt_people:: { TraktActorsResult , TraktPeopleSearchElement , TraktPerson } ;
1718
@@ -26,6 +27,8 @@ use self::{
2627// Context required for all requests
2728use unidecode:: unidecode;
2829
30+ pub const ENV_TRAKT_CLIENT_ID : & str = "REDSEAT_TRAKT_CLIENT_ID" ;
31+
2932/// Deserialize JSON response with detailed error path information
3033async fn json_with_path < T : DeserializeOwned > (
3134 response : Response ,
@@ -73,6 +76,13 @@ fn as_id_for_trakt(ids: &RsIds) -> Option<String> {
7376}
7477
7578impl TraktContext {
79+ pub fn from_env ( ) -> Self {
80+ let client_id = env:: var ( ENV_TRAKT_CLIENT_ID )
81+ . map ( |value| value. trim ( ) . to_string ( ) )
82+ . unwrap_or_default ( ) ;
83+ Self :: new ( client_id)
84+ }
85+
7686 pub fn new ( client_id : String ) -> Self {
7787 let base_url = reqwest:: Url :: parse ( "https://api.trakt.tv" ) . unwrap ( ) ;
7888 let mut headers = HeaderMap :: new ( ) ;
@@ -88,6 +98,17 @@ impl TraktContext {
8898 client,
8999 }
90100 }
101+
102+ fn ensure_configured ( & self ) -> crate :: Result < ( ) > {
103+ if self . client_id . is_empty ( ) {
104+ Err ( Error :: Error ( format ! (
105+ "Trakt API client ID is not configured; set {}" ,
106+ ENV_TRAKT_CLIENT_ID
107+ ) ) )
108+ } else {
109+ Ok ( ( ) )
110+ }
111+ }
91112}
92113
93114impl TraktContext {
@@ -153,6 +174,7 @@ impl TraktContext {
153174 & self ,
154175 search : & RsLookupMovie ,
155176 ) -> crate :: Result < Vec < ( Serie , Option < RsLookupMatchType > ) > > {
177+ self . ensure_configured ( ) ?;
156178 let query = search. name . as_deref ( ) . unwrap_or_default ( ) ;
157179 let url = self
158180 . base_url
@@ -167,7 +189,8 @@ impl TraktContext {
167189 . get ( url)
168190 . header ( "trakt-api-key" , & self . client_id )
169191 . send ( )
170- . await ?;
192+ . await ?
193+ . error_for_status ( ) ?;
171194
172195 let shows = r
173196 . json :: < Vec < TraktShowSearchElement > > ( )
@@ -371,6 +394,7 @@ impl TraktContext {
371394 & self ,
372395 search : & RsLookupMovie ,
373396 ) -> crate :: Result < Vec < ( Movie , Option < RsLookupMatchType > ) > > {
397+ self . ensure_configured ( ) ?;
374398 let query = search. name . as_deref ( ) . unwrap_or_default ( ) ;
375399 let url = self
376400 . base_url
@@ -385,7 +409,8 @@ impl TraktContext {
385409 . get ( url)
386410 . header ( "trakt-api-key" , & self . client_id )
387411 . send ( )
388- . await ?;
412+ . await ?
413+ . error_for_status ( ) ?;
389414 let movies = r
390415 . json :: < Vec < TraktMovieSearchElement > > ( )
391416 . await ?
@@ -489,6 +514,7 @@ impl TraktContext {
489514 & self ,
490515 search : & RsLookupMovie ,
491516 ) -> crate :: Result < Vec < ( Person , Option < RsLookupMatchType > ) > > {
517+ self . ensure_configured ( ) ?;
492518 let query = search. name . as_deref ( ) . unwrap_or_default ( ) ;
493519 let url = self
494520 . base_url
@@ -503,7 +529,8 @@ impl TraktContext {
503529 . get ( url)
504530 . header ( "trakt-api-key" , & self . client_id )
505531 . send ( )
506- . await ?;
532+ . await ?
533+ . error_for_status ( ) ?;
507534 let people = r
508535 . json :: < Vec < TraktPeopleSearchElement > > ( )
509536 . await ?
@@ -540,9 +567,7 @@ mod tests {
540567 #[ tokio:: test]
541568 #[ ignore] // requires network + valid Trakt API key
542569 async fn trakt_releases ( ) -> RsResult < ( ) > {
543- let trakt = TraktContext :: new (
544- "455f81b3409a8dd140a941e9250ff22b2ed92d68003491c3976363fe752a9024" . to_owned ( ) ,
545- ) ;
570+ let trakt = TraktContext :: from_env ( ) ;
546571
547572 let releases = trakt. get_movie_releases ( & exemple_movie ( ) ) . await ?;
548573
@@ -559,9 +584,7 @@ mod tests {
559584 #[ tokio:: test]
560585 #[ ignore] // requires network + valid Trakt API key
561586 async fn trakt_search_person ( ) -> RsResult < ( ) > {
562- let trakt = TraktContext :: new (
563- "455f81b3409a8dd140a941e9250ff22b2ed92d68003491c3976363fe752a9024" . to_owned ( ) ,
564- ) ;
587+ let trakt = TraktContext :: from_env ( ) ;
565588
566589 let search_result = trakt
567590 . search_person ( & RsLookupMovie {
0 commit comments