Skip to content

Commit

Permalink
refactor(rest): rename client
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Sep 7, 2024
1 parent c7fa0d7 commit 5002cf2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
20 changes: 10 additions & 10 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
)

var (
_ Client = (*client)(nil)
_ Client = (*restClient)(nil)
// DefaultClient is the default rest client used for making requests.
DefaultClient Client = newDefaultClient()
)

// Do makes a request to the given endpoint with the given payload and response objects.
// Do makes a request to the given endpoint with the given payload and response type.
// It applies the given options and returns an error if the request fails.
//
// Example:
Expand Down Expand Up @@ -137,9 +137,9 @@ func (e *ErrDecodingResponse) Unwrap() error {
return e.err
}

// client is the default implementation of the Client interface.
// The client is used for making requests to different endpoints.
type client struct {
// restClient is the default implementation of the Client interface.
// It is used for making requests to different endpoints.
type restClient struct {
// baseURL is the base URL for all requests.
baseURL string
// client is the HTTP client used for requests.
Expand Down Expand Up @@ -167,7 +167,7 @@ func NewClient(baseURL string, timeout ...time.Duration) (Client, error) {
tp.MaxIdleConnsPerHost = maxIdleConnsPerHost
tp.IdleConnTimeout = idleConnTimeout

return &client{
return &restClient{
baseURL: baseURL,
client: &http.Client{
Timeout: t,
Expand All @@ -178,19 +178,19 @@ func NewClient(baseURL string, timeout ...time.Duration) (Client, error) {
}

// Client returns the HTTP client the rest client uses.
func (r *client) Client() *http.Client {
func (r *restClient) Client() *http.Client {
return r.client
}

// RateLimiter returns the rate limiter the rest client uses.
func (r *client) RateLimiter() *rate.Limiter {
func (r *restClient) RateLimiter() *rate.Limiter {
return r.limiter
}

// Do makes a request to the given endpoint with the given payload and response objects.
// It applies the given options and returns an error if the request fails.
// If the response cannot be unmarshalled into the response object, it returns an [ErrDecodingResponse].
func (r *client) Do(ctx context.Context, endpoint *Endpoint, payload, response any, opts ...RequestOption) (int, error) {
func (r *restClient) Do(ctx context.Context, endpoint *Endpoint, payload, response any, opts ...RequestOption) (int, error) {
if ctx == nil || endpoint == nil {
return 0, errors.New("context and endpoint must not be nil")
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func (r *client) Do(ctx context.Context, endpoint *Endpoint, payload, response a

// Close closes the rest client and gracefully awaits all pending requests to finish.
// If the context is canceled, it will close the idle connections immediately.
func (r *client) Close(ctx context.Context) {
func (r *restClient) Close(ctx context.Context) {
done := make(chan struct{})
go func() {
defer close(done)
Expand Down
10 changes: 5 additions & 5 deletions rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type response struct {
func TestDefaultClient_Do(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
DefaultClient.(*client).client = http.DefaultClient
DefaultClient.(*restClient).client = http.DefaultClient

httpmock.RegisterResponder(http.MethodGet, "https://example.com/resource", httpmock.NewJsonResponderOrPanic(200, map[string]any{"id": 1, "name": "Resource"}))

Expand Down Expand Up @@ -225,7 +225,7 @@ func TestClient_Do(t *testing.T) { //nolint:gocyclo // Either complexity or dupl

httpmock.Activate()
defer httpmock.DeactivateAndReset()
c := &client{
c := &restClient{
baseURL: "https://example.com",
client: http.DefaultClient,
limiter: rate.NewLimiter(maxRequestRate, maxRequestBurst),
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestClient_Close(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &client{
c := &restClient{
client: &http.Client{
Transport: &mockTransport{},
},
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestClient_Close(t *testing.T) {
}

func TestClient_Client(t *testing.T) {
c := &client{
c := &restClient{
client: http.DefaultClient,
}

Expand All @@ -396,7 +396,7 @@ func TestClient_Client(t *testing.T) {

func TestClient_RateLimiter(t *testing.T) {
limiter := rate.NewLimiter(maxRequestRate, maxRequestBurst)
c := &client{
c := &restClient{
limiter: limiter,
}

Expand Down

0 comments on commit 5002cf2

Please sign in to comment.