-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
70 lines (58 loc) · 1.4 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
package autoderm_go_client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"strconv"
)
type Client struct {
apiKey string
}
func NewClient(apiKey string) *Client {
return &Client{apiKey: apiKey}
}
func (c *Client) Query(skinImageName string, skinImageReader io.Reader, saveImage bool) (*QueryResponse, error) {
var (
apiURL = "https://autoderm.ai/v1/query" +
"?model=autoderm_v2_2" +
"&language=en" +
"&save_image=" + strconv.FormatBool(saveImage)
requestBody = &bytes.Buffer{}
writer = multipart.NewWriter(requestBody)
)
// Add image to request
{
imageWriter, err := writer.CreateFormFile("file", skinImageName)
if err != nil {
return nil, err
}
_, err = io.Copy(imageWriter, skinImageReader)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
}
request, err := http.NewRequest("POST", apiURL, requestBody)
request.Header.Set("Content-Type", writer.FormDataContentType())
request.Header.Set("Api-Key", c.apiKey)
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", response.StatusCode)
}
result := &QueryResponse{}
err = json.NewDecoder(response.Body).Decode(result)
if err != nil {
return nil, err
}
return result, nil
}