-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathapi.go
More file actions
56 lines (46 loc) · 1.48 KB
/
Copy pathapi.go
File metadata and controls
56 lines (46 loc) · 1.48 KB
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
package upload
import (
"context"
"errors"
"fmt"
"github.com/michimani/gotwi"
"github.com/michimani/gotwi/media/upload/types"
)
const (
initializeEndpoint = "https://api.x.com/2/media/upload/initialize"
appendEndpoint = "https://api.x.com/2/media/upload/:mediaID/append"
finalizeEndpoint = "https://api.x.com/2/media/upload/:mediaID/finalize"
)
func Initialize(ctx context.Context, c gotwi.IClient, p *types.InitializeInput) (*types.InitializeOutput, error) {
if p == nil {
return nil, errors.New("InitializeInput is nil")
}
res := &types.InitializeOutput{}
if err := c.CallAPI(ctx, initializeEndpoint, "POST", p, res); err != nil {
return nil, err
}
return res, nil
}
func Append(ctx context.Context, c gotwi.IClient, p *types.AppendInput) (*types.AppendOutput, error) {
if p == nil {
return nil, errors.New("AppendInput is nil")
}
boundary := p.GenerateBoundary()
contentType := fmt.Sprintf("multipart/form-data;charset=UTF-8;boundary=%s", boundary)
res := &types.AppendOutput{}
ctx = context.WithValue(ctx, "Content-Type", contentType)
if err := c.CallAPI(ctx, appendEndpoint, "POST", p, res); err != nil {
return nil, err
}
return res, nil
}
func Finalize(ctx context.Context, c gotwi.IClient, p *types.FinalizeInput) (*types.FinalizeOutput, error) {
if p == nil {
return nil, errors.New("FinalizeInput is nil")
}
res := &types.FinalizeOutput{}
if err := c.CallAPI(ctx, finalizeEndpoint, "POST", p, res); err != nil {
return nil, err
}
return res, nil
}