mattermost/server/public/model/client4_route.go
Alejandro García Montoro 2b075c9b74
MM-65970: New way to build routes in Client4 (#34499)
* Add Client4 route building functions

* Make DoAPIRequestWithHeaders add the API URL

This makes it consistent with the other DoAPIXYZ functions, which all
prepend the provided URL with the client's API URL.

* Use the new route building logic in Client4

* Address review comments

- clean renamed to cleanSegment
- JoinRoutes and JoinSegments joined in Join
- newClientRoute uses Join

* Fix new routes from merge

* Remove unused import

* Simplify error handling around clientRoute (#34870)

---------

Co-authored-by: Jesse Hallam <jesse@mattermost.com>
Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-01-29 14:26:47 +00:00

46 lines
900 B
Go

package model
import (
"net/url"
"strings"
)
type clientRoute struct {
segments []string
}
func cleanSegment(segment string) string {
s := strings.ReplaceAll(segment, "..", "")
return url.PathEscape(s)
}
func newClientRoute(segment string) clientRoute {
return clientRoute{}.Join(segment)
}
func (r clientRoute) Join(segments ...any) clientRoute {
for _, segment := range segments {
if s, ok := segment.(string); ok {
r.segments = append(r.segments, cleanSegment(s))
}
if s, ok := segment.(clientRoute); ok {
r.segments = append(r.segments, s.segments...)
}
}
return r
}
func (r clientRoute) URL() (*url.URL, error) {
path, err := r.String()
if err != nil {
return nil, err
}
return &url.URL{Path: path}, nil
}
func (r clientRoute) String() (string, error) {
// Make sure that there is a leading slash
return url.JoinPath("/", strings.Join(r.segments, "/"))
}