restclient

package module
v0.0.0-...-039c042 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 4 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RestClient

type RestClient struct {
	// contains filtered or unexported fields
}

RestClient is a wrapper for an http client It also contains a host so all requests go to that post

func New

func New(host string) RestClient

New returns a new rest client that uses the Default HTTP Client witha timeout of x seconds and configures the base url according to the host

func NewInsecure

func NewInsecure(host string) RestClient

NewInsecure returns a new rest client that uses the Default HTTP Client witha timeout of x seconds and configures the base url according to the host Uses HTTP instead of HTTPS

func NewLocalhost

func NewLocalhost(port string) (rc RestClient)

NewLocalhost is a conveniance constructor that makes http calls to localhost using a specific port and http instead of https

func NewWithClient

func NewWithClient(host string, client *http.Client) RestClient

NewWithClient wraps the RestClient around an existing http client (example you could set a timeout)

func NewWithClientInsecure

func NewWithClientInsecure(host string, client *http.Client) RestClient

NewWithClientInsecure is the same as NewWithClient but uses http instead of https

func NewWithTimeout

func NewWithTimeout(host string, timeout time.Duration) RestClient

NewWithTimeout creates a RestClient with a specific timeout

func (*RestClient) Get

func (rc *RestClient) Get(path string) (*http.Response, error)

Get is a wrapper function for http.Client Post function It uses the base url and the path to construct the post url TODO: Make sure authentication and tokens work

func (*RestClient) Head

func (rc *RestClient) Head(path string) (*http.Response, error)

Head is a wrapper function for http.Client Head function It uses the base url and the path to construct the post url TODO: Make sure authentication and tokens work

func (*RestClient) Post

func (rc *RestClient) Post(path, contentType string, body io.Reader) (*http.Response, error)

Post is a wrapper function for http.Client Post function It uses the base url and the path to construct the post url TODO: Make sure authentication and tokens work

func (*RestClient) PostForm

func (rc *RestClient) PostForm(path string, data url.Values) (*http.Response, error)

PostForm is a wrapper function for http.Client PostForm function It uses the base url and the path to construct the post url TODO: Make sure authentication and tokens work

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL