Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix url handling #81

Merged
merged 2 commits into from
Jul 30, 2024
Merged

Fix url handling #81

merged 2 commits into from
Jul 30, 2024

Conversation

jcdyer
Copy link
Contributor

@jcdyer jcdyer commented Jul 26, 2024

Proposed changes

The current implementation double-escapes query parameters passed in crate::common::Options. This uses proper formencoded parsing instead of string splitting, so the escaping only happens once.

I also introduce a pub fn urlencoded to match the other api endpoints, and to help demonstrate the erroneous behavior.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING.md doc
  • I have added tests and/or examples that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@jcdyer jcdyer requested review from bd-g and DamienDeepgram July 26, 2024 18:30
Base automatically changed from support-options-debug to main July 26, 2024 18:41
Comment on lines -83 to +78
config: self.0,
deepgram: self.0,
Copy link
Contributor Author

@jcdyer jcdyer Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this because I was getting confused about what a config is vs an options. It's a strictly internal change. Not visible to users.

@jcdyer jcdyer force-pushed the fix-url-handling branch from d784b29 to 7681a26 Compare July 26, 2024 19:54
@jcdyer jcdyer force-pushed the fix-url-handling branch 3 times, most recently from 2e8b8cf to e49900e Compare July 29, 2024 14:32
Comment on lines +163 to +166
/// # let mut need_token = std::env::var("DEEPGRAM_API_TOKEN").is_err();
/// # if need_token {
/// # std::env::set_var("DEEPGRAM_API_TOKEN", "abc")
/// # }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this pattern. The #s ensure it doesn't show up in the docs, but it's still kind of ugly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intent of those lines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a hard time fixing these imports in the examples in VSCode when they would break the build since there is no auto complete for them.

Comment on lines -255 to -272
// Add standard pre-recorded options
if let Some(options) = &self.builder.options {
let query_string = options_to_query_string(options);
let query_pairs: Vec<(Cow<str>, Cow<str>)> = query_string
.split('&')
.map(|s| {
let mut split = s.splitn(2, '=');
(
Cow::from(split.next().unwrap_or_default()),
Cow::from(split.next().unwrap_or_default()),
)
})
.collect();

for (key, value) in query_pairs {
pairs.append_pair(&key, &value);
}
}
Copy link
Contributor Author

@jcdyer jcdyer Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the original logic which double-quoted inputs. So if the original option contained an &, L257 would quote it as %26 (as it should be), and then after L270, the % in %26 would get quoted to %25, and the final result would be %2526. The new logic unquotes parameters instead of just splitting on separators.

&'a self,
options: Option<&'a Options>,
) -> StreamRequestBuilder<'a> {
pub fn stream_request_with_options(&self, options: Options) -> StreamRequestBuilder<'_> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout: I changed this public interface to take a crate::common::options::Options instead of an Option<&crate::common::options::Options>. It says it takes options, so only sometimes taking them is weird, especially if we also provide a version that doesn't take them, and since we can get the same behavior by passing an empty Options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked: This function was not available in 0.5.0, so this isn't a breaking change.

@jcdyer jcdyer merged commit d45441c into main Jul 30, 2024
18 checks passed
@jcdyer jcdyer deleted the fix-url-handling branch July 30, 2024 17:02
Copy link
Contributor

@DamienDeepgram DamienDeepgram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants