(url string, content *os.File)
| 865 | return err |
| 866 | } |
| 867 | |
| 868 | func (a *API) postFile(url string, content *os.File) (*Result, error) { |
| 869 | req, err := http.NewRequest(http.MethodPost, url, content) |
| 870 | if err != nil { |
| 871 | return nil, err |
| 872 | } |
| 873 | fi, err := content.Stat() |
| 874 | if err != nil { |
| 875 | return nil, err |
| 876 | } |
| 877 | req.ContentLength = fi.Size() |
| 878 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 879 | req.Body = io.NopCloser(content) |
| 880 | req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(content), nil } |
| 881 | |
| 882 | resp, err := a.Client.Do(req) |
| 883 | if err != nil { |
| 884 | return nil, fmt.Errorf("failed to send post request with firmware: %w, %s", err, resp.Status) |
| 885 | } |
| 886 | defer resp.Body.Close() |
| 887 | |
| 888 | if resp.StatusCode == http.StatusInternalServerError { |
| 889 | return nil, ErrInternalError |
| 890 | } |
| 891 | |
| 892 | if resp.StatusCode != http.StatusOK { |
| 893 | return nil, fmt.Errorf("failed to upload firmware: %s", resp.Status) |
| 894 | } |
| 895 | |
| 896 | r := &Result{} |
| 897 | if err = json.NewDecoder(resp.Body).Decode(r); err != nil { |
| 898 | s, err := io.ReadAll(resp.Body) |
| 899 | if err != nil { |
| 900 | return nil, fmt.Errorf("failed to read response: %w", err) |
| 901 | } |
| 902 | return nil, fmt.Errorf("failed to decode response, body: %s, err: %w", string(s), err) |
| 903 | } |
| 904 | |
| 905 | if !r.Result { |
| 906 | return nil, errors.New(r.Message) |
| 907 | } |
| 908 | |
| 909 | return r, nil |
| 910 | } |
| 911 | |
| 912 | func (a *API) post(url string, content *bytes.Buffer) (*Result, error) { |
no test coverage detected