Change upload response

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) 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) fileURL := path.Join(s.config.URL, "/api/file", f.ID)
var resp = struct { resp := &ResponseAPIFilePost{
Message string `json:"message"`
ID string `json:"id"`
URL string `json:"url"`
}{
Message: "OK", Message: "OK",
ID: f.ID, Files: []ResponseAPIFilePostFiles{
URL: fileURL, ResponseAPIFilePostFiles{
ID: f.ID,
URL: fileURL,
},
},
} }
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w) 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) { func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
reqID := middleware.GetReqID(r.Context()) reqID := middleware.GetReqID(r.Context())
var responses []ResponseAPIFilePost var resp ResponseAPIFilePost
if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil { if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err) 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) 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) 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) w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w) 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) 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) t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
} }
var expectedResp []struct { var expectedResp api.ResponseAPIFilePost
Message string `json:"message"`
ID string `json:"id"`
URL string `json:"url"`
}
decoder := json.NewDecoder(rr.Result().Body) decoder := json.NewDecoder(rr.Result().Body)
if err := decoder.Decode(&expectedResp); err != nil { if err := decoder.Decode(&expectedResp); err != nil {
t.Fatalf("error decoding response: %s", err) 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) t.Errorf("Response has wrong length. Got %d want %d", l, 1)
} }
uploadID := expectedResp[0].ID uploadID := expectedResp.Files[0].ID
if uploadID == "" { if uploadID == "" {
t.Errorf("Response has empty id") t.Errorf("Response has empty id")
} }

View File

@ -14,9 +14,13 @@ type ResponseAPILogin struct {
Token string `json:"token"` Token string `json:"token"`
} }
type ResponseAPIFilePost struct { type ResponseAPIFilePost struct {
Message string `json:"message"` Message string `json:"message"`
ID string `json:"id"` Files []ResponseAPIFilePostFiles `json:"files"`
URL string `json:"url"` }
type ResponseAPIFilePostFiles struct {
ID string `json:"id"`
URL string `json:"url"`
} }
type ResponseAPIUserList struct { 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 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) url := fmt.Sprintf("%s/api/file", c.BaseURL)
client := &http.Client{} client := &http.Client{}
@ -194,7 +194,7 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon
} }
defer resp.Body.Close() defer resp.Body.Close()
var expectedResp []api.ResponseAPIFilePost var expectedResp *api.ResponseAPIFilePost
decoder := json.NewDecoder(resp.Body) decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&expectedResp); err != nil { if err := decoder.Decode(&expectedResp); err != nil {

View File

@ -120,7 +120,7 @@ func TestClient(t *testing.T) {
t.Fatalf("Error uploading: %s", err) 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 { if err != nil {
t.Fatalf("Error getting uploaded file from store: %s", err) 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) errmsg := fmt.Sprintf("Error uploading file: %s", err)
return cli.Exit(errmsg, 1) 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 return nil
} }