-
Notifications
You must be signed in to change notification settings - Fork 0
/
thenextleg.go
177 lines (159 loc) · 4.25 KB
/
thenextleg.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
package thenextleg
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const host = "https://api.thenextleg.io"
type Client struct {
authToken string
}
func NewClient(authToken string) *Client {
return &Client{authToken: authToken}
}
func (c *Client) Imagine(r ImagineRequest) (ImagineResponse, error) {
var response ImagineResponse
if err := c.postJson("/v2/imagine", r, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) ImagineProgress(messageId string) (ImagineProgress, error) {
var response ImagineProgress
if err := c.get("/v2/message/"+messageId, nil, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) Describe(r DescribeRequest) (DescribeResponse, error) {
var response DescribeResponse
if err := c.postJson("/v2/describe", r, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) DescribeProgress(messageId string) (DescribeProgress, error) {
var response DescribeProgress
if err := c.get("/v2/message/"+messageId, nil, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) Blend(r BlendRequest) (BlendResponse, error) {
var response BlendResponse
if err := c.postJson("/v2/blend", r, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) BlendProgress(messageId string) (BlendProgress, error) {
var response BlendProgress
if err := c.get("/v2/message/"+messageId, nil, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) FaceSwap(r FaceSwapRequest) (io.Reader, error) {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(r); err != nil {
return nil, err
}
res, err := c.post(body, "/face-swap")
if err != nil {
return nil, err
}
return res.Body, nil
}
func (c *Client) GetImage(r GetImageRequest) (io.Reader, error) {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(r); err != nil {
return nil, err
}
res, err := c.post(body, "/getImage")
if err != nil {
return nil, err
}
return res.Body, nil
}
func (c *Client) IsThisNaughty(r IsThisNaughtyRequest) (IsThisNaughtyResponse, error) {
var response IsThisNaughtyResponse
err := c.postJson("/v2/is-this-naughty", r, &response)
if err != nil {
return response, err
}
return response, nil
}
func (c *Client) Info(r ...InfoRequest) (InfoResponse, error) {
var request InfoRequest
if len(r) > 0 {
request = r[0]
}
var response InfoResponse
values := url.Values{}
if request.WebhookOverride != "" {
values.Set("webhookOverride", request.WebhookOverride)
}
if request.Ref != "" {
values.Set("ref", request.Ref)
}
if err := c.get("/v2/info", values, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) InfoProgress(messageId string) (InfoProgress, error) {
var response InfoProgress
if err := c.get("/v2/message/"+messageId, nil, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) get(path string, request url.Values, response interface{}) error {
u := host + path
if len(request) > 0 {
u += "?" + request.Encode()
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.authToken)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP request failed with status code: %d", res.StatusCode)
}
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return err
}
return nil
}
func (c *Client) postJson(path string, request interface{}, response interface{}) error {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(request); err != nil {
return err
}
res, err := c.post(body, path)
if err != nil {
return err
}
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return err
}
return nil
}
func (c *Client) post(body bytes.Buffer, path string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, host+path, &body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.authToken)
return http.DefaultClient.Do(req)
}