restclient

package module
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 13 Imported by: 0

README

Introduction

The restclient library is an HTTP client designed to make HTTP requests easier.

Getting Started

This library provides several HTTP request solutions, as demonstrated in the examples below.

If you can't find what you're looking for, you can help us implement the solution by sending a pull request or letting us know. We're open to implementing it for you. Take a look at the Contribute Section.

To import this library into your Golang project, use the following command:

go get github.com/JoseAntonioPdosSantos/restclient
The solutions implemented in this library are described in the examples below:

Before making a request, you need to create an httpClient object:

httpClient := restclient.NewRestClient()

Make sure to import the library correctly:

import (
	"github.com/JoseAntonioPdosSantos/restclient"
)
Making a simple HTTP request
response := httpClient.Get().
	Url("https://viacep.com.br/ws/01001000/json/").
	Exec()

body, _ := response.GetBody()

fmt.Printf("data: %v", string(body))

Making a simple HTTP request with parameters

response := httpClient.Get("https://viacep.com.br/ws/${cep}/json/").
	AddParams("cep", "01001000").
	Exec()

body, _ := response.GetBody()

fmt.Printf("data: %v", string(body))

Using unmarshal of client in your request
type YourStruct struct {
	ID                 int          `json:"id"`
	Title              string       `json:"title"`
	Description        string       `json:"description"`
	Object             OtherStruct  `json:"object"`
}

response := httpClient.Get("https://dummyjson.com/products/${product_id}").
        AddParams("product_id", "30").
	Authorization(restclient.NewBasic("your username", "your password")).
	Exec()

yourStruct := &YourStruct{}
err := response.Unmarshal(&yourStruct)	

fmt.Println(yourStruct)
Using basic authentication in your request
response := httpClient.Get("https://your-rest-api-integration-herer").
	Authorization(restclient.NewBasic("your username", "your password")).
	Exec()
