diff --git a/src/listen/websocket.rs b/src/listen/websocket.rs index e3813922..e503deb0 100644 --- a/src/listen/websocket.rs +++ b/src/listen/websocket.rs @@ -152,7 +152,51 @@ impl<'a, S, E> StreamRequestBuilder<'a, S, E> where S: Stream>, { - pub fn as_url(&self) -> std::result::Result { + + /// Return the options in urlencoded format. If serialization would + /// fail, this will also return an error. + /// + /// This is intended primarily to help with debugging API requests. + /// + /// ``` + /// use deepgram::{ + /// Deepgram, + /// DeepgramError, + /// common::options::{ + /// DetectLanguage, + /// Encoding, + /// Model, + /// Options, + /// }, + /// }; + /// # let mut need_token = std::env::var("DEEPGRAM_API_TOKEN").is_err(); + /// # if need_token { + /// # std::env::set_var("DEEPGRAM_API_TOKEN", "abc") + /// # } + /// let dg = Deepgram::new(std::env::var("DEEPGRAM_API_TOKEN").unwrap()); + /// let transcription = dg.transcription(); + /// let options = Options::builder() + /// .model(Model::Nova2) + /// .detect_language(DetectLanguage::Enabled) + /// .build(); + /// let builder = transcription + /// .stream_request_with_options::>>( + /// options, + /// ) + /// .no_delay(true); + /// + /// # if need_token { + /// # std::env::remove_var("DEEPGRAM_API_TOKEN"); + /// # } + /// + /// assert_eq!(&builder.urlencoded().unwrap(), "model=nova-2&detect_language=true&no_delay=true") + /// ``` + /// + pub fn urlencoded(&self) -> std::result::Result { + Ok(self.as_url()?.query().unwrap_or_default().to_string()) + } + + fn as_url(&self) -> std::result::Result { // Destructuring ensures we don't miss new fields if they get added let Self { deepgram: _, @@ -442,7 +486,7 @@ mod tests { ); // Currently fails because A&R is double escaped in the streaming URL assert_eq!( - builder.as_url().unwrap().query().unwrap(), + builder.urlencoded().unwrap(), opts.urlencoded().unwrap() ) }