http

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: Apache-2.0 Imports: 12 Imported by: 7

README

sacloud/go-http

Go Reference Tests Go Report Card

HTTP client library for SAKURA cloud in Go

Installation

Use go get.

go get github.com/sacloud/go-http

Then import the sacloud package into your own code.

import "github.com/sacloud/go-http"

License

go-http Copyright (C) 2021 The sacloud/go-http authors.

This project is published under Apache 2.0 License.

Documentation

Index

Constants

View Source
const (
	// APIAccessTokenEnvKey APIアクセストークンの環境変数名
	APIAccessTokenEnvKey = "SAKURACLOUD_ACCESS_TOKEN"
	// APIAccessSecretEnvKey APIアクセスシークレットの環境変数名
	APIAccessSecretEnvKey = "SAKURACLOUD_ACCESS_TOKEN_SECRET"
)

TODO 後で別プロジェクトへ切り出す

View Source
const Version = "0.0.3"

Version バージョン

Variables

View Source
var (
	// DefaultUserAgent デフォルトのユーザーエージェント
	DefaultUserAgent = fmt.Sprintf(
		"go-http/v%s (%s/%s; +https://github.com/sacloud/go-http)",
		Version,
		runtime.GOOS,
		runtime.GOARCH,
	)

	// DefaultAcceptLanguage デフォルトのAcceptLanguage
	DefaultAcceptLanguage = ""

	// DefaultRetryMax デフォルトのリトライ回数
	DefaultRetryMax = 10

	// DefaultRetryWaitMin デフォルトのリトライ間隔(最小)
	DefaultRetryWaitMin = 1 * time.Second
	// DefaultRetryWaitMax デフォルトのリトライ間隔(最大)
	DefaultRetryWaitMax = 64 * time.Second

	// DefaultCheckRetryFunc デフォルトのリトライ判定Func
	DefaultCheckRetryFunc = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
		if ctx.Err() != nil {
			return false, ctx.Err()
		}
		if err != nil {
			return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
		}
		if resp.StatusCode == 0 || resp.StatusCode == http.StatusServiceUnavailable || resp.StatusCode == http.StatusLocked {
			return true, nil
		}
		return false, nil
	}
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// AccessToken アクセストークン
	AccessToken string `validate:"required"`
	// AccessTokenSecret アクセストークンシークレット
	AccessTokenSecret string `validate:"required"`
	// ユーザーエージェント
	UserAgent string
	// Accept-Language
	AcceptLanguage string
	// Gzipを有効にするか
	Gzip bool
	// CheckRetryFunc リトライすべきか判定するためのfunc
	CheckRetryFunc func(ctx context.Context, resp *http.Response, err error) (bool, error)
	// リトライ回数
	RetryMax int
	// リトライ待ち時間(最小)
	RetryWaitMin time.Duration
	// リトライ待ち時間(最大)
	RetryWaitMax time.Duration
	// APIコール時に利用される*http.Client 未指定の場合http.DefaultClientが利用される
	HTTPClient *http.Client
	// RequestCustomizer リクエスト前に*http.Requestのカスタマイズを行うためのfunc
	RequestCustomizer RequestCustomizer
}

Client さくらのクラウドAPI(secure.sakura.ad.jp)向けのHTTPクライアント

レスポンスの状態に応じてリトライする仕組みを持つ デフォルトだとレスポンスステータスコード423、または503を受け取った場合にRetryMax回リトライする

リトライ間隔はRetryMinからRetryMaxまで指数的に増加する(Exponential Backoff)

リトライ時にcontext.Canceled、またはcontext.DeadlineExceededの場合はリトライしない

func NewClient

func NewClient(token, secret string) *Client

NewClient APIクライアント作成

func NewClientFromEnv

func NewClientFromEnv() (*Client, error)

NewClientFromEnv 環境変数からAPIキーを取得してAPIクライアントを作成する

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do APIコール実施

type RateLimitRoundTripper

type RateLimitRoundTripper struct {
	// Transport 親となるhttp.RoundTripper、nilの場合http.DefaultTransportが利用される
	Transport http.RoundTripper
	// RateLimitPerSec 秒あたりのリクエスト数
	RateLimitPerSec int
	// contains filtered or unexported fields
}

RateLimitRoundTripper 秒間アクセス数を制限するためのhttp.RoundTripper実装

func (*RateLimitRoundTripper) RoundTrip

func (r *RateLimitRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip http.RoundTripperの実装

type RequestCustomizer

type RequestCustomizer func(r *http.Request) error

RequestCustomizer リクエスト前に*http.Requestのカスタマイズを行うためのfunc

func ComposeRequestCustomizer

func ComposeRequestCustomizer(funcs ...RequestCustomizer) RequestCustomizer

ComposeRequestCustomizer 任意の個数のRequestCustomizerを合成してRequestCustomizerを返す

複数のRequestCustomizerを指定した場合は先頭から呼びだされ、エラーを返したら即時returnする

type TracingRoundTripper

type TracingRoundTripper struct {
	// Transport 親となるhttp.RoundTripper、nilの場合http.DefaultTransportが利用される
	Transport http.RoundTripper
}

TracingRoundTripper リクエスト/レスポンスのトレースログを出力するためのhttp.RoundTripper実装

Client.Gzipがtrueの場合でも関知しないため利用者側で制御する必要がある

func (*TracingRoundTripper) RoundTrip

func (r *TracingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip http.RoundTripperの実装

Jump to

Keyboard shortcuts

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