-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathservice.go
More file actions
76 lines (64 loc) · 1.75 KB
/
Copy pathservice.go
File metadata and controls
76 lines (64 loc) · 1.75 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package addon
import (
"context"
"fmt"
"net/http"
"github.com/uploadcare/uploadcare-go/v2/internal/codec"
"github.com/uploadcare/uploadcare-go/v2/internal/config"
"github.com/uploadcare/uploadcare-go/v2/internal/svc"
"github.com/uploadcare/uploadcare-go/v2/ucare"
)
// Service describes all addon related API
type Service interface {
// Execute starts an addon execution on a file
Execute(ctx context.Context, addonName string, params ExecuteParams) (ExecuteResult, error)
// Status checks the execution status of an addon request
Status(ctx context.Context, addonName string, requestID string) (StatusResult, error)
}
type service struct {
svc svc.Service
}
// NewService returns new instance of the Service
func NewService(client ucare.Client) Service {
return service{svc.New(config.RESTAPIEndpoint, client, log)}
}
// Execute starts an addon execution on a file
func (s service) Execute(
ctx context.Context,
addonName string,
params ExecuteParams,
) (data ExecuteResult, err error) {
err = s.svc.ResourceOp(
ctx,
http.MethodPost,
fmt.Sprintf("/addons/%s/execute/", addonName),
executeBody(params),
&data,
)
return
}
// Status checks the execution status of an addon request
func (s service) Status(
ctx context.Context,
addonName string,
requestID string,
) (data StatusResult, err error) {
err = s.svc.ResourceOp(
ctx,
http.MethodGet,
fmt.Sprintf("/addons/%s/execute/status/", addonName),
statusQuery{RequestID: requestID},
&data,
)
return
}
type executeBody ExecuteParams
func (p executeBody) EncodeReq(req *http.Request) error {
return codec.EncodeReqBody(p, req)
}
type statusQuery struct {
RequestID string `form:"request_id"`
}
func (p statusQuery) EncodeReq(req *http.Request) error {
return codec.EncodeReqQuery(&p, req)
}