|
|
@@ -3,6 +3,7 @@ package qiyuesuosdk
|
|
|
import (
|
|
|
"bytes"
|
|
|
"crypto/md5"
|
|
|
+ "encoding/base64"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
@@ -137,6 +138,66 @@ func (c *Client) postMultipart(path string, fields map[string]string, fileField
|
|
|
return c.doJSON(req, out)
|
|
|
}
|
|
|
|
|
|
+func (c *Client) postForImageBytes(path string, body any) ([]byte, string, error) {
|
|
|
+ payload, err := json.Marshal(body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ req, err := http.NewRequest(http.MethodPost, c.baseURL()+path, bytes.NewReader(payload))
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
+ req.Header.Set("Accept", "application/json,image/*,*/*")
|
|
|
+ for k, v := range c.authHeaders() {
|
|
|
+ req.Header.Set(k, v)
|
|
|
+ }
|
|
|
+ resp, err := c.http.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+ data, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ contentType := strings.TrimSpace(strings.Split(resp.Header.Get("Content-Type"), ";")[0])
|
|
|
+ if len(data) > 0 && data[0] == '{' {
|
|
|
+ var wrapper struct {
|
|
|
+ apiResponse
|
|
|
+ Result json.RawMessage `json:"result"`
|
|
|
+ }
|
|
|
+ if err = json.Unmarshal(data, &wrapper); err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ if err = wrapper.err(); err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ if len(wrapper.Result) == 0 || string(wrapper.Result) == "null" {
|
|
|
+ return nil, "", ErrSealImageUnavailable
|
|
|
+ }
|
|
|
+ var resultStr string
|
|
|
+ if err = json.Unmarshal(wrapper.Result, &resultStr); err == nil && resultStr != "" {
|
|
|
+ img, decErr := base64.StdEncoding.DecodeString(resultStr)
|
|
|
+ if decErr != nil {
|
|
|
+ return nil, "", decErr
|
|
|
+ }
|
|
|
+ if contentType == "" {
|
|
|
+ contentType = "image/png"
|
|
|
+ }
|
|
|
+ return img, contentType, nil
|
|
|
+ }
|
|
|
+ return nil, "", ErrSealImageUnavailable
|
|
|
+ }
|
|
|
+ if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
|
|
+ return nil, "", fmt.Errorf("qiyuesuo: http %d", resp.StatusCode)
|
|
|
+ }
|
|
|
+ if contentType == "" {
|
|
|
+ contentType = "image/png"
|
|
|
+ }
|
|
|
+ return data, contentType, nil
|
|
|
+}
|
|
|
+
|
|
|
func (c *Client) download(path string, query url.Values) ([]byte, error) {
|
|
|
u := c.baseURL() + path
|
|
|
if len(query) > 0 {
|