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

Address Common vs REST Defaults for Clients #254

Merged
merged 1 commit into from
Jul 3, 2024

Conversation

dvonthenen
Copy link
Contributor

@dvonthenen dvonthenen commented Jul 2, 2024

Proposed changes

This addresses which internal client to use in which situation. It turned out that the Common Client was being used in just about every situation and that the REST client, which is a lower-level client (although not being used), had a more restricted options set.

This clears up the confusion in the SDK and also makes the various level of client (API specific [most specific] - common - rest - HTTP [most general]). These are all internal changes not exposed to the user.

Tested all examples and they seem to work just fine.

Types of changes

What types of changes does your code introduce to the community Go SDK?
Put an x in the boxes that apply

  • 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

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

NA

Summary by CodeRabbit

  • Refactor

    • Simplified WebSocket initialization with default parameters for improved usability.
    • Updated Client structs to use RESTClient for more consistent request handling across the system.
    • Enhanced request processing by centralizing setup and execution methods.
  • Bug Fixes

    • Removed redundant logging from the performAction function to clean up console output.
    • Deprecated Tier field in LiveTranscriptionOptions, preparing for future removal.
  • Enhancements

    • Improved handling of HTTP requests and responses with updated methods and error handling.
    • Enhanced JSON encoding and request setup for text-to-speech operations.

Copy link

coderabbitai bot commented Jul 2, 2024

Warning

Rate limit exceeded

@dvonthenen has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 21 minutes and 37 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 94d3475 and f038bf5.

Walkthrough

The changes streamline and update the functionality across several packages in the codebase. Key modifications include simplifying WebSocket initialization, updating request handling methods, embedding RESTClient types for more consistent API usage, and marking fields as deprecated. These transformations bring more uniformity, improve error handling, and prepare the code for future updates.

Changes

File Path Change Summary
examples/text-to-speech/.../main.go Replaced speak.NewWebSocket call with speak.NewWebSocketWithDefaults for simplified parameters.
pkg/api/manage/v1/manage.go Changed Client.Client field to Client.RESTClient during initialization.
pkg/api/speak/v1/rest/speak.go Removed logging statement fmt.Printf("retVal: %v\n", retVal) from performAction function.
pkg/client/analyze/v1/client.go Updated DoText and DoURL methods to use c.Do instead of custom c.HTTPClient.Do execution blocks.
pkg/client/common/v1/common.go SetupRequest method now sets the "Content-Type" header to "application/json"; ProcessRequest method removed; HandleResponse method now logs HTTP response handling.
pkg/client/common/v1/types.go Changed Client struct to embed RESTClient using an alias instead of rest.Client; added type RESTClient = rest.Client.
pkg/client/interfaces/v1/types-stream.go Deprecated Tier field in LiveTranscriptionOptions struct, indicating future removal.
pkg/client/listen/v1/rest/client.go Updated DoStream method to use RESTClient methods for request setup and execution.
pkg/client/manage/v1/client.go Updated APIRequest method to use c.Do for executing HTTP requests, affecting header setting and response processing.
pkg/client/rest/v1/rest.go Added SetupRequest function to prepare REST requests; updated Do function to handle HTTP and Deepgram-specific errors.
pkg/client/speak/v1/rest/client.go Modified DoText method to use JSON encoding for text, adjusted request setup to use JSON text, updated comments and logging, and added handling of response headers using HTTPClient and HandleResponse.

Sequence Diagrams

WebSocket Initialization

sequenceDiagram
    participant Main
    participant Speak
    Note right of Main: Old Flow
    Main->>Speak: speak.NewWebSocket(ctx, "", cOptions, ttsOptions, callback)
    Note right of Main: New Flow
    Main->>Speak: speak.NewWebSocketWithDefaults(ctx, ttsOptions, callback)
Loading

Client Setup and Request Handling

sequenceDiagram
    participant Client
    participant Common
    Note right of Client: Old Flow
    Client->>Common: c.HTTPClient.Do(ctx, req, func(res *http.Response) error { ... })
    Note right of Client: New Flow
    Client->>Common: c.Do(ctx, req, resBody)
Loading

Deprecated Field

sequenceDiagram
    participant Client
    Note right of Client: New Flow
    Client->>Note: Tier in LiveTranscriptionOptions <br>Deprecated: This field will be <br>removed in a future release.
Loading

These diagrams provide a visual guide to key changes in control flow and functional updates in the codebase. They illustrate both old and new implementation flows to highlight differences.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c795e99 and aaf7892.

Files selected for processing (10)
  • examples/text-to-speech/websocket/simple/main.go (1 hunks)
  • pkg/api/manage/v1/manage.go (1 hunks)
  • pkg/api/speak/v1/rest/speak.go (2 hunks)
  • pkg/client/analyze/v1/client.go (3 hunks)
  • pkg/client/common/v1/common.go (1 hunks)
  • pkg/client/common/v1/types.go (1 hunks)
  • pkg/client/listen/v1/rest/client.go (1 hunks)
  • pkg/client/manage/v1/client.go (1 hunks)
  • pkg/client/rest/v1/rest.go (5 hunks)
  • pkg/client/speak/v1/rest/client.go (2 hunks)
