mirror of
https://github.com/mattermost/mattermost.git
synced 2026-04-15 22:12:19 -04:00
* 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>
46 lines
900 B
Go
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, "/"))
|
|
}
|