client

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package client implements the internal HTTP request/response handling

Package client implements the internal HTTP request/response handling

Package client implements the internal HTTP request/response handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Stream added in v0.2.0

func Stream(r *Response, handler func([]byte) error, opts ...StreamOption) error

Stream processes a response stream with the provided handler function. The handler is called for each chunk of data. If the handler returns an error, streaming stops and the error is returned.

func StreamInto added in v0.2.0

func StreamInto(r *Response, handler interface{}, opts ...StreamOption) error

StreamInto processes a response stream as JSON objects and unmarshals each object into a new instance of the provided type, then passes it to the handler function.

func StreamJSON added in v0.2.0

func StreamJSON(r *Response, handler func(json.RawMessage) error, opts ...StreamOption) error

StreamJSON processes a response stream as JSON objects with the provided handler function. The handler is called for each JSON object. If the handler returns an error, streaming stops and the error is returned.

func StreamLines added in v0.2.0

func StreamLines(r *Response, handler func([]byte) error, opts ...StreamOption) error

StreamLines processes a response stream line by line with the provided handler function. The handler is called for each line. If the handler returns an error, streaming stops and the error is returned.

func StreamSSE added in v0.2.0

func StreamSSE(reader io.ReadCloser, handler EventSourceHandler) error

StreamSSE processes a Server-Sent Events stream with the provided handler.

Types

type Event added in v0.2.0

type Event struct {
	ID    string
	Event string
	Data  string
	Retry int
}

Event represents a Server-Sent Event

type EventFullHandlerFunc added in v0.2.0

type EventFullHandlerFunc struct {
	OnEventFunc func(event Event) error
	OnOpenFunc  func() error
	OnCloseFunc func() error
}

EventFullHandlerFunc represents a function-based handler with lifecycle support

func (*EventFullHandlerFunc) OnClose added in v0.2.0

func (l *EventFullHandlerFunc) OnClose() error

OnClose implements EventSourceFullHandler interface

func (*EventFullHandlerFunc) OnEvent added in v0.2.0

func (l *EventFullHandlerFunc) OnEvent(event Event) error

OnEvent implements EventSourceHandler interface

func (*EventFullHandlerFunc) OnOpen added in v0.2.0

func (l *EventFullHandlerFunc) OnOpen() error

OnOpen implements EventSourceFullHandler interface

type EventHandlerFunc added in v0.2.0

type EventHandlerFunc func(event Event) error

EventHandlerFunc is a function type for handling SSE events

func (EventHandlerFunc) OnEvent added in v0.2.0

func (f EventHandlerFunc) OnEvent(event Event) error

OnEvent implements EventSourceHandler interface

type EventSourceFullHandler added in v0.2.0

type EventSourceFullHandler interface {
	EventSourceHandler
	OnOpen() error
	OnClose() error
}

EventSourceFullHandler extends EventSourceHandler with lifecycle methods

type EventSourceHandler added in v0.2.0

type EventSourceHandler interface {
	OnEvent(event Event) error
}

EventSourceHandler handles incoming Server-Sent Events

type HTTPClient added in v0.2.0

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
	GetMiddlewares() []middleware.Middleware
}

HTTPClient defines the interface for the HTTP client

type Request

type Request struct {
	Method  string
	URL     string
	Headers http.Header
	Query   url.Values
	Body    interface{}
	Client  HTTPClient
	// contains filtered or unexported fields
}

Request represents a prepared HTTP request with middleware support

func (*Request) Do

func (r *Request) Do(ctx context.Context) (*Response, error)

Do executes the request and returns the response

func (*Request) Stream added in v0.2.0

func (r *Request) Stream(ctx context.Context, handler func([]byte) error, opts ...StreamOption) error

Stream executes the request and streams the response as raw bytes

func (*Request) StreamInto added in v0.2.0

func (r *Request) StreamInto(ctx context.Context, handler interface{}) error

StreamInto executes the request and unmarshals each JSON object into the specified type

func (*Request) StreamJSON added in v0.2.0

func (r *Request) StreamJSON(ctx context.Context, handler func(json.RawMessage) error) error

StreamJSON executes the request and streams the response as JSON objects

func (*Request) StreamLines added in v0.2.0

func (r *Request) StreamLines(ctx context.Context, handler func([]byte) error, opts ...StreamOption) error

StreamLines executes the request and streams the response line by line

func (*Request) StreamSSE added in v0.2.0

func (r *Request) StreamSSE(ctx context.Context, handler EventSourceHandler) error

