From 36f2df62e956b0bd2142b50c56af77996b36b6e4 Mon Sep 17 00:00:00 2001 From: Andrew Halle Date: Thu, 3 Aug 2023 16:58:37 -0700 Subject: [PATCH] refactor!: remove generic from `Deepgram` The `Deepgram` struct is unnecessarily generic. Storing the API key as a generic offers flexibility which probably isn't warranted by any use cases. Fixes #49 --- CHANGELOG.md | 6 ++++++ Cargo.toml | 2 +- src/billing.rs | 12 ++++++------ src/invitations.rs | 12 ++++++------ src/keys.rs | 12 ++++++------ src/lib.rs | 15 +++++---------- src/members.rs | 12 ++++++------ src/projects.rs | 12 ++++++------ src/scopes.rs | 12 ++++++------ src/transcription.rs | 10 +++++----- src/transcription/live.rs | 27 +++++++++------------------ src/transcription/prerecorded.rs | 2 +- src/usage.rs | 12 ++++++------ 13 files changed, 69 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0156672..b1d2b7b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed +- Remove generic from `Deepgram` struct. + ## [0.3.0] ### Added @@ -16,4 +21,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Use Rustls instead of OpenSSL. +[Unreleased]: https://github.com/deepgram-devs/deepgram-rust-sdk/compare/0.3.0...HEAD [0.3.0]: https://github.com/deepgram-devs/deepgram-rust-sdk/compare/0.2.1...0.3.0 diff --git a/Cargo.toml b/Cargo.toml index eb3995be..41a1f601 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "deepgram" -version = "0.3.0" +version = "0.4.0-alpha.1" authors = ["Deepgram "] edition = "2021" description = "Official Rust SDK for Deepgram's automated speech recognition APIs." diff --git a/src/billing.rs b/src/billing.rs index 03f9cbf0..a3d775d5 100644 --- a/src/billing.rs +++ b/src/billing.rs @@ -18,23 +18,23 @@ use response::{Balance, Balances}; /// /// [api]: https://developers.deepgram.com/api-reference/#billing #[derive(Debug, Clone)] -pub struct Billing<'a, K: AsRef>(&'a Deepgram); +pub struct Billing<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Billing`] from a [`Deepgram`]. - pub fn billing(&'a self) -> Billing<'a, K> { + pub fn billing(&self) -> Billing<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Billing<'a, K> { +impl<'a> From<&'a Deepgram> for Billing<'a> { /// Construct a new [`Billing`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl> Billing<'_, K> { +impl Billing<'_> { /// Get the outstanding balances for the specified project. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/invitations.rs b/src/invitations.rs index 854b4c0c..fae53b64 100644 --- a/src/invitations.rs +++ b/src/invitations.rs @@ -18,23 +18,23 @@ use response::Message; /// /// [api]: https://developers.deepgram.com/api-reference/#invitations #[derive(Debug, Clone)] -pub struct Invitations<'a, K: AsRef>(&'a Deepgram); +pub struct Invitations<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Invitations`] from a [`Deepgram`]. - pub fn invitations(&'a self) -> Invitations<'a, K> { + pub fn invitations(&self) -> Invitations<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Invitations<'a, K> { +impl<'a> From<&'a Deepgram> for Invitations<'a> { /// Construct a new [`Invitations`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl> Invitations<'_, K> { +impl Invitations<'_> { /// Remove the authenticated account from the specified project. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/keys.rs b/src/keys.rs index 8fd39681..3b76b1c9 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -20,23 +20,23 @@ use response::{MemberAndApiKey, MembersAndApiKeys, Message, NewApiKey}; /// /// [api]: https://developers.deepgram.com/api-reference/#keys #[derive(Debug, Clone)] -pub struct Keys<'a, K: AsRef>(&'a Deepgram); +pub struct Keys<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Keys`] from a [`Deepgram`]. - pub fn keys(&'a self) -> Keys<'a, K> { + pub fn keys(&self) -> Keys<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Keys<'a, K> { +impl<'a> From<&'a Deepgram> for Keys<'a> { /// Construct a new [`Keys`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl<'a, K: AsRef> Keys<'_, K> { +impl Keys<'_> { /// Get keys for the specified project. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/lib.rs b/src/lib.rs index 017855cd..f0839b9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,11 +30,8 @@ mod response; /// /// Make transcriptions requests using [`Deepgram::transcription`]. #[derive(Debug, Clone)] -pub struct Deepgram -where - K: AsRef, -{ - api_key: K, +pub struct Deepgram { + api_key: String, client: reqwest::Client, } @@ -79,10 +76,7 @@ pub enum DeepgramError { type Result = std::result::Result; -impl Deepgram -where - K: AsRef, -{ +impl Deepgram { /// Construct a new Deepgram client. /// /// Create your first API key on the [Deepgram Console][console]. @@ -92,7 +86,7 @@ where /// # Panics /// /// Panics under the same conditions as [`reqwest::Client::new`]. - pub fn new(api_key: K) -> Self { + pub fn new>(api_key: K) -> Self { static USER_AGENT: &str = concat!( env!("CARGO_PKG_NAME"), "/", @@ -109,6 +103,7 @@ where ); header }; + let api_key = api_key.as_ref().to_owned(); Deepgram { api_key, diff --git a/src/members.rs b/src/members.rs index b14d27dc..8651da52 100644 --- a/src/members.rs +++ b/src/members.rs @@ -16,23 +16,23 @@ pub mod response; /// /// [api]: https://developers.deepgram.com/api-reference/#members #[derive(Debug, Clone)] -pub struct Members<'a, K: AsRef>(&'a Deepgram); +pub struct Members<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Members`] from a [`Deepgram`]. - pub fn members(&'a self) -> Members<'a, K> { + pub fn members(&self) -> Members<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Members<'a, K> { +impl<'a> From<&'a Deepgram> for Members<'a> { /// Construct a new [`Members`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl> Members<'_, K> { +impl Members<'_> { /// Get all members of the specified project. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/projects.rs b/src/projects.rs index bd03a047..64a170e4 100644 --- a/src/projects.rs +++ b/src/projects.rs @@ -23,23 +23,23 @@ use response::{Message, Project}; /// [console]: https://console.deepgram.com/ /// [api]: https://developers.deepgram.com/api-reference/#projects #[derive(Debug, Clone)] -pub struct Projects<'a, K: AsRef>(&'a Deepgram); +pub struct Projects<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Projects`] from a [`Deepgram`]. - pub fn projects(&'a self) -> Projects<'a, K> { + pub fn projects(&self) -> Projects<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Projects<'a, K> { +impl<'a> From<&'a Deepgram> for Projects<'a> { /// Construct a new [`Projects`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl<'a, K: AsRef> Projects<'_, K> { +impl Projects<'_> { /// Get all projects. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/scopes.rs b/src/scopes.rs index cedbec65..aa75c99a 100644 --- a/src/scopes.rs +++ b/src/scopes.rs @@ -20,23 +20,23 @@ use response::Message; /// /// [api]: https://developers.deepgram.com/api-reference/#scopes #[derive(Debug, Clone)] -pub struct Scopes<'a, K: AsRef>(&'a Deepgram); +pub struct Scopes<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Scopes`] from a [`Deepgram`]. - pub fn scopes(&'a self) -> Scopes<'a, K> { + pub fn scopes(&self) -> Scopes<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Scopes<'a, K> { +impl<'a> From<&'a Deepgram> for Scopes<'a> { /// Construct a new [`Scopes`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl> Scopes<'_, K> { +impl Scopes<'_> { /// Get the specified project scopes assigned to the specified member. /// /// See the [Deepgram API Reference][api] for more info. diff --git a/src/transcription.rs b/src/transcription.rs index 5303e4e3..6a576e79 100644 --- a/src/transcription.rs +++ b/src/transcription.rs @@ -17,18 +17,18 @@ pub mod prerecorded; /// /// [api]: https://developers.deepgram.com/api-reference/#transcription #[derive(Debug, Clone)] -pub struct Transcription<'a, K: AsRef>(&'a Deepgram); +pub struct Transcription<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Transcription`] from a [`Deepgram`]. - pub fn transcription(&'a self) -> Transcription<'a, K> { + pub fn transcription(&self) -> Transcription<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Transcription<'a, K> { +impl<'a> From<&'a Deepgram> for Transcription<'a> { /// Construct a new [`Transcription`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } diff --git a/src/transcription/live.rs b/src/transcription/live.rs index b0c0c6b7..1f43ec37 100644 --- a/src/transcription/live.rs +++ b/src/transcription/live.rs @@ -31,12 +31,11 @@ use crate::{Deepgram, DeepgramError, Result}; use super::Transcription; #[derive(Debug)] -pub struct StreamRequestBuilder<'a, S, K, E> +pub struct StreamRequestBuilder<'a, S, E> where S: Stream>, - K: AsRef, { - config: &'a Deepgram, + config: &'a Deepgram, source: Option, encoding: Option, sample_rate: Option, @@ -86,10 +85,10 @@ struct FileChunker { file: ReaderStream, } -impl> Transcription<'_, K> { +impl Transcription<'_> { pub fn stream_request>>( &self, - ) -> StreamRequestBuilder { + ) -> StreamRequestBuilder { StreamRequestBuilder { config: self.0, source: None, @@ -142,10 +141,9 @@ impl Stream for FileChunker { } } -impl<'a, S, K, E> StreamRequestBuilder<'a, S, K, E> +impl<'a, S, E> StreamRequestBuilder<'a, S, E> where S: Stream>, - K: AsRef, { pub fn stream(mut self, stream: S) -> Self { self.source = Some(stream); @@ -172,16 +170,13 @@ where } } -impl<'a, K> StreamRequestBuilder<'a, Receiver>, K, DeepgramError> -where - K: AsRef, -{ +impl<'a> StreamRequestBuilder<'a, Receiver>, DeepgramError> { pub async fn file( mut self, filename: impl AsRef, frame_size: usize, frame_delay: Duration, - ) -> Result>, K, DeepgramError>> { + ) -> Result>, DeepgramError>> { let file = File::open(filename).await?; let mut chunker = FileChunker::new(file, frame_size); let (mut tx, rx) = mpsc::channel(1); @@ -200,10 +195,9 @@ where } } -impl StreamRequestBuilder<'_, S, K, E> +impl StreamRequestBuilder<'_, S, E> where S: Stream> + Send + Unpin + 'static, - K: AsRef, E: Send + std::fmt::Debug, { pub async fn start(self) -> Result>> { @@ -236,10 +230,7 @@ where let request = Request::builder() .method("GET") .uri(base.to_string()) - .header( - "authorization", - format!("token {}", config.api_key.as_ref()), - ) + .header("authorization", format!("token {}", config.api_key)) .header("sec-websocket-key", client::generate_key()) .header("host", "api.deepgram.com") .header("connection", "upgrade") diff --git a/src/transcription/prerecorded.rs b/src/transcription/prerecorded.rs index 72a408b4..23713617 100644 --- a/src/transcription/prerecorded.rs +++ b/src/transcription/prerecorded.rs @@ -19,7 +19,7 @@ use response::{CallbackResponse, Response}; static DEEPGRAM_API_URL_LISTEN: &str = "https://api.deepgram.com/v1/listen"; -impl> Transcription<'_, K> { +impl Transcription<'_> { /// Sends a request to Deepgram to transcribe pre-recorded audio. /// If you wish to use the Callback feature, you should use [`Transcription::prerecorded_callback`] instead. /// diff --git a/src/usage.rs b/src/usage.rs index 7cb894b6..ad99a63f 100644 --- a/src/usage.rs +++ b/src/usage.rs @@ -21,23 +21,23 @@ use response::{Fields, Request, Requests, UsageSummary}; /// /// [api]: https://developers.deepgram.com/api-reference/#usage #[derive(Debug, Clone)] -pub struct Usage<'a, K: AsRef>(&'a Deepgram); +pub struct Usage<'a>(&'a Deepgram); -impl<'a, K: AsRef> Deepgram { +impl Deepgram { /// Construct a new [`Usage`] from a [`Deepgram`]. - pub fn usage(&'a self) -> Usage<'a, K> { + pub fn usage(&self) -> Usage<'_> { self.into() } } -impl<'a, K: AsRef> From<&'a Deepgram> for Usage<'a, K> { +impl<'a> From<&'a Deepgram> for Usage<'a> { /// Construct a new [`Usage`] from a [`Deepgram`]. - fn from(deepgram: &'a Deepgram) -> Self { + fn from(deepgram: &'a Deepgram) -> Self { Self(deepgram) } } -impl<'a, K: AsRef> Usage<'_, K> { +impl Usage<'_> { /// Get all requests sent to the Deepgram API for the specified project. /// /// See the [Deepgram API Reference][api] for more info.