Additional comments not posted (20)
pkg/client/common/v1/types.go (2)

11-11: LGTM! Introduce the type alias RESTClient.

The introduction of the type alias RESTClient improves readability and consistency.


15-15: LGTM! Embed RESTClient in Client.

Embedding RESTClient in Client aligns with the PR objectives to clarify the hierarchy and usage of various client levels.

However, ensure that all instances of Client in the codebase are updated to reflect this change.

pkg/api/manage/v1/manage.go (1)

32-32: LGTM! Initialize the RESTClient field.

Initializing the RESTClient field aligns with the PR objectives to clarify the hierarchy and usage of various client levels.

However, ensure that all instances of Client initialization in the codebase are updated to reflect this change.

pkg/client/manage/v1/client.go (2)

55-56: LGTM! Use SetupRequest method from RESTClient.

The comments clarify the use of SetupRequest method from RESTClient, aligning with the PR objectives.


63-72: LGTM! Use Do method from RESTClient.

The comments clarify the use of Do method from RESTClient, aligning with the PR objectives.

However, ensure that all instances of Do method calls in the codebase are updated to reflect this change.

pkg/client/speak/v1/rest/client.go (1)

84-91: LGTM! But verify the function usage in the codebase.

The changes are approved. The function now wraps the text in a JSON object and encodes it before sending the request.

However, ensure that all function calls to DoText match the new signature and usage.

pkg/api/speak/v1/rest/speak.go (4)

Line range hint 31-33: LGTM!

The changes are approved. The function calls DoText with the appropriate parameters.


Line range hint 52-54: LGTM!

The changes are approved. The function calls DoText with the appropriate parameters.


Line range hint 72-74: LGTM!

The changes are approved. The function calls ToFile with the appropriate parameters.


Line range hint 123-125: LGTM!

The changes are approved. The function processes the response and returns the appropriate values.

pkg/client/rest/v1/rest.go (2)

Line range hint 48-71: LGTM!

The changes are approved. The function sets the appropriate headers for the request.


Line range hint 74-136: LGTM!

The changes are approved. The function handles the response appropriately and processes the result.

pkg/client/common/v1/common.go (3)

Line range hint 48-65: LGTM!

The changes are approved. The function sets the appropriate headers for the request.


Line range hint 67-92: LGTM!

The changes are approved. The function handles the response appropriately and processes the result.


Line range hint 94-132: LGTM!

The changes are approved. The function processes the response body appropriately and returns the result.

pkg/client/listen/v1/rest/client.go (3)

Line range hint 222-223: Verify the usage of c.SetupRequest.

Ensure that c.SetupRequest is correctly used and that it sets the necessary headers for the request.

Verification successful

Verification successful.

The usage of c.SetupRequest in the pkg/client/listen/v1/rest/client.go file is correct. The method is implemented in the Client struct and prepares a new HTTP request with common headers set.

  • pkg/client/rest/v1/rest.go: func (c *Client) SetupRequest(ctx context.Context, method, uri string, body io.Reader) (*http.Request, error)
  • pkg/client/common/v1/common.go: func (c *Client) SetupRequest(ctx context.Context, method, uri string, body io.Reader) (*http.Request, error)
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `SetupRequest` in the `Client`.

# Test: Search for the `SetupRequest` method in the `Client`.
rg --type go --pattern 'func (c *Client) SetupRequest'

Length of output: 468



Script:

#!/bin/bash
# Description: Verify the implementation of `SetupRequest` in the `Client`.

# Test: Search for the `SetupRequest` method in the `Client`.
rg 'func (c \*Client) SetupRequest' --type go

Length of output: 47



Script:

#!/bin/bash
# Description: Verify the definition or usage of `SetupRequest` across the codebase.

# Test: Search for any definition or usage of `SetupRequest`.
rg 'SetupRequest' --type go

Length of output: 2423


126-129: Verify the usage of RESTClient.SetupRequest.

Ensure that RESTClient.SetupRequest is correctly used and that it sets the necessary headers for the request.


136-145: Verify the usage of RESTClient.Do.

Ensure that RESTClient.Do is correctly used and handles the response appropriately.

pkg/client/analyze/v1/client.go (2)

222-223: Verify the usage of c.SetupRequest.

Ensure that c.SetupRequest is correctly used and that it sets the necessary headers for the request.


153-154: Verify the usage of c.SetupRequest.

Ensure that c.SetupRequest is correctly used and that it sets the necessary headers for the request.

Verification successful

Verify the usage of c.SetupRequest.

The SetupRequest method in pkg/client/common/v1/common.go is correctly implemented and sets the necessary headers for the request. The comment about using c.SetupRequest instead of c.RESTClient.SetupRequest is valid.

  • pkg/client/common/v1/common.go:
    • func (c *Client) SetupRequest(ctx context.Context, method, uri string, body io.Reader) (*http.Request, error)
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `SetupRequest` in the `Client`.

