Can you please share your thoughts on the following simple httphelper in Go? Thanks in advance. I mainly would like to know whether returning a channel of a custom type seems like a good idea and if so , how could I improve it for only receive chans.
package services
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
type HHResult struct {
Result string
StatusCode int
Error error
}
type IHttpHelper interface {
Request(method string, url string, headers map[string][]string, payload interface{}) chan HHResult
RequestWithOptions(method string, url string, headers map[string][]string, payload interface{}) chan HHResult
}
var singleton IHttpHelper
type HttpHelper struct{}
func (h *HttpHelper) Request(method string, url string, headers map[string][]string, payload interface{}) chan HHResult {
var result chan HHResult = make(chan HHResult)
go func(output chan HHResult) {
payl, err := json.Marshal(payload)
if err != nil {
result <- HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
}
req, err := http.NewRequest(method, url, bytes.NewReader(payl))
client := http.Client{
Timeout: time.Second * 30,
}
resp, err := client.Do(req)
if err != nil {
result <- HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
}
respbytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
result <- HHResult{
Result: string(respbytes),
StatusCode: resp.StatusCode,
Error: nil,
}
}(result)
return result
}
func (h *HttpHelper) RequestWithOptions(method string, url string, headers map[string][]string, payload interface{}, timeout int) chan HHResult {
var result chan HHResult = make(chan HHResult)
go func(output chan<- HHResult) {
payl, err := json.Marshal(payload)
if err != nil {
result <- HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
}
req, err := http.NewRequest(method, url, bytes.NewReader(payl))
client := http.Client{
Timeout: time.Second * timeout,
}
resp, err := client.Do(req)
if err != nil {
result <- HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
}
respbytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
result <- HHResult{
Result: string(respbytes),
StatusCode: resp.StatusCode,
Error: nil,
}
}(result)
return result
}
func NewHttpHelper() IHttpHelper {
if singleton == nil {
singleton = &HttpHelper{}
}
return singleton
}
Example of usage
func main() {
helper := services.NewHttpHelper()
res := <-helper.Request("GET", "https://jsonplaceholder.typicode.com/users/1", nil, nil)
if res.Error != nil {
fmt.Println(res.Error.Error())
return
}
fmt.Println(res.StatusCode)
fmt.Println(res.Result)
}
IHttpHelperandRequestWithOptionsaren't particularly descriptive names \$\endgroup\$