-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.go
More file actions
44 lines (36 loc) · 1.11 KB
/
layer.go
File metadata and controls
44 lines (36 loc) · 1.11 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
package imagespy
import (
"fmt"
"net/http"
)
// Layer is the digest of a layer and a list of images which created the layer.
type Layer struct {
Digest string `json:"digest"`
SourceImages []*Image `json:"source_images"`
}
// LayerService exposes the Layer API of the Image Spy API.
type LayerService struct {
requester *requester
}
// Get retrieves a Layer.
func (ls *LayerService) Get(digest string) (*Layer, error) {
resp, err := ls.requester.readAsJSON("/v1/layers/" + digest)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
layer := &Layer{}
err = ls.requester.parseJSON(resp.Body, layer)
if err != nil {
return nil, err
}
return layer, nil
case http.StatusNotFound:
// TODO: This is only necessary because httpcache only caches when the body is read. Another solution possible?
ls.requester.parseJSON(resp.Body, struct{}{})
return nil, &NotFoundError{message: fmt.Sprintf("Error retrieving Layer: No Layer with digest %s found", digest)}
default:
return nil, fmt.Errorf("Error retrieving Layer: API returned status code %d", resp.StatusCode)
}
}