Last active
October 3, 2021 16:52
-
-
Save hriddhidey/117deebb508978f50b5bdf0b37561b23 to your computer and use it in GitHub Desktop.
Fetch makes network calls using the method (POST/GET..), the URL // to hit, headers to add (if any), and the body of the request. This function can serve as a singular resource for all your network calls to pass through, thus becoming an http interceptor. You may add a lot of n/w layer related customizations here as you wish - headers, auth, red…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch makes network calls using the method (POST/GET..), the URL // to hit, headers to add (if any), and the body of the request. | |
// Feel free to add more stuff to before/after making the actual n/w call! | |
func Fetch(method string, url string, header map[string]string, body io.Reader) (*http.Response, err) { | |
// Create client with required custom parameters. | |
// Options: Disable keep-alives, 30sec n/w call timeout. | |
client := &http.Client{ | |
Transport: &http.Transport{ | |
DisableKeepAlives: true, | |
}, | |
Timeout: time.Duration(10 * time.Second), | |
} | |
// Create request. | |
req, _ := http.NewRequest(method, url, body) | |
// Add any required headers. | |
for key, value := range header { | |
req.Header.Add(key, value) | |
} | |
// Perform said network call. | |
res, err := client.Do(req) | |
if err != nil { | |
glog.Error(err.Error()) // use glog it's amazing | |
return nil, err | |
} | |
// If response from network call is not 200, return error too. | |
if res.StatusCode != http.StatusOK { | |
return res, errors.New("Network call did not return SUCCESS!") | |
} | |
return res, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment