/
api.go
67 lines (55 loc) · 1.78 KB
/
api.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
61
62
63
64
65
66
67
package api
import (
"errors"
"fmt"
"net/http"
"runtime"
"runtime/debug"
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/dghubble/sling"
)
var version = "development"
var UserAgentString = GetUserAgentString()
// Generic OctopusDeploy API Get Function.
func ApiGet(sling *sling.Sling, inputStruct interface{}, path string) (interface{}, error) {
if sling == nil {
return nil, internal.CreateInvalidParameterError(constants.OperationAPIGet, "sling")
}
client := sling.New()
if client == nil {
return nil, internal.CreateClientInitializationError(constants.OperationAPIGet)
}
client = client.Get(path)
if client == nil {
return nil, internal.CreateClientInitializationError(constants.OperationAPIGet)
}
client.Set("User-Agent", UserAgentString)
octopusDeployError := new(core.APIError)
resp, err := client.Receive(inputStruct, &octopusDeployError)
// if err != nil {
// return nil, err
// }
// core.APIErrorChecker doesn't give us useful handling for auth errors; do this specifically
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
return nil, errors.New("unauthorized")
}
apiErrorCheck := core.APIErrorChecker(path, resp, http.StatusOK, err, octopusDeployError)
if apiErrorCheck != nil {
return nil, apiErrorCheck
}
return inputStruct, nil
}
func GetUserAgentString() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, dep := range info.Deps {
if dep.Path == "github.com/OctopusDeploy/go-octopusdeploy/v2" {
if dep.Version != "" {
version = dep.Version
}
}
}
}
return fmt.Sprintf("%s/%s (%s; %s) go/%s", "go-octopusdeploy", version, runtime.GOOS, runtime.GOARCH, runtime.Version())
}