Skip to content

eacp/restclient-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rest Client Wrappper in Go

A wrapper for making REST API calls to the appropiate host

Context

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.

Usage

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") 

Comming "soon"(ish)

Concurrent multiple requests

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

Authentication and tokens

The client could be set up to use tokens and other auth methods

About

A wrapper for making REST API calls to the appropiate host

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages