-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
133 lines (108 loc) · 2.59 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
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
package copicake
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"time"
)
type Client struct {
config *ClientConfig
}
type ClientConfig struct {
ApiKey string
RetryTimeout time.Duration
RetryMaxTries int
}
// New returns a new Copicake client
func New(config *ClientConfig) (*Client, error) {
// if config is nil, set default values
if config == nil {
config = &ClientConfig{
RetryTimeout: 1 * time.Second,
RetryMaxTries: 5,
ApiKey: os.Getenv("COPICAKE_API_KEY"),
}
}
// validate client configs
// if no API key is set, try to use the environment variable
if config.ApiKey == "" {
config.ApiKey = os.Getenv("COPICAKE_API_KEY")
}
// if still no API key, return error
if config.ApiKey == "" {
return nil, errors.New("copicake API key is required, either set it in the config or set the COPICAKE_API_KEY environment variable")
}
// if no retry timeout is set, set default value
if config.RetryTimeout == 0 {
config.RetryTimeout = 1 * time.Second
}
// if no retry max tries is set, set default value
if config.RetryMaxTries == 0 {
config.RetryMaxTries = 5
}
// return client
return &Client{
config: config,
}, nil
}
// NewRenderRequest creates a new render job
func (c Client) NewRenderRequest(request RenderRequest) (*RenderJob, error) {
// body
jsonBody, err := json.Marshal(request)
if err != nil {
return nil, err
}
// call API
resp, err := c.call("POST", "image/create", &jsonBody)
if err != nil {
return nil, err
}
// parse response
renderResp := new(RenderJobCreateResponse)
err = json.Unmarshal(resp, renderResp)
if err != nil {
return nil, err
}
if renderResp.Error != "" {
return nil, errors.New(renderResp.Error)
}
// create new Render job
renderJob := &RenderJob{
client: &c,
ID: renderResp.Data.ID,
}
return renderJob, nil
}
// post sends a POST request to the Copicake API
func (c Client) call(method string, path string, body *[]byte) ([]byte, error) {
// create new client
client := &http.Client{}
// create new request
var reader io.Reader
if body != nil {
reader = bytes.NewReader(*body)
}
req, err := http.NewRequest(method, "https://api.copicake.com/v1/"+path, reader)
req.Header.Add("Authorization", "Bearer "+c.config.ApiKey)
req.Header.Add("Content-Type", "application/json")
// send request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// read response
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// close response
err = resp.Body.Close()
if err != nil {
return nil, err
}
// return response
return respBody, nil
}