Skip to content

Commit

Permalink
generalizing condensedWriteJSON() into jutils.SimpleWriteJSON()
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Oct 3, 2023
1 parent 22d2623 commit 9f350d0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion japicore/fidRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (j JApiCore) DeleteByFidHandler() bunrouter.HandlerFunc {

// message := createJsonResponse("Deletion complete")
message := createJsonResponse("Deletion Not Implemented")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
2 changes: 1 addition & 1 deletion japicore/miscRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (j JApiCore) MethodNotAllowedHandler() bunrouter.HandlerFunc {
func (j JApiCore) VersionHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
message := createJsonResponse("")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
6 changes: 3 additions & 3 deletions japicore/pathRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (j JApiCore) deleteByPathCore(operatingRoot string, delFunc func(num int64)
}

message := createJsonResponse("Deletion complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (j JApiCore) ImportHandler() bunrouter.HandlerFunc {
wg.Wait()

message := createJsonResponse("Import complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (j JApiCore) UploadByPathHandler() bunrouter.HandlerFunc {
}

message := createJsonResponse("Upload complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
Expand Down
18 changes: 0 additions & 18 deletions japicore/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package japicore

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"

Expand Down Expand Up @@ -43,21 +40,6 @@ func processUpload(w http.ResponseWriter, fileIo *file_io_handler.FileIoHandler,
return m.Fid()
}

func condensedWriteJSON(w http.ResponseWriter, respVal interface{}) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(respVal); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("json.NewEncoder.Encode: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
written, err := w.Write(buf.Bytes())
if err != nil {
fmt.Printf("Written bytes: %d\n", written)
fmt.Println(err)
}
}

func readUniquePath(req bunrouter.Request) string {
uniquePath, ok := req.Context().Value(jutils.ReqUniquePath{}).(string)
if !ok {
Expand Down
17 changes: 17 additions & 0 deletions jutils/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package jutils

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

func CloneBytes(reader *bytes.Reader) []byte {
Expand Down Expand Up @@ -39,3 +42,17 @@ func CloneByteSlice(source []byte) ([]byte, []byte, error) {

return firstSlice, SecondSlice, nil
}

func SimpleWriteJSON(w http.ResponseWriter, respVal interface{}) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(respVal); err != nil {
ProcessHttpError("SimpleWriteJSON", err, 500, w)
return
}
w.Header().Set("Content-Type", "application/json")
written, err := w.Write(buf.Bytes())
if err != nil {
fmt.Printf("Written bytes: %d\n", written)
ProcessError("SimpleWriteJSON", err)
}
}

0 comments on commit 9f350d0

Please sign in to comment.