http

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package http provides HTTP client helpers for the config module.

It includes two complementary layers:

  • a lower-level REST wrapper built around net/http requests and responses
  • a higher-level generic client that serializes request bodies, deserializes responses, and emits trace and log metadata through the logger package

The package is intended for outbound service-to-service calls that should reuse QuicksGo conventions for context propagation, headers, timeouts, and observability.

Main entry points:

  • NewRestClient to create a plain *http.Client with a custom timeout and transport
  • NewIRest to execute explicit HTTP requests through the IRest abstraction
  • NewGenericRest to execute typed REST requests through RequestGeneric

Both the concrete and generic clients are designed to be mockable in tests by depending on their interfaces instead of the concrete implementations.

Package clientHttp is a generated GoMock package.

Package clientHttp is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRestClient

func NewRestClient(timeout time.Duration, tr *http.Transport) *http.Client

NewRestClient creates an http client

Types

type IRest

type IRest interface {
	// SetContext sets a context (`context.Context`) that will be used
	// by HTTP requests. It allows you to control cancellations and timeouts.
	SetContext(ctx context.Context)

	// SetRequest defines a custom request (*http.Request).
	SetRequest(req *http.Request)

	// SetHeaders defines the base HTTP headers that will be included in every
	// request. If called again, the previous values are overwritten.
	SetHeaders(header http.Header)

	// Executes a pre-built HTTP request and decodes the response
	// into the provided object, if applicable.
	Do(object any) (*http.Response, error)

	// Get sends an HTTP GET request to the specified `url`, with the specified
	// content type, and decodes the response into an `object`.
	Get(url, contentType string, object any) (*http.Response, error)

	// Post sends an HTTP POST request to the specified `url`, using `body`
	// as the request body, and decodes the response into `object`.
	Post(url, contentType string, body io.Reader, object any) (*http.Response, error)

	// Put sends a PUT HTTP request to the specified `url`, using `body`
	// as the request body, and decodes the response into `object`.
	Put(url, contentType string, body io.Reader, object any) (*http.Response, error)

	// Patch sends an HTTP PATCH request to the specified `url`, using `body`
	// as the request body, and decodes the response into `object`.
	Patch(url, contentType string, body io.Reader, object any) (*http.Response, error)

	// Option sends an HTTP OPTIONS request to the specified `url`, using `body`
	// as the request body, and decodes the response into `object`.
	Option(url, contentType string, body io.Reader, object any) (*http.Response, error)
}

IRest defines an interface for making HTTP requests in a generic manner.

Implementations of this interface allow you to execute HTTP methods (`GET`, `POST`, `PUT`, `PATCH`) and handle headers, context, and response decoding in a target object.

All functions return the HTTP status code (`int`) and a possible error (`error`) describing transport, encoding, or response failures.

func NewIRest

func NewIRest(timeout time.Duration, tr *http.Transport) IRest

NewIRest creates a new instance of IRest.

The timeOut parameter defines the maximum duration for each request. The tr parameter allows injecting a custom HTTP transport.

type IRestGeneric

type IRestGeneric interface {
	// DisableTrace disables automatic tracing for subsequent requests.
	DisableTrace()

	// PostGeneric executes an HTTP POST request.
	//
	// The input parameter contains the URL or Host/Path combination,
	// request body, headers, and the object where the response will
	// be deserialized.
	PostGeneric(context.Context, RequestGeneric) error

	// GetGeneric executes an HTTP GET request.
	//
	// Query parameters are taken from input.Params, and the response
	// is deserialized into input.Response.
	GetGeneric(ctx context.Context, input RequestGeneric) error

	// PutGeneric executes an HTTP PUT request.
	//
	// Typically used to replace or update existing resources.
	PutGeneric(ctx context.Context, input RequestGeneric) error

	// PatchGeneric executes an HTTP PATCH request.
	//
	// Used for partial updates of a resource.
	PatchGeneric(ctx context.Context, input RequestGeneric) error

	// OptionGeneric executes an HTTP OPTIONS request.
	//
	// Useful for retrieving the supported capabilities or methods
	// of a remote resource.
	OptionGeneric(ctx context.Context, input RequestGeneric) error
}

IRestGeneric defines the contract for executing generic REST requests.

Each method receives a context to support cancellation and timeouts, along with a RequestGeneric instance that contains all required data to build and execute the HTTP request.

The implementation is responsible for serializing the request body, setting headers, propagating context, and deserializing the response into the provided Response object.

func NewGenericRest

func NewGenericRest(timeout time.Duration, tr *http.Transport) IRestGeneric

NewGenericRest creates a new instance of IRestGeneric.

The timeOut parameter defines the maximum duration for each request. The tr parameter allows injecting a custom HTTP transport.

