A wrapper for making REST API calls to the appropiate host
I was once making a client for a REST API, and I always had to write the base url of the api (host).
Most people would declare it as a constant and write their methods or functions as:
resp, err := httpClient.Get(baseURL + "somethin/something")
if err != nil {
// ...
}But this could get cumbersome. What if we culd wrap this to remember the host and other configurations?
resp, err := rest.Get(something)
if err != nil {
// ...
}This package is more or less inspired in Retrofit, however, it does not intent to be a full replacement.
It is also similar to The Axios Package, most notably because both Axios and this package allow you to set up a default host endpoint so the requests are less repetitive.
Import and use like this:
import "eacp.dev/restclient"
//...
githubAPI := restclient.New("api.github.com")
// Request a specific license
resp, err := githubAPI.Get("licenses/mit") In general, it is supposed to work like the net/http package.
It contains wrappers for all the public functions in this package, such as http.Get()
or http.Post().
You can make a RestClient and use it to make requests as if it was the net/http package.
//...
githubAPI := restclient.New("api.github.com")
// Request a specific license
resp, err := githubAPI.Get("licenses/mit") I would like to replicate the axios.all function. It was deprecated in axios, but I believe it could have a nice place in Go :)
//...
githubAPI := restclient.New("api.github.com")
// Request multiple licenses
githubAPI.GetAll("license/mit", "licenses/gpl-3.0", "licenses/bsd-3-clause")This functions still needs some though, because I'm yet to decide its return types
The client could be set up to use tokens and other auth methods