-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
79 lines (72 loc) · 2.39 KB
/
client.go
File metadata and controls
79 lines (72 loc) · 2.39 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
package gobaclient
import (
"fmt"
"github.com/gomuddle/goba"
"github.com/gomuddle/gobaclient/internal/client"
"net/url"
"strings"
)
// GetImage retrieves the image with the given type
// and name from the server at the specified url.
func GetImage(uri url.URL, creds Credentials, typ goba.DatabaseType, name string) (image *goba.Image, err error) {
return image, client.GetJSON(&image, client.Request{
URL: buildPath(uri, "/images/%s/%s", typ, name),
Headers: []client.HeaderFunc{
authHeader(creds),
},
CheckResponse: checkResponse,
})
}
// GetAllImages retrieves all images with the
// given type from the server at the specified url.
func GetAllImages(uri url.URL, creds Credentials, typ goba.DatabaseType) (images []goba.Image, err error) {
return images, client.GetJSON(&images, client.Request{
URL: buildPath(uri, "/images/%s", typ),
Headers: []client.HeaderFunc{
authHeader(creds),
},
CheckResponse: checkResponse,
})
}
// CreateImage sends a request to create an image with
// the given type to the server at the specified url.
func CreateImage(uri url.URL, creds Credentials, typ goba.DatabaseType) (image *goba.Image, err error) {
return image, client.PostJSON(&image, client.Request{
URL: buildPath(uri, "/images/%s", typ),
Headers: []client.HeaderFunc{
authHeader(creds),
},
CheckResponse: checkResponse,
})
}
// ApplyImage sends a request to apply the image with the
// given type and name to the database with the given type
// to the server at the specified url.
func ApplyImage(uri url.URL, creds Credentials, typ goba.DatabaseType, name string) error {
return client.Post(client.Request{
URL: buildPath(uri, "/images/%s/%s", typ, name),
Headers: []client.HeaderFunc{
authHeader(creds),
},
CheckResponse: checkResponse,
})
}
// DeleteImage sends a request to delete the image with the
// given type and name to the server at the specified url.
func DeleteImage(uri url.URL, creds Credentials, typ goba.DatabaseType, name string) error {
return client.Delete(client.Request{
URL: buildPath(uri, "/images/%s/%s", typ, name),
Headers: []client.HeaderFunc{
authHeader(creds),
},
CheckResponse: checkResponse,
})
}
// buildPaths formats the given uri.
func buildPath(uri url.URL, format string, args ...interface{}) string {
if path := uri.Path; !strings.HasSuffix(path, "/") {
path += "/"
}
uri.Path += fmt.Sprintf(format, args...)
return uri.String()
}