type MockIRest

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

MockIRest is a mock of IRest interface.

func NewMockIRest

func NewMockIRest(ctrl *gomock.Controller) *MockIRest

NewMockIRest creates a new mock instance.

func (*MockIRest) Do

func (m *MockIRest) Do(object any) (*http.Response, error)

Do mocks base method.

func (*MockIRest) EXPECT

func (m *MockIRest) EXPECT() *MockIRestMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockIRest) Get

func (m *MockIRest) Get(url, contentType string, object any) (*http.Response, error)

Get mocks base method.

func (*MockIRest) Option

func (m *MockIRest) Option(url, contentType string, body io.Reader, object any) (*http.Response, error)

Option mocks base method.

func (*MockIRest) Patch

func (m *MockIRest) Patch(url, contentType string, body io.Reader, object any) (*http.Response, error)

Patch mocks base method.

func (*MockIRest) Post

func (m *MockIRest) Post(url, contentType string, body io.Reader, object any) (*http.Response, error)

Post mocks base method.

func (*MockIRest) Put

func (m *MockIRest) Put(url, contentType string, body io.Reader, object any) (*http.Response, error)

Put mocks base method.

func (*MockIRest) SetContext

func (m *MockIRest) SetContext(ctx context.Context)

SetContext mocks base method.

func (*MockIRest) SetHeaders

func (m *MockIRest) SetHeaders(header http.Header)

SetHeaders mocks base method.

func (*MockIRest) SetRequest

func (m *MockIRest) SetRequest(req *http.Request)

SetRequest mocks base method.

type MockIRestGeneric

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

MockIRestGeneric is a mock of IRestGeneric interface.

func NewMockIRestGeneric

func NewMockIRestGeneric(ctrl *gomock.Controller) *MockIRestGeneric

NewMockIRestGeneric creates a new mock instance.

func (*MockIRestGeneric) DisableTrace

func (m *MockIRestGeneric) DisableTrace()

DisableTrace mocks base method.

func (*MockIRestGeneric) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockIRestGeneric) GetGeneric

func (m *MockIRestGeneric) GetGeneric(ctx context.Context, input RequestGeneric) error

GetGeneric mocks base method.

func (*MockIRestGeneric) OptionGeneric

func (m *MockIRestGeneric) OptionGeneric(ctx context.Context, input RequestGeneric) error

OptionGeneric mocks base method.

func (*MockIRestGeneric) PatchGeneric

func (m *MockIRestGeneric) PatchGeneric(ctx context.Context, input RequestGeneric) error

PatchGeneric mocks base method.

func (*MockIRestGeneric) PostGeneric

func (m *MockIRestGeneric) PostGeneric(arg0 context.Context, arg1 RequestGeneric) error

PostGeneric mocks base method.

func (*MockIRestGeneric) PutGeneric

func (m *MockIRestGeneric) PutGeneric(ctx context.Context, input RequestGeneric) error

PutGeneric mocks base method.

type MockIRestGenericMockRecorder

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

MockIRestGenericMockRecorder is the mock recorder for MockIRestGeneric.

func (*MockIRestGenericMockRecorder) DisableTrace

func (mr *MockIRestGenericMockRecorder) DisableTrace() *gomock.Call

DisableTrace indicates an expected call of DisableTrace.

func (*MockIRestGenericMockRecorder) GetGeneric

func (mr *MockIRestGenericMockRecorder) GetGeneric(ctx, input interface{}) *gomock.Call

GetGeneric indicates an expected call of GetGeneric.

func (*MockIRestGenericMockRecorder) OptionGeneric

func (mr *MockIRestGenericMockRecorder) OptionGeneric(ctx, input interface{}) *gomock.Call

OptionGeneric indicates an expected call of OptionGeneric.

func (*MockIRestGenericMockRecorder) PatchGeneric

func (mr *MockIRestGenericMockRecorder) PatchGeneric(ctx, input interface{}) *gomock.Call

PatchGeneric indicates an expected call of PatchGeneric.

func (*MockIRestGenericMockRecorder) PostGeneric

func (mr *MockIRestGenericMockRecorder) PostGeneric(arg0, arg1 interface{}) *gomock.Call

PostGeneric indicates an expected call of PostGeneric.

func (*MockIRestGenericMockRecorder) PutGeneric

func (mr *MockIRestGenericMockRecorder) PutGeneric(ctx, input interface{}) *gomock.Call

PutGeneric indicates an expected call of PutGeneric.

type MockIRestMockRecorder

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

MockIRestMockRecorder is the mock recorder for MockIRest.

func (*MockIRestMockRecorder) Do

