-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
60 lines (53 loc) · 1.46 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package runway
import (
"fmt"
"net/http"
"regexp"
"strings"
)
func isValidHostedModelsV1URL(url string) bool {
re := regexp.MustCompile(`^https{0,1}:\/\/.+\.runwayml\.cloud\/v1/{0,1}$`)
return re.Match([]byte(url))
}
func isHostedModelResponseError(response *http.Response) bool {
return !strings.Contains(response.Header.Get("Content-Type"), "application/json") ||
!(response.StatusCode >= 200 && response.StatusCode < 300)
}
func doRequestWithRetry(responseCodesToRetry []int, request *http.Request) (*http.Response, error) {
client := http.Client{}
for {
response, err := client.Do(request)
if err != nil {
return nil, &ErrNetworkError{err}
}
if !intSliceIncludes(responseCodesToRetry, response.StatusCode) {
return response, nil
}
response.Body.Close()
// Requests that are repeated must be cloned, as their body has already been read
request, err = cloneRequest(request)
if err != nil {
return nil, err
}
}
}
func cloneRequest(request *http.Request) (*http.Request, error) {
clone := request.Clone(request.Context())
clonedBody, err := request.GetBody()
if err != nil {
return nil, fmt.Errorf("Error getting request body: %s", err)
}
clone.Body = clonedBody
return clone, nil
}
func intSliceIncludes(haystack []int, needle int) bool {
for i := 0; i < len(haystack); i++ {
if needle == haystack[i] {
return true
}
}
return false
}
func stripTrailingSlashIfExists(input string) string {
return strings.TrimRight(input, "/")
}