core

package
v1.62.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: Apache-2.0 Imports: 18 Imported by: 1,538

Documentation

Overview

This core package providers API and clients base struct for services, and also some infrastructure function, such as signer, send http(s) request and so on.

Index

Constants

View Source
const (
	SchemeHttp  = "http"
	SchemeHttps = "https"

	MethodGet    = "GET"
	MethodPut    = "PUT"
	MethodPost   = "POST"
	MethodDelete = "DELETE"
	MethodPatch  = "PATCH"
	MethodHead   = "HEAD"

	HeaderJcloudPrefix     = "x-jcloud"
	HeaderJdcloudPrefix    = "x-jdcloud"
	HeaderJdcloudRequestId = "x-jdcloud-request-id"
)
View Source
const (
	LogFatal = iota
	LogError
	LogWarn
	LogInfo
)
View Source
const Version = "1.1.5"

Variables

This section is empty.

Functions

func EscapePath

func EscapePath(path string, encodeSep bool) string

EscapePath escapes part of a URL path

Types

type Config

type Config struct {
	Scheme   string
	Endpoint string
	Timeout  time.Duration
}

func NewConfig

func NewConfig() *Config

NewConfig returns a pointer of Config

scheme only accepts http or https

endpoint is the host to access, the connection could not be created if it's error

func (*Config) SetEndpoint

func (c *Config) SetEndpoint(endpoint string)

func (*Config) SetScheme

func (c *Config) SetScheme(scheme string)

func (*Config) SetTimeout

func (c *Config) SetTimeout(timeout time.Duration)

type Credential

type Credential struct {
	AccessKey string
	SecretKey string
}

Credential is used to sign the request, AccessKey and SecretKey could be found in JDCloud console

func NewCredentials

func NewCredentials(accessKey, secretKey string) *Credential

type DefaultLogger

type DefaultLogger struct {
	Level int
}

func NewDefaultLogger

func NewDefaultLogger(level int) *DefaultLogger

func (DefaultLogger) Log

func (logger DefaultLogger) Log(level int, message ...interface{})

type DummyLogger added in v1.13.0

type DummyLogger struct {
}

func NewDummyLogger added in v1.13.0

func NewDummyLogger() *DummyLogger

func (DummyLogger) Log added in v1.13.0

func (logger DummyLogger) Log(level int, message ...interface{})

type ErrorResponse

type ErrorResponse struct {
	Code    int    `json:"code"`
	Status  string `json:"status"`
	Message string `json:"message"`
}

type JDCloudClient

type JDCloudClient struct {
	Credential  Credential
	Config      Config
	ServiceName string
	Revision    string
	Logger      Logger
}

JDCloudClient is the base struct of service clients

func (JDCloudClient) Send

func (c JDCloudClient) Send(request RequestInterface, serviceName string) ([]byte, error)

Send send the request and return the response to the client. Parameter request accepts concrete request object which follow RequestInterface.

type JDCloudRequest

type JDCloudRequest struct {
	URL     string // resource url, i.e. /regions/${regionId}/elasticIps/${elasticIpId}
	Method  string
	Header  map[string]string
	Version string
}

JDCloudRequest is the base struct of service requests

func (*JDCloudRequest) AddHeader

func (r *JDCloudRequest) AddHeader(key, value string)

AddHeader only adds pin or erp, they will be encoded to base64 code

func (JDCloudRequest) GetHeaders

func (r JDCloudRequest) GetHeaders() map[string]string

func (JDCloudRequest) GetMethod

func (r JDCloudRequest) GetMethod() string

func (JDCloudRequest) GetURL

func (r JDCloudRequest) GetURL() string

func (JDCloudRequest) GetVersion

func (r JDCloudRequest) GetVersion() string

type Logger

type Logger interface {
	Log(level int, message ...interface{})
}

type ParameterBuilder

type ParameterBuilder interface {
	BuildURL(url string, paramJson []byte) (string, error)
	BuildBody(paramJson []byte) (string, error)
}

func GetParameterBuilder

func GetParameterBuilder(method string, logger Logger) ParameterBuilder

type RequestInterface

type RequestInterface interface {
	GetURL() string
	GetMethod() string
	GetVersion() string
	GetHeaders() map[string]string
	GetRegionId() string
}

type ResponseProcessor

type ResponseProcessor interface {
	Process(response *http.Response) ([]byte, error)
}

func GetResponseProcessor

func GetResponseProcessor(method string) ResponseProcessor

type SignFunc

type SignFunc func(*http.Request) error

type Signer

type Signer struct {
	Credentials Credential
	Logger      Logger
}

func NewSigner

func NewSigner(credsProvider Credential, logger Logger) *Signer

func (Signer) Sign

func (v4 Signer) Sign(r *http.Request, body io.ReadSeeker, service, region string, signTime time.Time) (http.Header, error)

Sign signs the request by using AWS V4 signer algorithm, and adds Authorization header

type WithBodyBuilder

type WithBodyBuilder struct {
	Logger Logger
}

WithBodyBuilder supports PUT/POST/PATCH methods. It has path and body (json) parameters, but no query parameters.

func (WithBodyBuilder) BuildBody

func (b WithBodyBuilder) BuildBody(paramJson []byte) (string, error)

func (WithBodyBuilder) BuildURL

func (b WithBodyBuilder) BuildURL(url string, paramJson []byte) (string, error)

type WithBodyResponseProcessor

type WithBodyResponseProcessor struct {
}

func (WithBodyResponseProcessor) Process

func (p WithBodyResponseProcessor) Process(response *http.Response) ([]byte, error)

type WithoutBodyBuilder

type WithoutBodyBuilder struct {
	Logger Logger
}

WithoutBodyBuilder supports GET/DELETE methods. It only builds path and query parameters.

func (WithoutBodyBuilder) BuildBody

func (b WithoutBodyBuilder) BuildBody(paramJson []byte) (string, error)

func (WithoutBodyBuilder) BuildURL

func (b WithoutBodyBuilder) BuildURL(url string, paramJson []byte) (string, error)

type WithoutBodyResponseProcessor

type WithoutBodyResponseProcessor struct {
}

func (WithoutBodyResponseProcessor) Process

func (p WithoutBodyResponseProcessor) Process(response *http.Response) ([]byte, error)

Jump to

Keyboard shortcuts

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