Using HTTP Signature authentication with SHA256 in your POST request
httpSignatureAuthorization := restclient.NewHTTPSignatureBuilder().
	Algorithm(restclient.NewSHA256()).
	KeyID("Your_Key_ID").
	SharedSecretKey("Your_Shared_Secret_Key").
	Host("HOST").
	Date(time.Now().UTC().Format(time.RFC1123)).
	RequestTarget(fmt.Sprintf(
		"%s %s", 
		strings.ToLower(string(restclient.Post)), 
		"HOST",
		).
	Digest(body).
	VCMerchantID("Your_Merchant_ID").
	Build()

response := httpClient.Post(url).
	ContentType(restclient.ApplicationJson).
	AddHeader("Digest", authorization.Digest).
	AddHeader("V-C-Merchant-Id", "Your_Merchant_ID").
	AddHeader("Date", authorization.Date).
	AddHeader("Host", "Your_Host").
	AddParams("id", paymentId).
	Authorization(httpSignatureAuthorization).
	Body(body).
	Exec()
	
Using other configurations in your request
response := httpClient.Get("https://your-rest-api-integration-herer").
	ContentType(restclient.ApplicationJson).
	Accept(restclient.ApplicationJson).
	AddHeader("your_key", "your_value").
	AddParams("your_key", "your_value").
	Authorization(restclient.NewBasic("your username", "your password")).
	Interceptor(your_Interceptor_Implemented_Here).
	Body(body).
	Exec()
Using an interceptor in your request

First, you need to implement the http.RoundTripper interface from the net/http package in GoLang, and then pass your implementation as shown below:

response := httpClient.Get("https://your-rest-api-integration-herer").
	Interceptor(your_Interceptor_Implemented_Here).
	Exec()
Using custom timeout configuration in your request
  • With 10 seconds of timeout
response := httpClient.Get("https://your-rest-api-integration-herer").
	Authorization(restclient.NewBasic("your username", "your password")).
	Timeout(10).
	Exec()
  • With 60 seconds of timeout
response := httpClient.
	Get("https://your-rest-api-integration-herer").
	Authorization(restclient.NewBasic("your username", "your password")).
	Timeout(60).
	Exec()
  • With 5 minutes of timeout
response := httpClient.Get("https://your-rest-api-integration-herer").
	Authorization(restclient.NewBasic("your username", "your password")).
	Timeout(5).
	TimeoutDuration(time.Minute).
	Exec()

Documentation

Index

Constants

View Source
const AcceptDescription = "Accept"
View Source
const ContentTypeDescription = "Content-Type"

Variables

This section is empty.

Functions

func NewHTTPSignatureBuilder added in v1.0.4

func NewHTTPSignatureBuilder() *hTTPSignatureBuilder

Types

type Algorithm added in v1.0.12

type Algorithm interface {
	Prefix() string
	Name() string
	Exec(payload []byte) [32]byte
	Sign(doc []byte, secret []byte) []byte
}

func NewSHA256 added in v1.0.12

func NewSHA256() Algorithm

type Authorization

type Authorization interface {
	GetAuthorization() string
	GetHeaderKey() string
}

func NewBasic

func NewBasic(username string, password string) Authorization

func NewBearer

func NewBearer(token string) Authorization

type Basic

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

func (Basic) GetAuthorization

func (b Basic) GetAuthorization() string

func (Basic) GetHeaderKey added in v1.0.4

func (b Basic) GetHeaderKey() string

type Bearer

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

func (Bearer) GetAuthorization

func (b Bearer) GetAuthorization() string

func (Bearer) GetHeaderKey added in v1.0.4

func (b Bearer) GetHeaderKey() string

type ContentType

type ContentType string
const (
	ApplicationJson ContentType = "application/json"
	FormEncoded     ContentType = "application/x-www-form-urlencoded"
)

type HTTPSignature added in v1.0.4

type HTTPSignature struct {
	Signature string
	Digest    string
	Date      string
}

func (HTTPSignature) GetAuthorization added in v1.0.4

func (s HTTPSignature) GetAuthorization() string

func (HTTPSignature) GetHeaderKey added in v1.0.4

func (s HTTPSignature) GetHeaderKey() string

type HttpClient added in v1.0.2

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

func (*HttpClient) Accept added in v1.0.2

func (h *HttpClient) Accept(accept ContentType) HttpIntegration

func (*HttpClient) AddHeader added in v1.0.2

func (h *HttpClient) AddHeader(key string, value string) HttpIntegration

func (*HttpClient) AddParams added in v1.0.2

func (h *HttpClient) AddParams(key string, value string) HttpIntegration

func (*HttpClient) AddQuery added in v1.0.2

func (h *HttpClient) AddQuery(key string, value string) HttpIntegration

func (*HttpClient) Authorization added in v1.0.2

func (h *HttpClient) Authorization(authorization Authorization) HttpIntegration

func (*HttpClient) Body added in v1.0.2

func (h *HttpClient) Body(body []byte) HttpIntegration

func (*HttpClient) BodyJson added in v1.1.1

func (h *HttpClient) BodyJson(body any) HttpIntegration

func (*HttpClient) ContentType added in v1.0.2

func (h *HttpClient) ContentType(contentType ContentType) HttpIntegration

func (*HttpClient) Exec added in v1.0.2

func (h *HttpClient) Exec() HttpClientResponse

func (*HttpClient) Interceptor added in v1.0.2

func (h *HttpClient) Interceptor(interceptor http.RoundTripper) HttpIntegration

func (*HttpClient) Timeout added in v1.1.0

func (h *HttpClient) Timeout(timeout time.Duration) HttpIntegration

func (*HttpClient) TimeoutDuration added in v1.1.0

func (h *HttpClient) TimeoutDuration(timeoutDuration time.Duration) HttpIntegration

func (*HttpClient) WithContext added in v1.1.7

func (h *HttpClient) WithContext(ctx context.Context) HttpIntegration

type HttpClientMethod

type HttpClientMethod interface {
	// Post executes a single HTTP POST transaction, returning
	//
	// url is the url, host, or path of the request
	Post(url ...string) HttpIntegration
	// Put executes a single HTTP POST transaction, returning
	//
	// url is the url, host, or path of the request
	Put(url ...string) HttpIntegration
	// Get executes a single HTTP POST transaction, returning
	//
	// url is the url, host, or path of the request
	Get(url ...string) HttpIntegration
	// Delete executes a single HTTP POST transaction, returning
	//
	// url is the url, host, or path of the request
	Delete(url ...string) HttpIntegration
}

func NewRestClient

func NewRestClient() HttpClientMethod

type HttpClientResponse added in v1.0.1

type HttpClientResponse interface {
	GetBody() ([]byte, error)
	Unmarshal(response any) error
	GetResponse() *http.Response
	GetError() error
}

func NewHttpRestClientResponse added in v1.0.1

func NewHttpRestClientResponse(response *http.Response, err error) HttpClientResponse

type HttpIntegration

type HttpIntegration interface {
	ContentType(contentType ContentType) HttpIntegration
	Accept(accept ContentType) HttpIntegration
	Authorization(authorization Authorization) HttpIntegration
	Timeout(timeout time.Duration) HttpIntegration
	TimeoutDuration(timeoutDuration time.Duration) HttpIntegration
	AddHeader(key string, value string) HttpIntegration
	AddParams(key string, value string) HttpIntegration
	AddQuery(key string, value string) HttpIntegration
	Interceptor(interceptor http.RoundTripper) HttpIntegration
	Body(body []byte) HttpIntegration
	BodyJson(body any) HttpIntegration
	WithContext(ctx context.Context) HttpIntegration
	Exec() HttpClientResponse
}

type HttpMethod

type HttpMethod string
const (
	Post   HttpMethod = "POST"
	Put    HttpMethod = "PUT"
	Get    HttpMethod = "GET"
	Delete HttpMethod = "DELETE"
)

type HttpRestClientResponse added in v1.0.1

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

func (*HttpRestClientResponse) GetBody added in v1.0.1

func (h *HttpRestClientResponse) GetBody() (body []byte, err error)

func (*HttpRestClientResponse) GetError added in v1.0.1

func (h *HttpRestClientResponse) GetError() error

func (*HttpRestClientResponse) GetResponse added in v1.0.1

func (h *HttpRestClientResponse) GetResponse() *http.Response

func (*HttpRestClientResponse) Unmarshal added in v1.1.0

func (h *HttpRestClientResponse) Unmarshal(objectToConvert any) error

type RestClient

type RestClient struct {
}

func (*RestClient) Delete

func (r *RestClient) Delete(url ...string) HttpIntegration

func (*RestClient) Get

func (r *RestClient) Get(url ...string) HttpIntegration

func (*RestClient) Post

func (r *RestClient) Post(url ...string) HttpIntegration

func (*RestClient) Put added in v1.0.4

func (r *RestClient) Put(url ...string) HttpIntegration

type SHA256 added in v1.0.12

type SHA256 struct {
}

func (SHA256) Exec added in v1.0.12

func (s SHA256) Exec(payload []byte) [32]byte

func (SHA256) Name added in v1.0.12

func (s SHA256) Name() string

func (SHA256) Prefix added in v1.0.12

func (s SHA256) Prefix() string

func (SHA256) Sign added in v1.0.19

func (s SHA256) Sign(doc []byte, secret []byte) []byte

Jump to

Keyboard shortcuts

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