I am currently using the following library: https://github.com/opensauceryafrica/goaxios
I'm quite new to go but have managed to get the hang of a few things, although I'm struggling a bit with types.
This is my current code:
http := goaxios.GoAxios{
Url: fmt.Sprintf("https://example.com/test", config.APIVersion, config.Guilds[0]),
Method: "PATCH",
Headers: map[string]string{
"Authorization": token,
"User-Agent": config.Properties.UserAgent,
"Content-Type": "application/json",
},
ResponseStruct: FailedResponse{},
Body: map[string]string{
"code": "test",
},
}
start := time.Now()
res := http.RunRest()
elapsed := time.Since(start)
if res.Response.StatusCode == 429 {
json := res.Body.(*RatelimitedResponse)
ratelimit := time.Duration(json.RetryAfter) * time.Second
logger.Warnf("Ratelimited for %vs while trying to make request", ratelimit)
time.Sleep(ratelimit)
if config.Retries > tries {
tries += 1
logger.Infof("Retrying request after %v (Retry #%v)", ratelimit, tries)
doRequest()
return
} else {
logger.Infof("Failed requesting after %v attempts.", config.Retries)
}
}
if res.Response.StatusCode == 400 {
json := res.Body.(*FailedResponse)
logger.Warnf("Failed to to make request (Reason: %v, Time: %v)", json.Message, elapsed)
return
}
if res.Response.StatusCode == 200 {
json := res.Body.(*CodeResponse)
logger.Infof("%v", json)
logger.Infof("Successfully made request (Time: %v)", elapsed)
return
}
