diff --git a/internal/client/client.go b/internal/client/client.go index 831caa7..6f65040 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -49,10 +49,10 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o if idx := strings.Index(path, "?"); idx != -1 { basePath := path[:idx] queryStr := path[idx:] - if !strings.HasSuffix(basePath, ".json") { + if shouldAppendJSONSuffix(basePath) { path = basePath + ".json" + queryStr } - } else if !strings.HasSuffix(path, ".json") { + } else if shouldAppendJSONSuffix(path) { path += ".json" } fullURL := c.BaseURL + path @@ -160,6 +160,19 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o return output.SuccessEnvelope(raw, meta), nil } +func shouldAppendJSONSuffix(path string) bool { + 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 +} + func normalizeAPIPath(baseURL, path string) string { if strings.HasSuffix(strings.TrimRight(baseURL, "/"), "/api") { switch { diff --git a/internal/client/client_test.go b/internal/client/client_test.go index 87fb91d..da7398d 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -25,3 +25,21 @@ func TestNormalizeAPIPathKeepsAPIPrefixForNonAPIBaseURL(t *testing.T) { t.Fatalf("normalizeAPIPath() = %q, want %q", got, want) } } + +func TestShouldAppendJSONSuffixSkipsRawFilePath(t *testing.T) { + if shouldAppendJSONSuffix("/Gitlink/forgeplus/raw/master/README.md") { + t.Fatal("raw file path should not get .json suffix") + } +} + +func TestShouldAppendJSONSuffixKeepsRawRepositoryName(t *testing.T) { + if !shouldAppendJSONSuffix("/users/raw/projects") { + t.Fatal("regular API path should get .json suffix") + } +} + +func TestShouldAppendJSONSuffixSkipsExistingJSONPath(t *testing.T) { + if shouldAppendJSONSuffix("/projects.json") { + t.Fatal("existing .json path should not get another suffix") + } +}