Change upload response
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-24 19:15:43 +01:00
parent b559d28a38
commit 46401c1b98
6 changed files with 25 additions and 24 deletions

View File

@ -86,14 +86,14 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
}
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "remote_addr", r.RemoteAddr)
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
var resp = struct {
Message string `json:"message"`
ID string `json:"id"`
URL string `json:"url"`
}{
resp := &ResponseAPIFilePost{
Message: "OK",
ID: f.ID,
URL: fileURL,
Files: []ResponseAPIFilePostFiles{
ResponseAPIFilePostFiles{
ID: f.ID,
URL: fileURL,
},
},
}
w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w)
@ -143,7 +143,7 @@ func (s *HTTPServer) HandlerAPIFileDelete(w http.ResponseWriter, r *http.Request
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
reqID := middleware.GetReqID(r.Context())
var responses []ResponseAPIFilePost
var resp ResponseAPIFilePost
if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err)
@ -167,13 +167,14 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
responses = append(responses, ResponseAPIFilePost{Message: "OK", ID: f.ID, URL: fileURL})
fileResponse := ResponseAPIFilePostFiles{ID: f.ID, URL: fileURL}
resp.Files = append(resp.Files, fileResponse)
}
w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w)
if err := encoder.Encode(&responses); err != nil {
if err := encoder.Encode(&resp); err != nil {
s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
}
}

View File

@ -73,22 +73,18 @@ func TestHandlers(t *testing.T) {
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
}
var expectedResp []struct {
Message string `json:"message"`
ID string `json:"id"`
URL string `json:"url"`
}
var expectedResp api.ResponseAPIFilePost
decoder := json.NewDecoder(rr.Result().Body)
if err := decoder.Decode(&expectedResp); err != nil {
t.Fatalf("error decoding response: %s", err)
}
if l := len(expectedResp); l != 1 {
if l := len(expectedResp.Files); l != 1 {
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
}
uploadID := expectedResp[0].ID
uploadID := expectedResp.Files[0].ID
if uploadID == "" {
t.Errorf("Response has empty id")
}

View File

@ -14,9 +14,13 @@ type ResponseAPILogin struct {
Token string `json:"token"`
}
type ResponseAPIFilePost struct {
Message string `json:"message"`
ID string `json:"id"`
URL string `json:"url"`
Message string `json:"message"`
Files []ResponseAPIFilePostFiles `json:"files"`
}
type ResponseAPIFilePostFiles struct {
ID string `json:"id"`
URL string `json:"url"`
}
type ResponseAPIUserList struct {

View File

@ -159,7 +159,7 @@ func (c *Client) Download(ctx context.Context, id string) (io.ReadCloser, error)
return resp.Body, nil
}
func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.ResponseAPIFilePost, error) {
func (c *Client) Upload(ctx context.Context, files ...*files.File) (*api.ResponseAPIFilePost, error) {
url := fmt.Sprintf("%s/api/file", c.BaseURL)
client := &http.Client{}
@ -194,7 +194,7 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon
}
defer resp.Body.Close()
var expectedResp []api.ResponseAPIFilePost
var expectedResp *api.ResponseAPIFilePost
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&expectedResp); err != nil {

View File

@ -120,7 +120,7 @@ func TestClient(t *testing.T) {
t.Fatalf("Error uploading: %s", err)
}
retrieved, err := srv.Files.Get(resp[0].ID)
retrieved, err := srv.Files.Get(resp.Files[0].ID)
if err != nil {
t.Fatalf("Error getting uploaded file from store: %s", err)
}

View File

@ -34,7 +34,7 @@ func ActionUpload(c *cli.Context) error {
errmsg := fmt.Sprintf("Error uploading file: %s", err)
return cli.Exit(errmsg, 1)
}
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp[0].URL)
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp.Files[0].URL)
}
return nil
}