-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
188 lines (176 loc) · 5.52 KB
/
image.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package vanceai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
)
type Client struct {
APIKey string
httpClient *http.Client
BaseURL *url.URL
ProcessWebhook string
}
func NewClient(apiKey, processWebhook string) (*Client, error) {
baseURL, err := url.Parse("https://api-service.vanceai.com/web_api/v1")
if err != nil {
return nil, err
}
return &Client{
APIKey: apiKey,
httpClient: http.DefaultClient,
BaseURL: baseURL,
ProcessWebhook: processWebhook,
}, nil
}
type job interface {
jsonString() (string, error)
}
func (jc *JobConfig) jsonString() (string, error) {
b, err := json.Marshal(jc)
if err != nil {
return "", fmt.Errorf("failed to marshal job config: %w", err)
}
return string(b), nil
}
func (jc *MultipleJob) jsonString() (string, error) {
b, err := json.Marshal(jc)
if err != nil {
return "", fmt.Errorf("failed to marshal job config: %w", err)
}
return string(b), nil
}
func (cli *Client) UploadImage(
ctx context.Context,
reader io.Reader,
name string, //file name
) (Response, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", name)
if err != nil {
return Response{}, fmt.Errorf("failed to create form file: %w", err)
}
_, err = io.Copy(part, reader)
if err != nil {
return Response{}, fmt.Errorf("failed to copy file: %w", err)
}
if err := writer.WriteField("api_token", cli.APIKey); err != nil {
return Response{}, fmt.Errorf("failed to write field api_token: %w", err)
}
if err := writer.Close(); err != nil {
return Response{}, fmt.Errorf("failed to close writer: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cli.BaseURL.String()+"/upload", body)
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resStruct := Response{}
resp, err := cli.doAndUnmarshal(ctx, req, &resStruct)
if err != nil {
return Response{}, fmt.Errorf("failed to do request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return Response{}, fmt.Errorf("failed to upload image: %s", resp.Status)
}
if err := resStruct.Error(); err != nil {
return Response{}, fmt.Errorf("failed to upload image %s: %w", resStruct.MsgString(), err)
}
return resStruct, nil
}
func (cli *Client) ProcessImage(
ctx context.Context,
uid string,
jobConfig job,
) (Response, error) {
jc, err := jobConfig.jsonString()
if err != nil {
return Response{}, fmt.Errorf("failed to marshal job config: %w", err)
}
jsonBytes, err := json.Marshal(ProcessRequest{
APIToken: cli.APIKey,
UID: uid,
Webhook: cli.ProcessWebhook,
JobConfig: jc,
})
if err != nil {
return Response{}, fmt.Errorf("failed to encode json: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cli.BaseURL.String()+"/transform", bytes.NewBuffer(jsonBytes))
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resStruct := Response{}
resp, err := cli.doAndUnmarshal(ctx, req, &resStruct)
if err != nil {
return Response{}, fmt.Errorf("failed to do request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return Response{}, fmt.Errorf("failed to process image: %s", resp.Status)
}
if err := resStruct.Error(); err != nil {
return Response{}, fmt.Errorf("failed to upload image %s: %w", resStruct.MsgString(), err)
}
return resStruct, nil
}
func (cli *Client) GetProgress(
ctx context.Context,
transID string,
) (Response, error) {
jsonBytes, err := json.Marshal(ProgressRequest{
APIToken: cli.APIKey,
TransID: transID,
})
if err != nil {
return Response{}, fmt.Errorf("failed to encode json: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cli.BaseURL.String()+"/progress", bytes.NewBuffer(jsonBytes))
if err != nil {
return Response{}, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resStruct := Response{}
resp, err := cli.doAndUnmarshal(ctx, req, &resStruct)
if err != nil {
return Response{}, fmt.Errorf("failed to do request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return Response{}, fmt.Errorf("failed to get progress status is not ok: %s", resp.Status)
}
if err := resStruct.Error(); err != nil {
return Response{}, fmt.Errorf("failed to get progress error code returned%s: %w", resStruct.MsgString(), err)
}
return resStruct, nil
}
func (cli *Client) Download(ctx context.Context, transID string) (io.ReadCloser, error) {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
if err := writer.WriteField("api_token", cli.APIKey); err != nil {
return nil, fmt.Errorf("failed to write field api_token: %w", err)
}
if err := writer.WriteField("trans_id", transID); err != nil {
return nil, fmt.Errorf("failed to write field trans_id: %w", err)
}
if err := writer.Close(); err != nil {
return nil, fmt.Errorf("failed to close writer: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cli.BaseURL.String()+"/download", payload)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := cli.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to do request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download image: %s", resp.Status)
}
return resp.Body, nil
}