restclient

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

restclient

Generic REST Client with connection pool

⚡️ Quickstart

package main

import (
	"errors"
	"github.com/arielsrv/golang-toolkit/examples/service"
	"github.com/arielsrv/golang-toolkit/restclient"
	"log"
	"time"
)

type UserResponse struct {
	ID   int64  `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

func main() {
	restPool, err := restclient.
		NewRESTPoolBuilder().
		WithName("users").
		WithTimeout(time.Millisecond * 1000).
		WithMaxConnectionsPerHost(20).
		WithMaxIdleConnectionsPerHost(20).
		Build()

	if err != nil {
		log.Fatalln(err)
	}

	restClient := restclient.
		NewRESTClient(*restPool)

	// Generic get
	usersResponse, err := restclient.
		Read[[]UserResponse]{RESTClient: restClient}.
		Get("https://gorest.co.in/public/v2/users", nil)

	if err != nil {
		var restClientError *restclient.APIError
		switch {
		case errors.As(err, &restClientError):
			log.Println(err.Error())
			log.Println(usersResponse.Status)
		default:
			log.Printf("unexpected error: %s\n", err)
		}
	}

	for _, userResponse := range usersResponse.Data {
		log.Printf("User: ID: %d, Name: %s", userResponse.ID, userResponse.Name)
	}

	// Generic post
	userRequest := &service.UserRequest{
		Name: "John Doe",
	}

	result, err := restclient.
		Write[service.UserRequest, UserResponse]{RESTClient: restClient}.
		Post("https://gorest.co.in/public/v2/users", *userRequest, nil)

	if err != nil {
		var restClientError *restclient.APIBadRequestError
		switch {
		case errors.As(err, &restClientError):
			log.Println(err.Error())
			log.Println(result.Status)
		default:
			log.Printf("unexpected error: %s\n", err)
		}
	}

	log.Printf("User: ID: %d, Name: %s", result.Data.ID, result.Data.Name)
}

Documentation

Index

Constants

View Source
const IdleConnectionTimeout = time.Millisecond * 1000
View Source
const MaxConnectionsPerHost = 50
View Source
const MaxIdleConnections = 50
View Source
const MaxIdleConnectionsPerHost = 100
View Source
const SocketKeepAlive = time.Millisecond * 5000
View Source
const SocketTimeout = time.Millisecond * 5000
View Source
const TLSHandshakeTimeout = time.Second * 1000
View Source
const Timeout = time.Millisecond * 1000

Variables

This section is empty.

Functions

func IsOk

func IsOk(statusCode int) bool

func NetworkError

func NetworkError() error

func NoNetworkError

func NoNetworkError() error

Types

type APIBadRequestError

type APIBadRequestError struct {
	APIError
}

type APIError

type APIError struct {
	StatusCode int
	Message    string
}

func NewAPIError added in v0.1.1

func NewAPIError(statusCode int, message string) *APIError

func (*APIError) Error

func (e *APIError) Error() string

type APINotFoundError

type APINotFoundError struct {
	APIError
}

type APIResponse

type APIResponse[TOutput any] struct {
	Data    TOutput
	Status  int
	Headers *Headers
}

type APISecurityError

type APISecurityError struct {
	APIError
}

type Headers

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

func NewHeaders added in v0.2.1

func NewHeaders() *Headers

func (Headers) Get

func (headers Headers) Get(key string) string

func (Headers) Put

func (headers Headers) Put(key string, value string)

type IClient

type IClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type MockError

type MockError struct {
	Message string
}

func (*MockError) Error

func (e *MockError) Error() string

type MockRequest

type MockRequest struct {
	Method string
	URL    string
}

func (MockRequest) GetHashCode

func (mockRequest MockRequest) GetHashCode() uint64

type MockResponse

type MockResponse[TOutput any] struct {
	Responses map[uint64]Tuple[TOutput]
}

func (MockResponse[TOutput]) AddMockRequest

func (mockResponse MockResponse[TOutput]) AddMockRequest(mockRequest MockRequest, response APIResponse[TOutput], err error) *MockResponse[TOutput]

func (MockResponse[TOutput]) Build

func (mockResponse MockResponse[TOutput]) Build() *RESTClient

func (MockResponse[TOutput]) NewRESTClient

func (mockResponse MockResponse[TOutput]) NewRESTClient() *MockResponse[TOutput]

type RESTClient

type RESTClient struct {
	HTTPClient IClient

	Mock any
	// contains filtered or unexported fields
}

func NewRESTClient

func NewRESTClient(restPool RESTPool) *RESTClient

type RESTCommand

type RESTCommand[TInput any, TOutput any] interface {
	Post(url string, request TInput) (*APIResponse[TOutput], error)
}

type RESTPool

type RESTPool struct {
	Name                      string // mandatory
	MaxConnectionsPerHost     int
	MaxIdleConnections        int
	MaxIdleConnectionsPerHost int
	Timeout                   time.Duration
	IdleConnectionTimeout     time.Duration
	TLSHandshakeTimeout       time.Duration
	SocketTimeout             time.Duration
	SocketKeepAlive           time.Duration
}

type RESTPoolBuilder

type RESTPoolBuilder struct {
	Name                      string
	MaxConnectionsPerHost     int
	MaxIdleConnections        int
	MaxIdleConnectionsPerHost int
	Timeout                   time.Duration
	IdleConnectionTimeout     time.Duration
	TLSHandshakeTimeout       time.Duration
	SocketTimeout             time.Duration
	SocketKeepAlive           time.Duration
}

func NewRESTPoolBuilder

func NewRESTPoolBuilder() *RESTPoolBuilder

func (*RESTPoolBuilder) Build

func (restPoolBuilder *RESTPoolBuilder) Build() (*RESTPool, error)

func (*RESTPoolBuilder) MakeDefault

func (restPoolBuilder *RESTPoolBuilder) MakeDefault() *RESTPoolBuilder

func (*RESTPoolBuilder) WithIdleConnectionTimeout

func (restPoolBuilder *RESTPoolBuilder) WithIdleConnectionTimeout(idleConnectionTimeout time.Duration) *RESTPoolBuilder

func (*RESTPoolBuilder) WithMaxConnectionsPerHost

func (restPoolBuilder *RESTPoolBuilder) WithMaxConnectionsPerHost(maxConnectionsPerHost int) *RESTPoolBuilder

func (*RESTPoolBuilder) WithMaxIdleConnections

func (restPoolBuilder *RESTPoolBuilder) WithMaxIdleConnections(maxIdleConnections int) *RESTPoolBuilder

func (*RESTPoolBuilder) WithMaxIdleConnectionsPerHost

func (restPoolBuilder *RESTPoolBuilder) WithMaxIdleConnectionsPerHost(maxIdleConnectionsPerHost int) *RESTPoolBuilder

func (*RESTPoolBuilder) WithName

func (restPoolBuilder *RESTPoolBuilder) WithName(name string) *RESTPoolBuilder

func (*RESTPoolBuilder) WithSocketKeepAlive

func (restPoolBuilder *RESTPoolBuilder) WithSocketKeepAlive(socketKeepAlive time.Duration) *RESTPoolBuilder

func (*RESTPoolBuilder) WithSocketTimeout

func (restPoolBuilder *RESTPoolBuilder) WithSocketTimeout(socketTimeout time.Duration) *RESTPoolBuilder

func (*RESTPoolBuilder) WithTLSHandshakeTimeout

func (restPoolBuilder *RESTPoolBuilder) WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) *RESTPoolBuilder

func (*RESTPoolBuilder) WithTimeout

func (restPoolBuilder *RESTPoolBuilder) WithTimeout(timeout time.Duration) *RESTPoolBuilder

type RESTQuery

type RESTQuery[TOutput any] interface {
	Get(url string) (*APIResponse[TOutput], error)
}

type Read

type Read[TOutput any] struct {
	*RESTClient
}

func (Read[TOutput]) Get

func (e Read[TOutput]) Get(url string, headers *Headers) (*APIResponse[TOutput], error)

func (Read[TOutput]) GetMock

func (e Read[TOutput]) GetMock(method string, url string, result *APIResponse[TOutput]) (*APIResponse[TOutput], error)

type Tuple

type Tuple[TOutput any] struct {
	Method   string
	Response *APIResponse[TOutput]
	Error    error
}

type Write

type Write[TInput any, TOutput any] struct {
	*RESTClient
}

func (Write[TInput, TOutput]) GetMock

func (e Write[TInput, TOutput]) GetMock(method string, url string, result *APIResponse[TOutput]) (*APIResponse[TOutput], error)

func (Write[TInput, TOutput]) Post

func (e Write[TInput, TOutput]) Post(url string, request TInput, headers *Headers) (*APIResponse[TOutput], error)

Jump to

Keyboard shortcuts

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