StreamSSE executes the request and streams the response as Server-Sent Events

func (*Request) WithBody

func (r *Request) WithBody(body interface{}) *Request

WithBody sets the request body

func (*Request) WithHeader

func (r *Request) WithHeader(key, value string) *Request

WithHeader sets a header for this request

func (*Request) WithHeaders

func (r *Request) WithHeaders(headers map[string]string) *Request

WithHeaders sets multiple headers for this request

func (*Request) WithMiddleware added in v0.2.0

func (r *Request) WithMiddleware(m middleware.Middleware) *Request

WithMiddleware adds middleware specific to this request

func (*Request) WithMiddlewares added in v0.2.0

func (r *Request) WithMiddlewares(middlewares ...middleware.Middleware) *Request

WithMiddlewares adds multiple middlewares specific to this request

func (*Request) WithQuery

func (r *Request) WithQuery(key, value string) *Request

WithQuery adds a query parameter to the request

func (*Request) WithQueryMap

func (r *Request) WithQueryMap(params map[string]string) *Request

WithQueryMap adds multiple query parameters to the request

func (*Request) WithTimeout added in v0.2.0

func (r *Request) WithTimeout(timeout time.Duration) *Request

WithTimeout sets a timeout specific to this request

type Response

type Response struct {
	*http.Response
}

Response wraps the standard http.Response with additional utility methods

func (*Response) Bytes

func (r *Response) Bytes() ([]byte, error)

Bytes reads the entire response body and returns it as a byte slice

func (*Response) Close

func (r *Response) Close() error

Close closes the response body

func (*Response) Consume added in v0.2.0

func (r *Response) Consume() error

Consume reads and discards the response body

func (*Response) IsClientError

func (r *Response) IsClientError() bool

IsClientError returns true if the status code is 4xx

func (*Response) IsError added in v0.2.0

func (r *Response) IsError() bool

IsError returns true if the status code indicates an error (4xx or 5xx)

func (*Response) IsRedirect

func (r *Response) IsRedirect() bool

IsRedirect returns true if the status code is 3xx

func (*Response) IsServerError

func (r *Response) IsServerError() bool

IsServerError returns true if the status code is 5xx

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the status code is between 200 and 299

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON unmarshals the response body into the provided interface

func (*Response) Pipe added in v0.2.0

func (r *Response) Pipe(ch chan<- []byte) error

Pipe allows for piping the response body to the provided channel

func (*Response) Stream added in v0.2.0

func (r *Response) Stream(handler func([]byte) error, opts ...StreamOption) error

Stream processes a response stream with the provided handler function. The handler is called for each chunk of data.

func (*Response) StreamInto added in v0.2.0

func (r *Response) StreamInto(handler interface{}, opts ...StreamOption) error

StreamInto processes a response stream as JSON objects and unmarshals each object into a new instance of the provided type, then passes it to the handler function.

func (*Response) StreamJSON added in v0.2.0

func (r *Response) StreamJSON(handler func(json.RawMessage) error, opts ...StreamOption) error

StreamJSON processes a response stream as JSON objects with the provided handler function.

func (*Response) StreamLines added in v0.2.0

func (r *Response) StreamLines(handler func([]byte) error, opts ...StreamOption) error

StreamLines processes a response stream line by line with the provided handler function.

func (*Response) StreamSSE added in v0.2.0

func (r *Response) StreamSSE(handler EventSourceHandler) error

StreamSSE processes a Server-Sent Events stream with the provided handler function.

func (*Response) String

func (r *Response) String() (string, error)

String reads the entire response body and returns it as a string

func (*Response) WriteTo added in v0.2.0

func (r *Response) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo, streaming the response body to the provided writer

type StreamOption added in v0.2.0

type StreamOption func(*streamOptions)

StreamOption represents options for stream processing

func WithBufferSize added in v0.2.0

func WithBufferSize(size int) StreamOption

WithBufferSize sets the buffer size for stream reading

func WithByteDelimiter added in v0.2.0

func WithByteDelimiter(delimiter byte) StreamOption

WithByteDelimiter sets a byte delimiter for stream reading

func WithContentType added in v0.2.0

func WithContentType(contentType string) StreamOption

WithContentType sets the expected content type for the stream

func WithDelimiter added in v0.2.0

func WithDelimiter(delimiter string) StreamOption

WithDelimiter sets the delimiter for line-based stream reading

Jump to

Keyboard shortcuts

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