44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package ari
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const resourcesPath = "api-docs/resources.json"
|
|
|
|
func (c *Client) HealthCheck(ctx context.Context) HealthStatus {
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
|
|
status := HealthStatus{ResourcesEndpoint: c.endpoint(resourcesPath)}
|
|
|
|
authResp, err := c.AuthenticatedGET(ctx, resourcesPath)
|
|
if err != nil {
|
|
status.Error = "authenticated ARI request failed: " + err.Error()
|
|
return status
|
|
}
|
|
drainAndClose(authResp)
|
|
if authResp.StatusCode == 200 {
|
|
status.AuthenticatedOK = true
|
|
} else {
|
|
status.Error = statusError("authenticated ARI request", authResp.StatusCode, 200)
|
|
return status
|
|
}
|
|
|
|
unauthResp, err := c.UnauthenticatedGET(ctx, resourcesPath)
|
|
if err != nil {
|
|
status.Error = "unauthenticated ARI request failed: " + err.Error()
|
|
return status
|
|
}
|
|
drainAndClose(unauthResp)
|
|
if unauthResp.StatusCode == 401 {
|
|
status.UnauthenticatedReturns401 = true
|
|
} else {
|
|
status.Error = statusError("unauthenticated ARI request", unauthResp.StatusCode, 401)
|
|
}
|
|
status.ResourcesEndpoint = strings.Replace(status.ResourcesEndpoint, "///", "//", 1)
|
|
return status
|
|
}
|