Welcome! This is Sandy’s take on making a simple yet powerful enough HTTP library. Inspired by python’s requests.
When it comes to making network requests, I find myself writing a lot of repetitive go code, you know the gist of getting a json response,
ctx := context.WithTimeout(context.TODO(), time.Minute)
req, err := http.NewRequestWithContext(ctx, method, URLpath, body)
if err != nil { /* ... */ }
params := req.URL.Query()
params.Add(key, value)
// more great parameters you need
req.URL.RawQuery = params.Encode()
resp, err := client.Do(req)
if err != nil { /* ... */ }
defer resp.Body.Close()
// do a response status check if needed...
ans := &SomeStruct{}
if err != json.NewDecoder(resp.Body).Decode(ans); err != nil { /*... */ }
// whatever other magic you need to do!
This is still a condensed version! The example above skips a couple of steps for brevity, like setting timeouts, deadlines, proper reading, buffering, progress, etc.
You can end up making something of your own, but repeating the same code has many issues, such as forgetting to close the response body or set proper contexts with plenty more.
What if instead, all you had to write was
ans := &SomeStruct{}
err := haruhi.URL(URLpath).
Method(method).
Params(params).
Body(body).
Timeout(time.Minute).
ResponseJSON(ans)
and it did everything for you? You can even simplify it with haruhi’s sensible defaults, such as running a GET
request by default, etc, so you could even do
respStr, err := haruhi.URL(url).Get()
Haruhi of course, supports more funtionality that is aimed to be simple and straight-forward. Entire codebase is documented, so head on to go docs to see what else she can do.
Have fun requesting!
Please visit the github page and let me know through issues or PRs if you have any questions!