blob: 33bad290d6299610e7185816e24e182b623fdca7 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package objects
2
3import (
4 "bytes"
5 "encoding/json"
6 "strings"
7)
8
Jon Perrittc19adea2014-04-15 16:56:01 -05009type Object map[string]interface{}
Jon Perritt816d2a02014-03-11 20:49:46 -050010
11type ListOpts struct {
12 Container string
13 Full bool
14 Params map[string]string
15}
16
17type DownloadOpts struct {
18 Container string
19 Name string
20 Headers map[string]string
21 Params map[string]string
22}
23
24type CreateOpts struct {
25 Container string
26 Name string
27 Content *bytes.Buffer
28 Metadata map[string]string
29 Headers map[string]string
30 Params map[string]string
31}
32
33type CopyOpts struct {
34 Container string
35 Name string
36 NewContainer string
37 NewName string
38 Metadata map[string]string
39 Headers map[string]string
40}
41
42type DeleteOpts struct {
43 Container string
44 Name string
45 Params map[string]string
46}
47
48type GetOpts struct {
49 Container string
50 Name string
51 Headers map[string]string
52 Params map[string]string
53}
54
55type UpdateOpts struct {
56 Container string
57 Name string
58 Metadata map[string]string
59 Headers map[string]string
60}
61
62// GetInfo is a function that takes a ListResult (of type *perigee.Response)
63// and returns the objects' information.
64func GetInfo(lr ListResult) ([]Object, error) {
65 var oi []Object
66 err := json.Unmarshal(lr.JsonResult, &oi)
67 return oi, err
68}
69
70// GetNames is a function that takes a ListResult (of type *perigee.Response)
71// and returns the objects' names.
72func GetNames(lr ListResult) []string {
73 jr := string(lr.JsonResult)
74 ons := strings.Split(jr, "\n")
75 ons = ons[:len(ons)-1]
76 return ons
77}
78
79// GetContent is a function that takes a DownloadResult (of type *perigee.Response)
80// and returns the object's content.
81func GetContent(dr DownloadResult) []byte {
82 return dr.JsonResult
83}
84
85// GetMetadata is a function that takes a GetResult (of type *perifee.Response)
86// and returns the custom metadata associated with the object.
87func GetMetadata(gr GetResult) map[string]string {
88 metadata := make(map[string]string)
89 for k, v := range gr.HttpResponse.Header {
90 if strings.HasPrefix(k, "X-Object-Meta-") {
91 key := strings.TrimPrefix(k, "X-Object-Meta-")
92 metadata[key] = v[0]
93 }
94 }
95 return metadata
96}