surf

package module
v1.0.58 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 43 Imported by: 0

README ΒΆ

πŸ€–πŸ‘‹ Surf: makes HTTP fun and easy!

Go Reference Go Report Card Go

Surf is a fun, user-friendly, and lightweight Go library that allows you to interact with HTTP services as if you were chatting with them face-to-face! πŸ˜„ Imagine if you could make HTTP requests by simply asking a server politely, and receiving responses as if you were having a delightful conversation with a friend. That's the essence of surf!

🌟 Features

  1. πŸ’¬ Simple and Expressive: Surf's API is designed to make your code look like a conversation, making it easier to read and understand.
  2. πŸ’Ύ Caching and Streaming: Efficiently cache response bodies and stream data on the fly, like a superhero saving the world from slow internet connections.
  3. πŸ“‰ Limit and Deflate: Limit the amount of data you receive and decompress it on the fly, giving you more control over your HTTP interactions.
  4. 🎩 Flexible: Customize headers, query parameters, timeouts, and more for a truly tailor-made experience.
  5. πŸ” Browser Impersonation: Mimic various browsers such as Chrome, Firefox, and others, with a wide range of possible fingerprints for enhanced privacy and compatibility.

πŸ’» Example

Here's a fun and friendly example of how surf makes HTTP requests look like a conversation:

package main

import (
	"fmt"
	"log"

	"github.com/enetx/surf"
)

func main() {
	resp := surf.NewClient().Get("https://api.example.com/jokes/random").Do() // A simple GET request
	if r.IsErr() { log.Fatal(r.Err()) }

	joke := struct {
		ID     int    `json:"id"`
		Setup  string `json:"setup"`
		Punch  string `json:"punch"`
	}{}

	resp.Ok().Body.JSON(&joke)

	fmt.Println("Joke of the day:")
	fmt.Printf("%s\n%s\n", joke.Setup, joke.Punch)
}

πŸš€ Getting Started

To start making friends with HTTP services, follow these simple steps:

  1. Install the surf package using go get:
go get -u github.com/enetx/surf
  1. Import the package into your project:
import "github.com/enetx/surf"
  1. Start making requests and have fun! πŸ˜„

Give surf a try, and watch your HTTP conversations come to life!

Requires GOEXPERIMENT=rangefunc.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Body ΒΆ added in v1.0.51

type Body struct {
	Reader io.ReadCloser // ReadCloser for accessing the body content.
	// contains filtered or unexported fields
}

Body represents the content and properties of an HTTP response Body.

func (*Body) Bytes ΒΆ added in v1.0.51

func (b *Body) Bytes() g.Bytes

Bytes returns the body's content as a byte slice.

func (*Body) Close ΒΆ added in v1.0.51

func (b *Body) Close() error

Close closes the body and returns any error encountered.

func (*Body) Contains ΒΆ added in v1.0.51

func (b *Body) Contains(pattern any) bool

Contains checks if the body's content contains the provided pattern (byte slice, string, or *regexp.Regexp) and returns a boolean.

func (*Body) Dump ΒΆ added in v1.0.51

func (b *Body) Dump(filename string) error

Dump dumps the body's content to a file with the given filename.

func (*Body) JSON ΒΆ added in v1.0.51

func (b *Body) JSON(data any) error

JSON decodes the body's content as JSON into the provided data structure.

func (*Body) Limit ΒΆ added in v1.0.51

func (b *Body) Limit(limit int64) *Body

Limit sets the body's size limit and returns the modified body.

func (*Body) MD5 ΒΆ added in v1.0.51

func (b *Body) MD5() g.String

MD5 returns the MD5 hash of the body's content as a HString.

func (*Body) SSE ΒΆ added in v1.0.53

func (b *Body) SSE(fn func(event *sse.Event) bool) error

SSE reads the body's content as Server-Sent Events (SSE) and calls the provided function for each event. It expects the function to take an *sse.Event pointer as its argument and return a boolean value. If the function returns false, the SSE reading stops.

func (*Body) Stream ΒΆ added in v1.0.51

func (b *Body) Stream() *bufio.Reader

Stream returns the body's bufio.Reader for streaming the content.

func (*Body) String ΒΆ added in v1.0.51

func (b *Body) String() g.String

String returns the body's content as a g.String.

