forked from cloudinary/cloudinary-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudinary.go
92 lines (79 loc) · 2.45 KB
/
cloudinary.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
package cloudinary
import (
"github.com/cloudinary/cloudinary-go/api/admin"
"github.com/cloudinary/cloudinary-go/api/uploader"
"github.com/cloudinary/cloudinary-go/asset"
"github.com/cloudinary/cloudinary-go/config"
"github.com/cloudinary/cloudinary-go/logger"
)
// Cloudinary main struct
type Cloudinary struct {
Config config.Configuration
Admin admin.API
Upload uploader.API
Logger *logger.Logger
}
// New returns a new Cloudinary instance from environment variable.
func New() (*Cloudinary, error) {
c, err := config.New()
if err != nil {
return nil, err
}
return NewFromConfiguration(*c)
}
// NewFromURL returns a new Cloudinary instance from a cloudinary url.
func NewFromURL(cloudinaryURL string) (*Cloudinary, error) {
c, err := config.NewFromURL(cloudinaryURL)
if err != nil {
return nil, err
}
return NewFromConfiguration(*c)
}
// NewFromParams returns a new Cloudinary instance from the provided parameters.
func NewFromParams(cloud string, key string, secret string) (*Cloudinary, error) {
c, err := config.NewFromParams(cloud, key, secret)
if err != nil {
return nil, err
}
return NewFromConfiguration(*c)
}
// NewFromOAuthToken returns a new Cloudinary instance from the provided cloud name and OAuth token.
func NewFromOAuthToken(cloud string, oAuthToken string) (*Cloudinary, error) {
c, err := config.NewFromOAuthToken(cloud, oAuthToken)
if err != nil {
return nil, err
}
return NewFromConfiguration(*c)
}
// NewFromConfiguration returns a new Cloudinary instance from the provided configuration.
func NewFromConfiguration(configuration config.Configuration) (*Cloudinary, error) {
logger := logger.New()
return &Cloudinary{
Config: configuration,
Admin: admin.API{
Config: configuration,
Logger: logger,
},
Upload: uploader.API{
Config: configuration,
Logger: logger,
},
Logger: logger,
}, nil
}
// Image creates a new asset.Image instance.
func (c Cloudinary) Image(publicID string) (*asset.Asset, error) {
return asset.Image(publicID, &c.Config)
}
// Video creates a new asset.Video instance.
func (c Cloudinary) Video(publicID string) (*asset.Asset, error) {
return asset.Video(publicID, &c.Config)
}
// File creates a new asset.File instance.
func (c Cloudinary) File(publicID string) (*asset.Asset, error) {
return asset.File(publicID, &c.Config)
}
// Media creates a new asset.Media instance.
func (c Cloudinary) Media(publicID string) (*asset.Asset, error) {
return asset.Media(publicID, &c.Config)
}