Browse Source

Maybe a bit better

Piotr Czajkowski 2 years ago
parent
commit
396a5812ee
3 changed files with 13 additions and 16 deletions
  1. 1 8
      fileUpload.go
  2. 1 8
      push.go
  3. 11 0
      pushInterface.go

+ 1 - 8
fileUpload.go

@@ -2,7 +2,6 @@ package bullet
 
 import (
 	"bytes"
-	"encoding/json"
 	"io"
 	"mime/multipart"
 	"net/http"
@@ -25,13 +24,7 @@ func newFileUpload(filename, fileType string) fileUpload {
 }
 
 func (f fileUpload) getReader() (*bytes.Buffer, error) {
-	jsonBytes, err := json.Marshal(f)
-	if err != nil {
-		return nil, err
-	}
-
-	buffer := bytes.NewBuffer(jsonBytes)
-	return buffer, err
+	return getReader(f)
 }
 
 func getFileRequest(filePath string, upload fileUpload) (*http.Request, error) {

+ 1 - 8
push.go

@@ -2,7 +2,6 @@ package bullet
 
 import (
 	"bytes"
-	"encoding/json"
 )
 
 type pushStruct struct {
@@ -13,13 +12,7 @@ type pushStruct struct {
 }
 
 func (p pushStruct) getReader() (*bytes.Buffer, error) {
-	jsonBytes, err := json.Marshal(p)
-	if err != nil {
-		return nil, err
-	}
-
-	buffer := bytes.NewBuffer(jsonBytes)
-	return buffer, err
+	return getReader(p)
 }
 
 func newNotePush(title, text string) pushStruct {

+ 11 - 0
pushInterface.go

@@ -2,8 +2,19 @@ package bullet
 
 import (
 	"bytes"
+	"encoding/json"
 )
 
 type pushInterface interface {
 	getReader() (*bytes.Buffer, error)
 }
+
+func getReader(object interface{}) (*bytes.Buffer, error) {
+	jsonBytes, err := json.Marshal(object)
+	if err != nil {
+		return nil, err
+	}
+
+	buffer := bytes.NewBuffer(jsonBytes)
+	return buffer, nil
+}