func (mr *MockIRestMockRecorder) Do(object interface{}) *gomock.Call

Do indicates an expected call of Do.

func (*MockIRestMockRecorder) Get

func (mr *MockIRestMockRecorder) Get(url, contentType, object interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockIRestMockRecorder) Option

func (mr *MockIRestMockRecorder) Option(url, contentType, body, object interface{}) *gomock.Call

Option indicates an expected call of Option.

func (*MockIRestMockRecorder) Patch

func (mr *MockIRestMockRecorder) Patch(url, contentType, body, object interface{}) *gomock.Call

Patch indicates an expected call of Patch.

func (*MockIRestMockRecorder) Post

func (mr *MockIRestMockRecorder) Post(url, contentType, body, object interface{}) *gomock.Call

Post indicates an expected call of Post.

func (*MockIRestMockRecorder) Put

func (mr *MockIRestMockRecorder) Put(url, contentType, body, object interface{}) *gomock.Call

Put indicates an expected call of Put.

func (*MockIRestMockRecorder) SetContext

func (mr *MockIRestMockRecorder) SetContext(ctx interface{}) *gomock.Call

SetContext indicates an expected call of SetContext.

func (*MockIRestMockRecorder) SetHeaders

func (mr *MockIRestMockRecorder) SetHeaders(header interface{}) *gomock.Call

SetHeaders indicates an expected call of SetHeaders.

func (*MockIRestMockRecorder) SetRequest

func (mr *MockIRestMockRecorder) SetRequest(req interface{}) *gomock.Call

SetRequest indicates an expected call of SetRequest.

type RequestGeneric

type RequestGeneric struct {
	HttpRequest      *http.Request
	System           string
	Process          string
	Header           http.Header
	Url              string
	Host             string
	Path             string
	Params           url.Values
	DisableTraceBody bool
	Request          any
	Response         any
}

RequestGeneric: Generic request for requests using generic REST methods

type Rest

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

func (*Rest) Do

func (sr *Rest) Do(object any) (*http.Response, error)

func (*Rest) Get

func (sr *Rest) Get(url, contentType string, object any) (*http.Response, error)

func (*Rest) Option

func (sr *Rest) Option(url, contentType string, body io.Reader, object any) (*http.Response, error)

func (*Rest) Patch

func (sr *Rest) Patch(url, contentType string, body io.Reader, object any) (*http.Response, error)

func (*Rest) Post

func (sr *Rest) Post(url, contentType string, body io.Reader, object any) (*http.Response, error)

func (*Rest) Put

func (sr *Rest) Put(url, contentType string, body io.Reader, object any) (*http.Response, error)

func (*Rest) SetContext

func (sr *Rest) SetContext(ctx context.Context)

func (*Rest) SetHeaders

func (sr *Rest) SetHeaders(header http.Header)

func (*Rest) SetRequest

func (sr *Rest) SetRequest(req *http.Request)

type RestGeneric

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

RestGeneric implements the IRestGeneric interface using an internal IRest instance to execute HTTP requests and process responses.

func (*RestGeneric) DisableTrace

func (gr *RestGeneric) DisableTrace()

DisableTrace disables request tracing for the current instance.

func (*RestGeneric) GetGeneric

func (gr *RestGeneric) GetGeneric(ctx context.Context, input RequestGeneric) error

GetGeneric executes an HTTP GET request.

If input.Params contains values, they are encoded as a query string and appended to the final URL. The response is deserialized into input.Response.

func (*RestGeneric) OptionGeneric

func (gr *RestGeneric) OptionGeneric(ctx context.Context, input RequestGeneric) error

OptionGeneric executes an HTTP OPTIONS request.

This method can be used to retrieve the capabilities supported by a remote resource. If the response contains a body, it will be deserialized into input.Response.

func (*RestGeneric) PatchGeneric

func (gr *RestGeneric) PatchGeneric(ctx context.Context, input RequestGeneric) error

PatchGeneric executes an HTTP PATCH request with JSON content.

The input.Request is serialized into JSON and the response is deserialized into input.Response.

func (*RestGeneric) PostGeneric

func (gr *RestGeneric) PostGeneric(ctx context.Context, input RequestGeneric) error

PostGeneric executes an HTTP POST request with JSON content.

If input.Url is provided, it is used directly; otherwise, the URL is constructed using input.Host and input.Path.

The input.Request is serialized into JSON and the response is deserialized into input.Response.

func (*RestGeneric) PutGeneric

func (gr *RestGeneric) PutGeneric(ctx context.Context, input RequestGeneric) error

PutGeneric executes an HTTP PUT request with JSON content.

The input.Request is serialized into JSON and the response is deserialized into input.Response.

Jump to

Keyboard shortcuts

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