Skip to content

Commit

Permalink
Add public urlencoded function to match other builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdyer committed Jul 26, 2024
1 parent 22b531a commit 7e0ccfd
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/listen/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,51 @@ impl<'a, S, E> StreamRequestBuilder<'a, S, E>
where
S: Stream<Item = std::result::Result<Bytes, E>>,
{
pub fn as_url(&self) -> std::result::Result<Url, serde_urlencoded::ser::Error> {

/// 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::<DeepgramError, futures::stream::Empty<Result<_, _>>>(
/// 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<String, serde_urlencoded::ser::Error> {
Ok(self.as_url()?.query().unwrap_or_default().to_string())
}

fn as_url(&self) -> std::result::Result<Url, serde_urlencoded::ser::Error> {
// Destructuring ensures we don't miss new fields if they get added
let Self {
deepgram: _,
Expand Down Expand Up @@ -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()
)
}
Expand Down

0 comments on commit 7e0ccfd

Please sign in to comment.