-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.go
81 lines (68 loc) · 2.16 KB
/
client.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
package gphotos
import (
"errors"
"github.com/gphotosuploader/google-photos-api-client-go/v3/albums"
"github.com/gphotosuploader/google-photos-api-client-go/v3/media_items"
"github.com/gphotosuploader/google-photos-api-client-go/v3/uploader"
"net/http"
)
const (
Version = "v3.0.0"
defaultBaseURL = "https://photoslibrary.googleapis.com/"
defaultUserAgent = "gphotos" + "/" + Version
)
// A Client manages communication with the Google Photos API.
type Client struct {
// Uploader implementation used when uploading files to Google Photos.
Uploader MediaUploader
// Services used for talking to different parts of the Google Photos API.
Albums AlbumsService
MediaItems MediaItemsService
}
// NewClient returns a new Google Photos API client.
// API methods require authentication, provide an [net/http.Client]
// that will perform the authentication for you (such as that provided
// by the [golang.org/x/oauth2] library).
func NewClient(httpClient *http.Client) (*Client, error) {
return NewClientWithBaseURL(httpClient, defaultBaseURL)
}
// NewClientWithBaseURL returns a new Google Photos API client with a custom baseURL.
// See [NewClient] for more details.
func NewClientWithBaseURL(httpClient *http.Client, baseURL string) (*Client, error) {
if httpClient == nil {
return nil, errors.New("client is nil")
}
if baseURL == "" {
return nil, errors.New("baseURL is empty")
}
httpClient = addRetryHandler(httpClient)
// Create the Albums Service using default values.
albumsConfig := albums.Config{
Client: httpClient,
BaseURL: baseURL,
UserAgent: defaultUserAgent,
}
albumsService, err := albums.New(albumsConfig)
if err != nil {
return nil, err
}
// Create the Media Items Service using default values.
mediaItemsConfig := media_items.Config{
Client: httpClient,
BaseURL: baseURL,
UserAgent: defaultUserAgent,
}
mediaItemsService, err := media_items.New(mediaItemsConfig)
if err != nil {
return nil, err
}
simpleUploader, err := uploader.NewSimpleUploader(httpClient)
if err != nil {
return nil, err
}
return &Client{
Uploader: simpleUploader,
Albums: albumsService,
MediaItems: mediaItemsService,
}, nil
}