wso2 / wso2/api-platform

[Platform API] Maintainability & Code Quality

Open
#2,520 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
71
Forks
111
Avg merge
1d 14h
Merged PRs (30d)
110

Description

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Extract the repeated org-claim check into a helper.

The orgID, ok := middleware.GetOrganizationFromRequest(r); if !ok { ... } block with the identical CodeCommonUnauthorized response is duplicated verbatim across every handler in this file (16+ occurrences), and the same pattern repeats in llm_apikey.go, llm_deployment.go, and llm_proxy_apikey.go. Extracting a shared helper reduces copy/paste drift risk (e.g. message text diverging over time) and shrinks each handler.

♻️ Suggested helper
func requireOrgID(w http.ResponseWriter, r *http.Request) (string, bool) {
	orgID, ok := middleware.GetOrganizationFromRequest(r)
	if !ok {
		httputil.WriteJSON(w, http.StatusUnauthorized, utils.NewErrorResponseWithCode(
			utils.CodeCommonUnauthorized, "Organization claim not found in token"))
		return "", false
	}
	return orgID, true
}

Then each handler becomes:

-	orgID, ok := middleware.GetOrganizationFromRequest(r)
-	if !ok {
-		httputil.WriteJSON(w, http.StatusUnauthorized, utils.NewErrorResponseWithCode(
-			utils.CodeCommonUnauthorized, "Organization claim not found in token"))
-		return
-	}
+	orgID, ok := requireOrgID(w, r)
+	if !ok {
+		return
+	}

Also applies to: 161-166, 225-230, 320-325, 350-355, 397-402, 454-459, 494-499, 546-551, 586-591, 616-621, 672-677, 710-715, 767-772, 818-823, 870-875, 900-905, 952-957

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@platform-api/internal/handler/llm.go` around lines 110 - 115, The org-claim
authorization check is duplicated across multiple LLM handlers, causing repeated
Unauthorized response logic and risking drift. Add a shared helper in llm.go,
such as requireOrgID, that wraps middleware.GetOrganizationFromRequest and
returns the org ID or writes the CodeCommonUnauthorized JSON error with the same
message. Update each handler in llm.go and the related copies in llm_apikey.go,
llm_deployment.go, and llm_proxy_apikey.go to call this helper instead of
inlining the block.

Originally posted by @coderabbitai[bot] in https://github.com/wso2/api-platform/pull/2507#discussion_r3535719217

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in platform-api/internal/handler/llm.go and compare the repeated authorization blocks with llm_apikey.go, llm_deployment.go, and llm_proxy_apikey.go. Extract the shared organization-claim check, update the listed handlers, and verify that the existing unauthorized response and message remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.