Skip to content

Commit

Permalink
refactor!: remove generic from Deepgram
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrewhalle committed Aug 4, 2023
1 parent 697ce7c commit 36f2df6
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 77 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deepgram"
version = "0.3.0"
version = "0.4.0-alpha.1"
authors = ["Deepgram <[email protected]>"]
edition = "2021"
description = "Official Rust SDK for Deepgram's automated speech recognition APIs."
Expand Down
12 changes: 6 additions & 6 deletions src/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>>(&'a Deepgram<K>);
pub struct Billing<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Billing<'a, K> {
impl<'a> From<&'a Deepgram> for Billing<'a> {
/// Construct a new [`Billing`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<K: AsRef<str>> Billing<'_, K> {
impl Billing<'_> {
/// Get the outstanding balances for the specified project.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
12 changes: 6 additions & 6 deletions src/invitations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ use response::Message;
///
/// [api]: https://developers.deepgram.com/api-reference/#invitations
#[derive(Debug, Clone)]
pub struct Invitations<'a, K: AsRef<str>>(&'a Deepgram<K>);
pub struct Invitations<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Invitations<'a, K> {
impl<'a> From<&'a Deepgram> for Invitations<'a> {
/// Construct a new [`Invitations`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<K: AsRef<str>> Invitations<'_, K> {
impl Invitations<'_> {
/// Remove the authenticated account from the specified project.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
12 changes: 6 additions & 6 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>>(&'a Deepgram<K>);
pub struct Keys<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Keys<'a, K> {
impl<'a> From<&'a Deepgram> for Keys<'a> {
/// Construct a new [`Keys`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<'a, K: AsRef<str>> Keys<'_, K> {
impl Keys<'_> {
/// Get keys for the specified project.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
15 changes: 5 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ mod response;
///
/// Make transcriptions requests using [`Deepgram::transcription`].
#[derive(Debug, Clone)]
pub struct Deepgram<K>
where
K: AsRef<str>,
{
api_key: K,
pub struct Deepgram {
api_key: String,
client: reqwest::Client,
}

Expand Down Expand Up @@ -79,10 +76,7 @@ pub enum DeepgramError {

type Result<T> = std::result::Result<T, DeepgramError>;

impl<K> Deepgram<K>
where
K: AsRef<str>,
{
impl Deepgram {
/// Construct a new Deepgram client.
///
/// Create your first API key on the [Deepgram Console][console].
Expand All @@ -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<K: AsRef<str>>(api_key: K) -> Self {
static USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
Expand All @@ -109,6 +103,7 @@ where
);
header
};
let api_key = api_key.as_ref().to_owned();

Deepgram {
api_key,
Expand Down
12 changes: 6 additions & 6 deletions src/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ pub mod response;
///
/// [api]: https://developers.deepgram.com/api-reference/#members
#[derive(Debug, Clone)]
pub struct Members<'a, K: AsRef<str>>(&'a Deepgram<K>);
pub struct Members<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Members<'a, K> {
impl<'a> From<&'a Deepgram> for Members<'a> {
/// Construct a new [`Members`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<K: AsRef<str>> Members<'_, K> {
impl Members<'_> {
/// Get all members of the specified project.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
12 changes: 6 additions & 6 deletions src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>>(&'a Deepgram<K>);
pub struct Projects<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Projects<'a, K> {
impl<'a> From<&'a Deepgram> for Projects<'a> {
/// Construct a new [`Projects`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<'a, K: AsRef<str>> Projects<'_, K> {
impl Projects<'_> {
/// Get all projects.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
12 changes: 6 additions & 6 deletions src/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ use response::Message;
///
/// [api]: https://developers.deepgram.com/api-reference/#scopes
#[derive(Debug, Clone)]
pub struct Scopes<'a, K: AsRef<str>>(&'a Deepgram<K>);
pub struct Scopes<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Scopes<'a, K> {
impl<'a> From<&'a Deepgram> for Scopes<'a> {
/// Construct a new [`Scopes`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<K: AsRef<str>> Scopes<'_, K> {
impl Scopes<'_> {
/// Get the specified project scopes assigned to the specified member.
///
/// See the [Deepgram API Reference][api] for more info.
Expand Down
10 changes: 5 additions & 5 deletions src/transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ pub mod prerecorded;
///
/// [api]: https://developers.deepgram.com/api-reference/#transcription
#[derive(Debug, Clone)]
pub struct Transcription<'a, K: AsRef<str>>(&'a Deepgram<K>);
pub struct Transcription<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Transcription<'a, K> {
impl<'a> From<&'a Deepgram> for Transcription<'a> {
/// Construct a new [`Transcription`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}
27 changes: 9 additions & 18 deletions src/transcription/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = std::result::Result<Bytes, E>>,
K: AsRef<str>,
{
config: &'a Deepgram<K>,
config: &'a Deepgram,
source: Option<S>,
encoding: Option<String>,
sample_rate: Option<u32>,
Expand Down Expand Up @@ -86,10 +85,10 @@ struct FileChunker {
file: ReaderStream<File>,
}

impl<K: AsRef<str>> Transcription<'_, K> {
impl Transcription<'_> {
pub fn stream_request<E, S: Stream<Item = std::result::Result<Bytes, E>>>(
&self,
) -> StreamRequestBuilder<S, K, E> {
) -> StreamRequestBuilder<S, E> {
StreamRequestBuilder {
config: self.0,
source: None,
Expand Down Expand Up @@ -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<Item = std::result::Result<Bytes, E>>,
K: AsRef<str>,
{
pub fn stream(mut self, stream: S) -> Self {
self.source = Some(stream);
Expand All @@ -172,16 +170,13 @@ where
}
}

impl<'a, K> StreamRequestBuilder<'a, Receiver<Result<Bytes>>, K, DeepgramError>
where
K: AsRef<str>,
{
impl<'a> StreamRequestBuilder<'a, Receiver<Result<Bytes>>, DeepgramError> {
pub async fn file(
mut self,
filename: impl AsRef<Path>,
frame_size: usize,
frame_delay: Duration,
) -> Result<StreamRequestBuilder<'a, Receiver<Result<Bytes>>, K, DeepgramError>> {
) -> Result<StreamRequestBuilder<'a, Receiver<Result<Bytes>>, DeepgramError>> {
let file = File::open(filename).await?;
let mut chunker = FileChunker::new(file, frame_size);
let (mut tx, rx) = mpsc::channel(1);
Expand All @@ -200,10 +195,9 @@ where
}
}

impl<S, K, E> StreamRequestBuilder<'_, S, K, E>
impl<S, E> StreamRequestBuilder<'_, S, E>
where
S: Stream<Item = std::result::Result<Bytes, E>> + Send + Unpin + 'static,
K: AsRef<str>,
E: Send + std::fmt::Debug,
{
pub async fn start(self) -> Result<Receiver<Result<StreamResponse>>> {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/transcription/prerecorded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use response::{CallbackResponse, Response};

static DEEPGRAM_API_URL_LISTEN: &str = "https://api.deepgram.com/v1/listen";

impl<K: AsRef<str>> 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.
///
Expand Down
12 changes: 6 additions & 6 deletions src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>>(&'a Deepgram<K>);
pub struct Usage<'a>(&'a Deepgram);

impl<'a, K: AsRef<str>> Deepgram<K> {
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<str>> From<&'a Deepgram<K>> for Usage<'a, K> {
impl<'a> From<&'a Deepgram> for Usage<'a> {
/// Construct a new [`Usage`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram<K>) -> Self {
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}

impl<'a, K: AsRef<str>> 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.
Expand Down

0 comments on commit 36f2df6

Please sign in to comment.