github.com/acornpublishing/functional-programming-go@v0.0.0-20220401005601-c3bd3786d5a1/Chapter06/04_onion/src/interfaces/webservice_helpers.go (about)

     1  package interfaces
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/pkg/errors"
     6  	"net/http"
     7  	"fmt"
     8  	"domain"
     9  	. "utils"
    10  )
    11  
    12  var ErrorResponse = []byte("Error")
    13  var sf = fmt.Sprintf
    14  
    15  func getFormat(r *http.Request) (format string) {
    16  	//format = r.URL.Query()["format"][0]
    17  	// Hard code json for now
    18  	format = "json"
    19  	return
    20  }
    21  
    22  func setFormat(format string, data interface{}) ([]byte, error) {
    23  	var apiOutput []byte
    24  	if format == "json" {
    25  		output, err := json.Marshal(data)
    26  		if err != nil {
    27  			return nil, errors.Wrap(err, "unable to marshal data to json")
    28  		}
    29  		apiOutput = output
    30  	} else {
    31  		Error.Printf("invalid data format encountered")
    32  		apiOutput = ErrorResponse
    33  	}
    34  	return apiOutput, nil
    35  }
    36  
    37  func handleSuccess(debugMsg, msg string, req *http.Request, res http.ResponseWriter, err error, success bool) {
    38  	Debug.Printf(debugMsg)
    39  	response := domain.Outcome{}
    40  	response.Success = success
    41  	if err != nil {
    42  		Error.Printf("Failed to %s. %v", msg, err)
    43  	}
    44  	output, err := setFormat(getFormat(req), response)
    45  	if err != nil {
    46  		output = ErrorResponse
    47  		Error.Printf("Failed to setFormat. %v",  err)
    48  	}
    49  	Debug.Printf("string(output): %s", string(output))
    50  	fmt.Fprintln(res, string(output))
    51  }
    52  
    53  
    54  func handleExists(debugMsg, msg string, req *http.Request, res http.ResponseWriter, err error, exists bool) {
    55  	Debug.Printf(debugMsg)
    56  	response := domain.Existence{}
    57  	response.Exists = exists
    58  	if err != nil {
    59  		Error.Printf("Failed to %s. %v", msg, err)
    60  	}
    61  	output, err := setFormat(getFormat(req), response)
    62  	if err != nil {
    63  		output = ErrorResponse
    64  		Error.Printf("Failed to setFormat. %v",  err)
    65  	}
    66  	Debug.Printf("string(output): %s", string(output))
    67  	fmt.Fprintln(res, string(output))
    68  }
    69  
    70  func handleBuckets(debugMsg, msg string, req *http.Request, res http.ResponseWriter, err error, bucketNames []domain.Bucket) {
    71  	Debug.Printf(debugMsg)
    72  	response := domain.Buckets{}
    73  	for _, bucketName := range bucketNames {
    74  		Debug.Printf("bucketName: %s", bucketName)
    75  		response.Buckets = append(response.Buckets, bucketName)
    76  	}
    77  	if err != nil {
    78  		Error.Printf("Failed to %s. %v", msg, err)
    79  	}
    80  	output, err := setFormat(getFormat(req), response)
    81  	if err != nil {
    82  		output = ErrorResponse
    83  		Error.Printf("Failed to setFormat. %v",  err)
    84  		//return
    85  	}
    86  	Debug.Printf("string(output): %s", string(output))
    87  	fmt.Fprintln(res, string(output))
    88  }
    89  
    90  
    91  
    92  
    93  
    94