-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
56 lines (49 loc) · 1.07 KB
/
endpoint.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
package api
import (
"encoding/json"
"io"
"net/http"
)
const (
apiPrefix string = "/v0"
)
// Endpoint is the starting point for all
// publishing activity
type Endpoint struct {
location string
password string
client *http.Client
}
// AvailableVersions returns a list of versions available on the server. The
// version 'latest' is not included in the list.
func (ep *Endpoint) AvailableVersions() ([]string, error) {
loc := ep.location + "/versions"
req, err := http.NewRequest("GET", loc, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(ep.password, "")
resp, err := ep.client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var versions []string
err = json.Unmarshal(data, &versions)
if err != nil {
return nil, err
}
return versions, nil
}
// NewEndpoint starts a publishing session.
func NewEndpoint(authstring string, location string) (*Endpoint, error) {
ep := &Endpoint{
client: &http.Client{},
location: location + apiPrefix,
password: authstring,
}
return ep, nil
}