Skip to content

Conversation

@0xEbrahim
Copy link

@0xEbrahim 0xEbrahim commented Oct 16, 2025

Pull Request Template

Adresses: #2434
Description:
This PR refactors the pkg/gofr/service/auth.go file to eliminate duplicated logic across HTTP method implementations (Get, Post, Patch, Put, and Delete).
A new helper function doAuthWithHeaders was introduced to centralize the authentication and header injection process, significantly improving code maintainability and readability.

Key improvements:

Consolidated repetitive code that handled authentication and HTTP header merging into a single utility method doAuthWithHeaders.

Reduced boilerplate across all HTTP methods by reusing the same authentication logic.

Ensured consistent behavior for all request types (GET, POST, PATCH, PUT, DELETE).

Improved testability by isolating authentication header injection logic.

Enhanced readability and maintainability for future extension (e.g., adding HEAD, OPTIONS, or custom verbs).

Breaking Changes (if applicable):
None — this is a purely internal refactor; all public method signatures remain unchanged.
Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

@Umang01-hash
Copy link
Member

Screenshot 2025-10-17 at 9 53 52 AM @0xEbrahim Thanks for making your contribution to GoFr. After running the workflow we got to know your PR has some linter errors, can you please resolve them so that we can reveiw the PR.

@0xEbrahim
Copy link
Author

0xEbrahim commented Oct 17, 2025

Yeah i will fix it >

@0xEbrahim Thanks for making your contribution to GoFr. After running the workflow we got to know your PR has some linter errors, can you please resolve them so that we can reveiw the PR.

@Umang01-hash
Copy link
Member

@0xEbrahim I reviewed the refactoring and foudnd that this refactoring adds unnecessary complexity through the WithHeaderType function type and doAuthWithHeaders abstraction. The original implementation was more readable with its straightforward approach.

Go favors clarity over cleverness - the anonymous functions in methods like GetWithHeaders and DeleteWithHeaders create additional cognitive load without solving a significant problem.

@Umang01-hash
Copy link
Member

@0xEbrahim I was majorly referring to file pkg/gofr/service/new_test.go file that has the following concerns:

  • Excessive code duplication across HTTP method tests
  • Repetitive test server setup and teardown
  • Limited testing of the core createAndSendRequest method
  • Repeated response handling patterns
  • Similar assertion logic duplicated throughout

I feel that auth.go is already simplified, we need to focus on other files like the one above and refactor them.

@0xEbrahim
Copy link
Author

@0xEbrahim I was majorly referring to file pkg/gofr/service/new_test.go file that has the following concerns:

  • Excessive code duplication across HTTP method tests
  • Repetitive test server setup and teardown
  • Limited testing of the core createAndSendRequest method
  • Repeated response handling patterns
  • Similar assertion logic duplicated throughout

I feel that auth.go is already simplified, we need to focus on other files like the one above and refactor them.

Such a great feedback, i will start working on new_test.go, thanks for your concern..
I just saw some repeated code on auth.go so i gave it a trry, the lines that handle error & headers, but the function a..HTTP.(method)WithHeaders added more complexity yeah..
thanks


return a.HTTP.DeleteWithHeaders(ctx, path, body, headers)
return a.doAuthWithHeaders(ctx,
func(ctx context.Context, path string, _ map[string]any, body []byte, headers map[string]string) (*http.Response, error) {

Choose a reason for hiding this comment

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

Hi, I am not a code owner. But I think it is better to leave initial

headers, err := a.auth(ctx, headers)
if err != nil { 
	return nil, err
}
return a.HTTP.DeleteWithHeaders(ctx, path, body, headers)

for better readability. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, You are totally right, I over-refactored it

@Umang01-hash
Copy link
Member

Umang01-hash commented Oct 24, 2025

@0xEbrahim As discussed earlier, closing this PR as of now as auth.go is already simplified.

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