httpc

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: MIT Imports: 10 Imported by: 1

README

httpc

Utility module for abstraction and productivity-improvement on golang "net/http"

Install

Use go get command in your go-project.

$ go get github.com/unvurn/httpc

Usage

package main

import (
	"context"
	"fmt"
	
	"github.com/unvurn/httpc"
)

func main() {
	data, err := httpc.NewRequest[[]byte]().Get(context.TODO(), "https://example.com/") 
	// "data" is sequence of byte as result of downloading for "GET https://example.com/".
	// "err" indicates with its cause if downloading failed.
	if err != nil {
		panic(err)
	}
	
	// you can assume that "data" is not nil if "err" is nil
	fmt.Println(string(data))
}

you can assign "encoder" and "decoder" for each specific "content-type", like this:

type GetItemsResult struct {
	Code int `json:"code"`
	Message string `json:"message"`
	Items struct {
		ID int `json:"id"`
		Name string `json:"name"`
		Description string `json:"description"`
	} `json:"items"`
}

func jsonDecoder[T any](data []byte) (T, error) {
	var response T
	err := json.Unmarshal(data, &response)
	if err != nil {
		var zero T
		return zero, err
	}
	return response, nil
}

func main() {
	result, err := httpc.NewRequest[GetItemsResult]().Decoder(
		"application/json",
		jsonDecoder[GetItemsResult],
	).Get(context.TODO(), "https://api.example.com/v1/items") 
	// "result" is a instance of GetItemsResult decoded from json response of "GET https://api.example.com/v1/items".
	// "err" indicates with its cause if downloading failed.
	if err != nil {
		panic(err)
	}
	
	// you can assume that "result" is not nil if "err" is nil
	for i, item := range result.Items {
		fmt.Printf("%d) Item %d: %s\n", i, item.ID, item.Name)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAvailableDecoder = errors.New("no available decoder")
View Source
var ErrNoAvailableEncoder = errors.New("no available encoder")
View Source
var ErrUnexpectedType = errors.New("unexpected type")

Functions

This section is empty.

Types

type DecoderFunc

type DecoderFunc[T any] func([]byte) (T, error)

type EncoderFunc

type EncoderFunc func(any) (io.Reader, error)

type Error

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

func (*Error) Body

func (e *Error) Body() []byte

func (*Error) Error

func (e *Error) Error() string

func (*Error) StatusCode

func (e *Error) StatusCode() int

type ErrorHandlerFunc

type ErrorHandlerFunc func(*http.Response, []byte) error

type HttpResult

type HttpResult[T any] struct {
	Response *http.Response
	// contains filtered or unexported fields
}

func (*HttpResult[T]) As

func (r *HttpResult[T]) As(value any) error

type MultipartFormData

type MultipartFormData interface {
	AttachTo(mw *multipart.Writer) error
}

MultipartFormData multipart/form-data形式のPOSTリクエストに添付するためのインターフェイス

multipart/form-data形式でHTTPリクエストに添付ファイルを追加するためのメソッドを定義します。

type Request

type Request[T any] struct {
	// contains filtered or unexported fields
}

Request HTTPリクエストの実装

Tはレスポンスの型を表します。

func NewRequest

func NewRequest[T any]() *Request[T]

func NewRequestFunc

func NewRequestFunc[T any]() *Request[T]

NewRequestFunc Request[T]を生成する関数

デコーダーを指定してHTTPレスポンスボディを処理する方法を決定します。

func (*Request[T]) BasicAuth

func (r *Request[T]) BasicAuth(username, password string) *Request[T]

func (*Request[T]) Decoder

func (r *Request[T]) Decoder(contentType string, decoder DecoderFunc[T]) *Request[T]

func (*Request[T]) Delete

func (r *Request[T]) Delete(ctx context.Context) (T, error)

Delete HTTP DELETEリクエストを実行

note: このメソッドは未実装です。

func (*Request[T]) Encoder

func (r *Request[T]) Encoder(contentType string, encoder EncoderFunc) *Request[T]

func (*Request[T]) Error

func (r *Request[T]) Error(contentType string, errorFunc func(*http.Response, []byte) error) *Request[T]

func (*Request[T]) Get

func (r *Request[T]) Get(ctx context.Context, u string, params ...any) (T, error)

Get HTTP GETリクエストを実行

func (*Request[T]) HTTPClient

func (r *Request[T]) HTTPClient(c *http.Client) *Request[T]

func (*Request[T]) Header

func (r *Request[T]) Header(key, value string) *Request[T]

Header HTTPリクエストのヘッダーを設定(key, valueによるstringペア)

func (*Request[T]) Headers

func (r *Request[T]) Headers(headers any) *Request[T]

Headers HTTPリクエストのヘッダーを設定

func (*Request[T]) KeepAlive

func (r *Request[T]) KeepAlive(keepAlive bool) *Request[T]

func (*Request[T]) Method

func (r *Request[T]) Method(m string) *Request[T]

Method HTTPリクエストのメソッドを設定

func (*Request[T]) Post

func (r *Request[T]) Post(ctx context.Context, u string, params any) (T, error)

Post HTTP GETリクエストを実行

func (*Request[T]) PostForm

func (r *Request[T]) PostForm(ctx context.Context, u string, params any, attachments ...MultipartFormData) (T, error)

PostForm HTTP POSTリクエストを実行

func (*Request[T]) Put

func (r *Request[T]) Put(ctx context.Context) (T, error)

Put HTTP PUTリクエストを実行

note: このメソッドは未実装です。

func (*Request[T]) Query

func (r *Request[T]) Query(params ...any) *Request[T]

Query HTTPリクエストのクエリパラメータを設定

func (*Request[T]) TryDoFunc

func (r *Request[T]) TryDoFunc(ctx context.Context, method, u, contentType string, payloadFunc func() (io.Reader, error)) (Result, error)

DoFunc JSONエンコードされたデータをリクエストボディに含むHTTP POSTリクエストを実行

func (*Request[T]) TryGet

func (r *Request[T]) TryGet(ctx context.Context, u string, params ...any) (Result, error)

TryGet HTTP GETリクエストを実行

func (*Request[T]) TryPost

func (r *Request[T]) TryPost(ctx context.Context, u string, params any) (Result, error)

func (*Request[T]) TryPostForm

func (r *Request[T]) TryPostForm(ctx context.Context, u string, params any, attachments ...MultipartFormData) (Result, error)

TryPostForm HTTP POSTリクエストを実行

返値として

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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