func (*Body) UTF8 ΒΆ added in v1.0.51

func (b *Body) UTF8() g.String

UTF8 converts the body's content to UTF-8 encoding and returns it as a string.

func (*Body) XML ΒΆ added in v1.0.51

func (b *Body) XML(data any) error

XML decodes the body's content as XML into the provided data structure.

type Client ΒΆ

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

Client struct provides a customizable HTTP client.

func NewClient ΒΆ

func NewClient() *Client

NewClient creates a new Client with default settings.

func (*Client) Builder ΒΆ added in v1.0.50

func (c *Client) Builder() *builder

Builder creates a new client builder instance with default values

func (*Client) CloseIdleConnections ΒΆ

func (c *Client) CloseIdleConnections()

CloseIdleConnections removes all entries from the cached transports. Specifically used when Singleton is enabled for JA3 or Impersonate functionalities.

func (*Client) Delete ΒΆ

func (c *Client) Delete(rawURL string, data ...any) *Request

Delete creates a new DELETE request.

func (*Client) FileUpload ΒΆ

func (c *Client) FileUpload(rawURL, fieldName, filePath string, data ...any) *Request

FileUpload creates a new multipart file upload request.

func (*Client) Get ΒΆ

func (c *Client) Get(rawURL string, data ...any) *Request

Get creates a new GET request.

func (*Client) GetClient ΒΆ

func (c *Client) GetClient() *http.Client

GetClient returns http.Client used by the Client.

func (*Client) GetDialer ΒΆ

func (c *Client) GetDialer() *net.Dialer

GetDialer returns the net.Dialer used by the Client.

func (*Client) GetTLSConfig ΒΆ

func (c *Client) GetTLSConfig() *tls.Config

GetTLSConfig returns the tls.Config used by the Client.

func (*Client) GetTransport ΒΆ

func (c *Client) GetTransport() http.RoundTripper

GetTransport returns the http.transport used by the Client.

func (*Client) Head ΒΆ

func (c *Client) Head(rawURL string) *Request

Head creates a new HEAD request.

func (*Client) Multipart ΒΆ

func (c *Client) Multipart(rawURL string, multipartValues map[string]string) *Request

Multipart creates a new multipart form data request.

func (*Client) Patch ΒΆ

func (c *Client) Patch(rawURL string, data any) *Request

Patch creates a new PATCH request.

func (*Client) Post ΒΆ

func (c *Client) Post(rawURL string, data any) *Request

Post creates a new POST request.

func (*Client) Put ΒΆ

func (c *Client) Put(rawURL string, data any) *Request

Put creates a new PUT request.

func (*Client) Raw ΒΆ

func (c *Client) Raw(raw, scheme string) *Request

Raw creates a new HTTP request using the provided raw data and scheme. The raw parameter should contain the raw HTTP request data as a string. The scheme parameter specifies the scheme (e.g., http, https) for the request.

type Cookies ΒΆ added in v1.0.51

type Cookies []*http.Cookie

Cookies represents a list of HTTP Cookies.

func (*Cookies) Contains ΒΆ added in v1.0.51

func (cs *Cookies) Contains(pattern any) bool

Contains checks if the cookies collection contains a cookie that matches the provided pattern. The pattern parameter can be either a string or a pointer to a regexp.Regexp object. The method returns true if a matching cookie is found and false otherwise.

type Headers ΒΆ added in v1.0.51

type Headers http.Header

Headers represents a collection of HTTP Headers.

func (Headers) Clone ΒΆ added in v1.0.52

func (h Headers) Clone() Headers

Clone returns a copy of Headers or nil if Headers is nil.

func (Headers) Contains ΒΆ added in v1.0.51

func (h Headers) Contains(header string, patterns any) bool

Contains checks if the header contains any of the specified patterns. It accepts a header name and a pattern (or list of patterns) and returns a boolean value indicating whether any of the patterns are found in the header values. The patterns can be a string, a slice of strings, or a slice of *regexp.Regexp.

func (Headers) Get ΒΆ added in v1.0.51

func (h Headers) Get(key string) string

Get returns the first value associated with a specified header key. It wraps the Get method from the textproto.MIMEHeader type.

func (Headers) Values ΒΆ added in v1.0.51

func (h Headers) Values(key string) []string

