Skip to content

Commit

Permalink
go fmt ./...
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Jun 28, 2024
1 parent 3d36171 commit 04c0d4a
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions uploads.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
package redmine

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)

type uploadResponse struct {
Upload Upload `json:"upload"`
}

type Upload struct {
Token string `json:"token"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
}

func (c *Client) Upload(filename string) (*Upload, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.endpoint+"/uploads.json?key="+c.apikey, bytes.NewBuffer(content))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/octet-stream")
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

decoder := json.NewDecoder(res.Body)
var r uploadResponse
if res.StatusCode != 201 {
var er errorsResult
err = decoder.Decode(&er)
if err == nil {
err = errors.New(strings.Join(er.Errors, "\n"))
}
} else {
err = decoder.Decode(&r)
}
if err != nil {
return nil, err
}
return &r.Upload, nil
}
package redmine

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)

type uploadResponse struct {
Upload Upload `json:"upload"`
}

type Upload struct {
Token string `json:"token"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
}

func (c *Client) Upload(filename string) (*Upload, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.endpoint+"/uploads.json?key="+c.apikey, bytes.NewBuffer(content))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/octet-stream")
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

decoder := json.NewDecoder(res.Body)
var r uploadResponse
if res.StatusCode != 201 {
var er errorsResult
err = decoder.Decode(&er)
if err == nil {
err = errors.New(strings.Join(er.Errors, "\n"))
}
} else {
err = decoder.Decode(&r)
}
if err != nil {
return nil, err
}
return &r.Upload, nil
}

0 comments on commit 04c0d4a

Please sign in to comment.