# Test: Search for the `SetupRequest` method in the `Client`.
rg --type go --pattern 'func (c *Client) SetupRequest'

Length of output: 468



Script:

#!/bin/bash
# Description: Verify the implementation of `SetupRequest` in the `Client`.

# Test: Search for the `SetupRequest` method in the `Client`.
rg 'func (c \*Client\) SetupRequest'

Length of output: 126



Script:

#!/bin/bash
# Description: Verify the implementation of `SetupRequest` in the `Client`.

# Test: Search for the `SetupRequest` method in the `Client`.
rg 'func \(c \*Client\) SetupRequest'

Length of output: 321



Script:

#!/bin/bash
# Description: Check the imports in `pkg/client/analyze/v1/client.go` to determine which `SetupRequest` method is being used.

# Test: Search for import statements in `pkg/client/analyze/v1/client.go`.
rg '^import' -A 10 pkg/client/analyze/v1/client.go

Length of output: 155



Script:

#!/bin/bash
# Description: Search for `SetupRequest` references within `pkg/client/analyze/v1/client.go` and trace its origin.

# Test: Search for `SetupRequest` references in `pkg/client/analyze/v1/client.go`.
rg 'SetupRequest' pkg/client/analyze/v1/client.go -A 5 -B 5

Length of output: 1163



Script:

#!/bin/bash
# Description: Search for the declaration of `c` within `pkg/client/analyze/v1/client.go` to determine its type.

# Test: Search for `c :=` or `var c` in `pkg/client/analyze/v1/client.go`.
rg '\bc\b' pkg/client/analyze/v1/client.go -A 5 -B 5

Length of output: 4552



Script:

#!/bin/bash
# Description: Verify the `common.New` function to ensure it initializes `SetupRequest` correctly.

# Test: Locate and inspect the `New` function in `pkg/client/common/v1/common.go`.
rg 'func New' pkg/client/common/v1/common.go -A 10 -B 10

Length of output: 515



Script:

#!/bin/bash
# Description: Retrieve the complete implementation of the `New` function in `pkg/client/common/v1/common.go`.

# Test: Display the complete implementation of the `New` function.
rg 'func New' pkg/client/common/v1/common.go -A 50

Length of output: 1664

examples/text-to-speech/websocket/simple/main.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aaf7892 and 94d3475.

Files selected for processing (11)
  • examples/text-to-speech/websocket/simple/main.go (1 hunks)
  • pkg/api/manage/v1/manage.go (1 hunks)
  • pkg/api/speak/v1/rest/speak.go (2 hunks)
  • pkg/client/analyze/v1/client.go (3 hunks)
  • pkg/client/common/v1/common.go (1 hunks)
  • pkg/client/common/v1/types.go (1 hunks)
  • pkg/client/interfaces/v1/types-stream.go (1 hunks)
  • pkg/client/listen/v1/rest/client.go (1 hunks)
  • pkg/client/manage/v1/client.go (1 hunks)
  • pkg/client/rest/v1/rest.go (5 hunks)
  • pkg/client/speak/v1/rest/client.go (2 hunks)
Files skipped from review as they are similar to previous changes (9)
  • examples/text-to-speech/websocket/simple/main.go
  • pkg/api/manage/v1/manage.go
  • pkg/api/speak/v1/rest/speak.go
  • pkg/client/analyze/v1/client.go
  • pkg/client/common/v1/common.go
  • pkg/client/common/v1/types.go
  • pkg/client/listen/v1/rest/client.go
  • pkg/client/manage/v1/client.go
  • pkg/client/rest/v1/rest.go
Additional comments not posted (4)
pkg/client/interfaces/v1/types-stream.go (1)

40-40: Deprecation Notice Added

The Tier field has been marked as deprecated. Ensure that any usage of this field is updated or removed in future releases.

pkg/client/speak/v1/rest/client.go (3)

84-90: LGTM! JSON Encoding for Text Source

The text is now properly encoded as JSON before being sent in the request. This ensures that the text is correctly formatted for the API.


92-93: LGTM! HTTP Request Setup

The SetupRequest method is used to create the HTTP request, ensuring consistent request setup with common headers.


103-111: LGTM! Handling of Response Headers

The use of HTTPClient.Do and HandleResponse methods allows for the extraction of response headers, which is necessary for processing the response correctly.

@dvonthenen dvonthenen force-pushed the fix-common-rest-client-use branch 2 times, most recently from f9a480e to 4ad60ed Compare July 3, 2024 15:57
@SandraRodgers SandraRodgers self-requested a review July 3, 2024 16:08
@dvonthenen dvonthenen merged commit 1456965 into deepgram:main Jul 3, 2024
2 checks passed
@dvonthenen dvonthenen deleted the fix-common-rest-client-use branch July 3, 2024 16:25
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.

2 participants