BuilderHttpClient

package module
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: MIT Imports: 16 Imported by: 4

README

BuilderHttpClient

BuilderHttpClient is a Go package that provides a simple and flexible HTTP client builder for making HTTP requests. It allows you to easily construct and customize HTTP requests using a fluent interface.

Installation

To use BuilderHttpClient in your Go project, you can simply run the following command:

go get github.com/catnovelapi/BuilderHttpClient

Usage

Import the package in your Go file:

import "github.com/catnovelapi/BuilderHttpClient"
Making GET Requests

To make a GET request, you can use the Get function:

response := BuilderHttpClient.Get(url, options...)

Here, url is the URL of the endpoint you want to send the GET request to, and options is an optional list of request options.

Making POST Requests

To make a POST request, you can use the Post function:

response := BuilderHttpClient.Post(url, options...)

Here, url is the URL of the endpoint you want to send the POST request to, and options is an optional list of request options.

Making PUT Requests

To make a PUT request, you can use the Put function:

response := BuilderHttpClient.Put(url, options...)

Here, url is the URL of the endpoint you want to send the PUT request to, and options is an optional list of request options.

Making DELETE Requests

To make a DELETE request, you can use the Delete function:

response := BuilderHttpClient.Delete(url, options...)

Here, url is the URL of the endpoint you want to send the DELETE request to, and options is an optional list of request options.

Making PATCH Requests

To make a PATCH request, you can use the Patch function:

response := BuilderHttpClient.Patch(url, options...)

Here, url is the URL of the endpoint you want to send the PATCH request to, and options is an optional list of request options.

Request Options

You can customize the request by providing various options. The available options are:

  • Method(method string): Sets the HTTP method for the request.
  • ApiPath(URL string): Sets the URL of the API endpoint.
  • Header(header map[string]interface{}): Sets the headers for the request.
  • Body(dataBody interface{}): Sets the request body data.

Here's an example that demonstrates how to use request options:

options := []BuilderHttpClient.Option{
    BuilderHttpClient.Method("POST"),
    BuilderHttpClient.ApiPath("https://api.example.com/users"),
    BuilderHttpClient.Header(map[string]interface{}{
        "Content-Type": "application/json",
        "Authorization": "Bearer token",
    }),
    BuilderHttpClient.Body(map[string]interface{}{
        "name": "John Doe",
        "email": "john@example.com",
    }),
}

response := BuilderHttpClient.Post(url, options...)
Response Handling

The response from the HTTP request is returned as a ResponseInterfaceBuilder. You can use the following methods to handle the response:

  • Code() int: Returns the HTTP status code of the response.
  • Status() string: Returns the HTTP status message of the response.
  • Json(v interface{}) error: Parses the response body as JSON and stores it in the provided struct or map.
  • Text() string: Returns the response body as a string.
  • Gjson() gjson.Result: Parses the response body as JSON and returns a gjson.Result object.
  • Debug() *ResponseBuilder: Prints debug information about the request and response.
  • Cookies() string: Returns the cookies from the response.
  • Bytes() []byte: Returns the response body as a byte array. Here's an example that demonstrates how to handle the response:
response := BuilderHttpClient.Get(url, options...)

fmt.Println("Status Code:", response.Code())
fmt.Println("Status:", response.Status())
fmt.Println("Response Body:", response.Text())

var data map[string]interface{}
err := response.Json(&data)
if err != nil {
    fmt.Println("Error parsing JSON:", err)
} else {
    fmt.Println("Parsed JSON:", data)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientBuilder

type ClientBuilder struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func BuilderDefault added in v1.2.4

func BuilderDefault(method, url string, options ...Option) (*ClientBuilder, error)

func (*ClientBuilder) NewRequestClient added in v1.4.7

func (b *ClientBuilder) NewRequestClient() (*ResponseBuilder, error)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func Body

func Body(dataBody any) Option
func Cookie(cookie map[string]string) Option
func Header(header map[string]any) Option

func Method

func Method(method string) Option

func Proxy added in v1.2.9

func Proxy(proxy string) Option

func Timeout added in v1.2.9

func Timeout(timeout int) Option

type OptionFunc

type OptionFunc func(*ClientBuilder)

type RequestInterfaceBuilder

type RequestInterfaceBuilder interface {
	NewRequestClient() (*ResponseBuilder, error)
}

type ResponseBuilder

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

func Df

func Df(method, url string, options ...Option) (*ResponseBuilder, error)

func (*ResponseBuilder) Byte added in v1.0.7

func (c *ResponseBuilder) Byte() []byte

func (*ResponseBuilder) Code

func (c *ResponseBuilder) Code() int

func (*ResponseBuilder) Cookie added in v1.0.7

func (c *ResponseBuilder) Cookie() string

func (*ResponseBuilder) Debug

func (c *ResponseBuilder) Debug() *ResponseBuilder

func (*ResponseBuilder) DebugString added in v1.2.1

func (c *ResponseBuilder) DebugString() string

func (*ResponseBuilder) Gjson

func (c *ResponseBuilder) Gjson() gjson.Result

func (*ResponseBuilder) Html added in v1.2.9

func (c *ResponseBuilder) Html() *goquery.Document

func (*ResponseBuilder) HtmlGbk added in v1.3.0

func (c *ResponseBuilder) HtmlGbk() *goquery.Document

func (*ResponseBuilder) Json

func (c *ResponseBuilder) Json(v any) error

func (*ResponseBuilder) Status

func (c *ResponseBuilder) Status() string

func (*ResponseBuilder) Text

func (c *ResponseBuilder) Text() string

func (*ResponseBuilder) TextGbk added in v1.3.0

func (c *ResponseBuilder) TextGbk() string

type ResponseInterfaceBuilder

type ResponseInterfaceBuilder interface {
	Code() int
	Status() string
	Json(v any) error
	Text() string
	TextGbk() string
	Gjson() gjson.Result
	Debug() *ResponseBuilder
	DebugString() string
	Byte() []byte
	Cookie() string
	Html() *goquery.Document
	HtmlGbk() *goquery.Document
}

func Delete

func Delete(url string, options ...Option) ResponseInterfaceBuilder

func Get

func Get(url string, options ...Option) ResponseInterfaceBuilder

func Patch

func Patch(url string, options ...Option) ResponseInterfaceBuilder

func Post

func Post(url string, options ...Option) ResponseInterfaceBuilder

func Put

func Put(url string, options ...Option) ResponseInterfaceBuilder

Jump to

Keyboard shortcuts

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