Skip to content

Use interface to define http client instead using *http.Client #100

@mtavano

Description

@mtavano

I think resend package could define an interface for http client so it will be easier handle rate limit.

example:

package resend

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

// Client handles communication with Resend API.
type Client struct {
	// HTTP client

	// proposal:
	client HTTPClient 

	// current:
	// client *http.Client

	// Api Key
	ApiKey string

	// .....
}


func NewCustomClient(httpClient HTTPClient, apiKey string) *Client {
    ...
 }

This makes it easier for each developer to handle rate limiting using the strategy that best fits their needs, while exposing only the required client method: .Do(req) (*res, error)

This way, I can plug in my own rate limiter via an interface instead of relying on a concrete pointer implementation.

example:

package impl

import (
    "net/http"

    "github.com/resend/resend-go"
    ratelimit "github.com/mtavano/devkito/clients/http"
)

func NewMailer(apiKey string, maxReq, windowSeconds int) *resend.Client {
    httpClient := ratelimit.NewClient(http.Options{
        MaxRequest: 2,
        WindowInSeconds: 1,
    }, http.DefaultClient)

    return resend.NewCustomClient(httpClient, apiKet)
}

I would like to help submitting the PR but havent seen any contribution guide.

That said, I’ll probably implement this in a fork anyway, since I don’t see an elegant way to achieve this without wrapping the entire package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions