forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
73 lines (58 loc) · 1.59 KB
/
site.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
package onedrive
import (
"context"
"net/http"
"net/url"
"time"
)
// SiteService manages the communication with Site related API endpoints.
type SiteService interface {
Search(context.Context, string) (*Sites, error)
RootSite(context.Context) (*Site, error)
}
type siteService struct {
client *OneDrive
}
type Site struct {
ID string `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdDateTime"`
WebURL string `json:"webUrl"`
Extra SiteData `json:"siteCollection"`
}
type SiteData struct {
Hostname string `json:"hostname"`
}
type Sites struct {
Collection []Site `json:"value"`
}
// Search queries for the sites with the provided query
func (ss siteService) Search(ctx context.Context, query string) (*Sites, error) {
q := url.Values{"search": {query}}
req, err := ss.client.newRequest(http.MethodGet, "/sites?"+q.Encode(), nil, nil)
if err != nil {
return nil, err
}
sites := new(Sites)
_, err = ss.client.do(req.WithContext(ctx), sites)
if err != nil {
return nil, err
}
return sites, nil
}
// RootSite returns the tenant root sharepoint site which is the
// parent (toplevel) for all sharepoint sites for a given account
func (ss siteService) RootSite(ctx context.Context) (*Site, error) {
req, err := ss.client.newRequest(http.MethodGet, "/sites/root", nil, nil)
if err != nil {
return nil, err
}
site := new(Site)
_, err = ss.client.do(req.WithContext(ctx), site)
if err != nil {
return nil, err
}
return site, nil
}