forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
167 lines (143 loc) · 4.78 KB
/
drive.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package onedrive
import (
"context"
"fmt"
"net/http"
"net/url"
"regexp"
)
var (
dangerousQueryCharsRegexp = regexp.MustCompile(`\*|<|>|'|\?|\\|\/`)
)
// DriveService manages the communication with Drive related API endpoints.
type DriveService struct {
*OneDrive
}
// The Identity type represents an identity of an actor. For example, and actor
// can be a user, device, or application.
// See: http://onedrive.github.io/resources/identity.htm
type Identity struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
}
// The IdentitySet type is a keyed collection of Identity objects. It is used
// to represent a set of identities associated with various events for an item,
// such as created by or last modified by.
// See: http://onedrive.github.io/resources/identitySet.htm
type IdentitySet struct {
User *Identity `json:"user"`
Application *Identity `json:"application"`
Device *Identity `json:"device"`
}
// The Quota facet groups storage space quota-related information on OneDrive
// into a single structure.
// See: http://onedrive.github.io/facets/quotainfo_facet.htm
type Quota struct {
Total int64 `json:"total"`
Used int64 `json:"used"`
Remaining int64 `json:"remaining"`
Deleted int64 `json:"deleted"`
State string `json:"state"`
}
// Drives represents a collection of Drives
type Drives struct {
Collection []*Drive `json:"value"`
}
// The Drive resource represents a drive in OneDrive. It provides information
// about the owner of the drive, total and available storage space, and exposes
// a collection of all the Items contained within the drive.
// See: http://onedrive.github.io/resources/drive.htm
type Drive struct {
ID string `json:"id"`
DriveType string `json:"driveType"`
Owner *IdentitySet `json:"owner"`
Quota *Quota `json:"quota"`
// Relationships
Items *Items `json:"items"`
Root *Item `json:"root"`
Special *Items `json:"special"`
Shares *Items `json:"shares"`
}
// driveURIFromID returns a valid request URI based on the ID of the drive.
func driveURIFromID(driveID string) string {
switch driveID {
case "", "default":
return "/drive"
default:
return fmt.Sprintf("/drives/%s", driveID)
}
}
// driveChildrenURIFromID returns a valid request URI to fetch the Drive root
// children based on the ID of the Drive.
func driveChildrenURIFromID(driveID string) string {
switch driveID {
case "", "root", "default":
return "/drive/root/children"
default:
return fmt.Sprintf("/drives/%s/root/children", driveID)
}
}
// Get returns a Drive for the authenticated user. If no driveID is provided
// the users default Drive is returned. A user will always have at least one
// Drive available -- the default Drive.
func (ds *DriveService) Get(ctx context.Context, driveID string) (*Drive, *http.Response, error) {
req, err := ds.newRequest("GET", driveURIFromID(driveID), nil, nil)
if err != nil {
return nil, nil, err
}
drive := new(Drive)
resp, err := ds.do(req.WithContext(ctx), drive)
if err != nil {
return nil, resp, err
}
return drive, resp, nil
}
// GetDefault is a convenience function to return the users default Drive
func (ds *DriveService) GetDefault(ctx context.Context) (*Drive, *http.Response, error) {
return ds.Get(ctx, "")
}
// ListAll returns all the Drives available to the authenticated user
func (ds *DriveService) ListAll(ctx context.Context) (*Drives, *http.Response, error) {
req, err := ds.newRequest("GET", "/drives", nil, nil)
if err != nil {
return nil, nil, err
}
drives := new(Drives)
resp, err := ds.do(req.WithContext(ctx), drives)
if err != nil {
return nil, resp, err
}
return drives, resp, nil
}
// ListChildren returns a collection of all the Items under the Drive root. If no
// driveID is specified, the children from the root drive are retrieved.
func (ds *DriveService) ListChildren(ctx context.Context, driveID string, filter string) (*Items, *http.Response, error) {
var filterParam string
if filter != "" {
filterParam = "filter=" + url.QueryEscape(filter)
}
req, err := ds.newRequest("GET", driveChildrenURIFromID(driveID)+"?"+filterParam, nil, nil)
if err != nil {
return nil, nil, err
}
items := new(Items)
resp, err := ds.do(req.WithContext(ctx), items)
if err != nil {
return nil, resp, err
}
return items, resp, nil
}
// Search allow to set the scope of the drive search and the query
// Which will query files for either the toplevel onedrive or a specific sharepoint site.
func (ds *DriveService) Search(ctx context.Context, opt DriveOption) (*Items, *http.Response, error) {
req, err := ds.newRequest("GET", opt.SearchPath(), nil, nil)
if err != nil {
return nil, nil, err
}
items := new(Items)
resp, err := ds.do(req.WithContext(ctx), items)
if err != nil {
return nil, resp, err
}
return items, resp, nil
}