From 0a6e0f0b9462c60111c8ff80ff2781e9c070a959 Mon Sep 17 00:00:00 2001 From: camelliamc <16583354+camelliamc@user.noreply.gitee.com> Date: Sun, 14 Jun 2026 21:19:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(client):=20=E6=8F=90=E5=8F=96=20should?= =?UTF-8?q?AppendJSONSuffix=20=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E7=95=99=20raw=20=E8=B7=AF=E5=BE=84=E7=89=B9=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/client/client.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 7db76fbb..9dc465d9 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -48,14 +48,12 @@ func New() (*Client, error) { func (c *Client) Do(method, path string, body interface{}, query url.Values) (*output.Envelope, error) { // Append .json suffix if not already present (GitLink API convention) // Handle paths that may already contain query strings (e.g., /path?key=val) - if !c.SkipJSONSuffix { + if c.shouldAppendJSONSuffix(path) { if idx := strings.Index(path, "?"); idx != -1 { basePath := path[:idx] queryStr := path[idx:] - if !strings.HasSuffix(basePath, ".json") { - path = basePath + ".json" + queryStr - } - } else if !strings.HasSuffix(path, ".json") { + path = basePath + ".json" + queryStr + } else { path += ".json" } } @@ -225,3 +223,24 @@ func lookupStatusInfo(code int) statusInfo { message: fmt.Sprintf("API 返回错误码 %d", code), } } + +// shouldAppendJSONSuffix reports whether the .json suffix should be appended to path. +// Returns false (skip append) when: +// - c.SkipJSONSuffix is set (explicit opt-out for non-JSON endpoints such as gateway) +// - path already ends with .json +// - path matches the raw content pattern (e.g., /api/:owner/:repo/raw/...) +func (c *Client) shouldAppendJSONSuffix(path string) bool { + if c.SkipJSONSuffix { + return false + } + if strings.HasSuffix(path, ".json") { + return false + } + parts := strings.Split(strings.Trim(path, "/"), "/") + for i, part := range parts { + if part == "raw" && i >= 2 && i+2 < len(parts) { + return false + } + } + return true +}