Values returns the values associated with a specified header key. It wraps the Values method from the textproto.MIMEHeader type.

type Request ΒΆ

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

Request is a struct that holds information about an HTTP request.

func (*Request) AddCookies ΒΆ

func (req *Request) AddCookies(cookies ...*http.Cookie) *Request

AddCookies adds cookies to the request.

func (*Request) AddHeaders ΒΆ

func (req *Request) AddHeaders(headers any) *Request

AddHeaders adds headers to the request, appending to any existing headers with the same name.

func (*Request) Do ΒΆ

func (req *Request) Do() g.Result[*Response]

Do performs the HTTP request and returns a Response object or an error if the request failed.

func (*Request) GetRequest ΒΆ

func (req *Request) GetRequest() *http.Request

GetRequest returns the underlying http.Request of the custom request.

func (*Request) SetHeaders ΒΆ

func (req *Request) SetHeaders(headers any) *Request

SetHeaders sets headers for the request, replacing existing ones with the same name.

func (*Request) WithContext ΒΆ

func (req *Request) WithContext(ctx context.Context) *Request

WithContext associates the provided context with the request.

type Response ΒΆ

type Response struct {
	*Client // Client is the associated client for the response.

	URL *url.URL // URL of the response.

	Body *Body // Response body.

	Headers       Headers       // Response headers.
	UserAgent     string        // User agent string.
	Proto         string        // HTTP protocol version.
	Cookies       Cookies       // Response cookies.
	Time          time.Duration // Total time taken for the response.
	ContentLength int64         // Length of the response content.
	StatusCode    StatusCode    // HTTP status code.
	Attempts      int           // Number of attempts made.
	// contains filtered or unexported fields
}

Response represents a custom response structure.

func (Response) Debug ΒΆ

func (resp Response) Debug() *debug

Debug returns a debug instance associated with a Response.

func (Response) GetCookies ΒΆ

func (resp Response) GetCookies(rawURL string) []*http.Cookie

GetCookies returns the cookies from the response for the given URL.

func (Response) GetResponse ΒΆ

func (resp Response) GetResponse() *http.Response

GetResponse returns the underlying http.Response of the custom response.

func (Response) Location ΒΆ added in v1.0.47

func (resp Response) Location() string

Location returns the location of the response.

func (Response) Referer ΒΆ

func (resp Response) Referer() string

Referer returns the referer of the response.

func (*Response) Release ΒΆ added in v1.0.51

func (resp *Response) Release()

Release releases the Response object back to the response pool.

func (Response) RemoteAddress ΒΆ

func (resp Response) RemoteAddress() net.Addr

RemoteAddress returns the remote address of the response.

func (*Response) SetCookies ΒΆ

func (resp *Response) SetCookies(rawURL string, cookies []*http.Cookie) error

SetCookies sets cookies for the given URL in the response.

func (Response) TLSGrabber ΒΆ

func (resp Response) TLSGrabber() *tlsData

TLSGrabber returns a tlsData struct containing information about the TLS connection if it exists.

type StatusCode ΒΆ added in v1.0.47

type StatusCode int

StatusCode represents an HTTP status code.

func (StatusCode) IsClientError ΒΆ added in v1.0.47

func (s StatusCode) IsClientError() bool

IsClientError returns true if the status code indicates a client error [400, 500].

func (StatusCode) IsInformational ΒΆ added in v1.0.47

func (s StatusCode) IsInformational() bool

IsInformational returns true if the status code is in the informational range [100, 200].

func (StatusCode) IsRedirection ΒΆ added in v1.0.47

func (s StatusCode) IsRedirection() bool

IsRedirection returns true if the status code indicates a redirection [300, 400].

func (StatusCode) IsServerError ΒΆ added in v1.0.47

func (s StatusCode) IsServerError() bool

IsServerError returns true if the status code indicates a server error [500, ∞].

func (StatusCode) IsSuccess ΒΆ added in v1.0.47

func (s StatusCode) IsSuccess() bool

IsSuccess returns true if the status code indicates a successful response [200, 300].

func (StatusCode) Text ΒΆ added in v1.0.47

func (s StatusCode) Text() string

Text returns the textual representation of the status code.

Directories ΒΆ

Path Synopsis
internal
pkg
sse

Jump to

Keyboard shortcuts

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