forked from Gitlink/gitlink-cli
fix: preserve raw file API paths
This commit is contained in:
parent
2f8b987c2c
commit
c28b72d46f
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue