httpc

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 6 Imported by: 0

README

Httpc

Mentioned in Awesome Go Go Report Card Go Reference Release GitHub

A customizable and simple HTTP client library. Only depend on the stdlib HTTP client.

Requirements

  • Go 1.14 or higher.

Features

  • Simple and easy to use
  • Make HTTP calls customizable

Installing

go mod:

go get github.com/valord577/httpc

Example

- Do HTTP calls
package main

import (
    "fmt"
    "net/http"
    
    "github.com/valord577/httpc"
)

func main() {
    c := httpc.PackedReq{
        URL:              "https://www.google.com",
        Method:           http.MethodGet,
        ReqBodyPublisher: httpc.PublisherNoBody{},
        RespBodyHandler:  httpc.RespBodyAsByteArray{},
    }

    bs, err := c.Send()
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", bs)
}
- Customize the processing of response body
package main

import (
    "fmt"
    "io"
    "net/http"
    
    "github.com/valord577/httpc"
)

type RespBodyAsString struct {}

func (r RespBodyAsString) Apply(body io.ReadCloser) (interface{}, error) {
    bs, err := io.ReadAll(body)
    if err != nil {
        return nil, err
    }
    return string(bs), nil
}

func main() {
    c := httpc.PackedReq{
        URL:              "https://www.google.com",
        Method:           http.MethodGet,
        ReqBodyPublisher: httpc.PublisherNoBody{},
        RespBodyHandler:  RespBodyAsString{},
    }

    bs, err := c.Send()
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", bs)
}

Changes

See the CHANGES for changes.

License

See the LICENSE for Rights and Limitations (MIT).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RawTypesMap = map[RawType]string{
	RawTypeText: "text/plain; charset=utf-8",
	RawTypeHtml: "text/html; charset=utf-8",
	RawTypeJson: "application/json; charset=utf-8",
	RawTypeXml:  "application/xml; charset=utf-8",

	RawTypeUrlEncodedForm: "application/x-www-form-urlencoded; charset=utf-8",
}

RawTypesMap HTTP content-type map

Functions

func SetGlobalHttpClient

func SetGlobalHttpClient(client *http.Client)

SetGlobalHttpClient set default http client

Types

type PackedReq

type PackedReq struct {

	// if nil, use global.defaultHttpClient
	Client *http.Client

	Ctx context.Context

	URL string

	Method string

	Header map[string]string

	// if nil, use PublisherNoBody
	ReqBodyPublisher ReqBodyPublisher

	// if nil, use RespBodyNoHandle
	RespBodyHandler RespBodyHandler
}

PackedReq is a struct used to define the HTTP request

func (PackedReq) Send

func (r PackedReq) Send() (interface{}, error)

Send the HTTP request

type PublisherNoBody

type PublisherNoBody struct{}

PublisherNoBody is the implement of ReqBodyPublisher

-> No request body

func (PublisherNoBody) Subscribe

func (e PublisherNoBody) Subscribe() ReqBody

Subscribe the empty request body

type PublisherRawBytesBody

type PublisherRawBytesBody struct {
	Body []byte
	Type RawType
}

PublisherRawBytesBody is the implement of ReqBodyPublisher

-> Byte array request body

func (PublisherRawBytesBody) Subscribe

func (raw PublisherRawBytesBody) Subscribe() ReqBody

Subscribe the byte array request body

type PublisherRawStringBody

type PublisherRawStringBody struct {
	Body string
	Type RawType
}

PublisherRawStringBody is the implement of ReqBodyPublisher

-> String request body

func (PublisherRawStringBody) Subscribe

func (raw PublisherRawStringBody) Subscribe() ReqBody

Subscribe the string request body

type RawType

type RawType int

RawType HTTP content-type enum

const (
	// RawTypeText "text/plain; charset=utf-8"
	RawTypeText RawType = iota
	// RawTypeHtml "text/html; charset=utf-8"
	RawTypeHtml
	// RawTypeJson "application/json; charset=utf-8"
	RawTypeJson
	// RawTypeXml "application/xml; charset=utf-8"
	RawTypeXml

	// RawTypeUrlEncodedForm "application/x-www-form-urlencoded; charset=utf-8"
	RawTypeUrlEncodedForm
)

type ReqBody

type ReqBody struct {

	// Request body
	Content io.Reader

	// Content-Length
	Length int64

	// Content-type
	Type string
}

ReqBody need by stdlib HTTP client

type ReqBodyPublisher

type ReqBodyPublisher interface {
	Subscribe() ReqBody
}

ReqBodyPublisher processing request body

type RespBodyAsByteArray

type RespBodyAsByteArray struct{}

RespBodyAsByteArray is the implement of RespBodyHandler

-> Read body as byte array

func (RespBodyAsByteArray) Apply

func (r RespBodyAsByteArray) Apply(body io.ReadCloser) (interface{}, error)

Apply the handle

type RespBodyHandler

type RespBodyHandler interface {
	// Apply the handle of response body
	Apply(body io.ReadCloser) (interface{}, error)
}

RespBodyHandler processing response body

type RespBodyNoHandle

type RespBodyNoHandle struct{}

RespBodyNoHandle is the implement of RespBodyHandler

-> No read body

func (RespBodyNoHandle) Apply

func (r RespBodyNoHandle) Apply(_ io.ReadCloser) (interface{}, error)

Apply the handle

Jump to

Keyboard shortcuts

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