gotten

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 21 Imported by: 0

README

Coverage Status Go Report Card Build Status Documentation

Usage

package example

import (
	"fmt"
	"github.com/Hexilee/gotten"
	"net/http"
	"time"
)

type (
	SimpleParams struct {
		Id   int `type:"path"`
		Page int `type:"query"`
	}

	Item struct {
		TypeId      int
		IId         int
		Name        string
		Description string
	}

	SimpleService struct {
		GetItems func(*SimpleParams) (gotten.Response, error) `method:"GET";path:"itemType/{id}"`
	}
)

var (
	creator, err = gotten.NewBuilder().
		SetBaseUrl("https://api.sample.com").
		AddCookie(&http.Cookie{Name: "clientcookieid", Value: "121", Expires: time.Now().Add(111 * time.Second)}).
		Build()

	simpleServiceImpl = new(SimpleService)
)

func init() {
	err := creator.Impl(simpleServiceImpl)
	if err != nil {
		panic(err)
	}
}

func InYourFunc() {
	resp, err := simpleServiceImpl.GetItems(&SimpleParams{1, 1})
	if err == nil && resp.StatusCode() == http.StatusOK {
		result := make([]*Item, 0) 
		err = resp.Unmarshal(&result)
		fmt.Printf("%#v\n", result)
	}
}

Documentation

Index

Constants

View Source
const (
	BaseUrlCannotBeEmpty          = "baseUrl cannot be empty"
	MustPassPtrToImpl             = "must pass the ptr of the service to be implemented"
	ServiceMustBeStruct           = "service must be struct"
	UnrecognizedHTTPMethod        = "http method is unrecognized"
	ParamTypeMustBePtrOfStruct    = "param type must be ptr of struct"
	ValueIsNotString              = "value is not a string"
	ValueIsNotInt                 = "value is not a int"
	DuplicatedPathKey             = "duplicated path key"
	UnsupportedValueType          = "field type is unrecognized"
	UnrecognizedPathKey           = "path key is unrecognized"
	EmptyRequiredVariable         = "required variable is empty"
	UnsupportedFieldType          = "field type is unsupported"
	SomePathVarHasNoValue         = "some pathValue has no value"
	NoUnmarshalerFoundForResponse = "no unmarshaler found for response"
	ContentTypeConflict           = "content type conflict: "
	UnsupportedFuncType           = "function type is not supported"
)
View Source
const (
	// support types: fmt.Stringer, int, string
	TypeHeader = "header"

	// support types: fmt.Stringer, int, string
	TypePath = "path"

	// support types: fmt.Stringer, int, string
	TypeQuery = "query"

	// support types: fmt.Stringer, int, string
	TypeForm = "form"

	// support types: fmt.Stringer, int, string
	TypeCookie = "cookie"

	// support types: fmt.Stringer, int, string, Reader, FilePath
	TypeMultipart = "part"

	// support types: fmt.Stringer, Reader, string, struct, slice, map
	TypeJSON = "json"

	// support types: fmt.Stringer, Reader, string, struct, slice, map
	TypeXML = "xml"
)

value types

View Source
const (
	KeyKey    = "key"
	KeyMethod = "method"
	KeyType   = "type"
	//KeyStatus  = "status"
	KeyPath    = "path"
	KeyDefault = "default"
	KeyRequire = "require"
)
View Source
const (
	PathKeyRegexp = `\{[a-zA-Z_][0-9a-zA-Z_]*\}`
	ZeroStr       = ""

	// json Marshal nil
	NullStr = "null"
	ZeroInt = 0
)

Variables

View Source
var (
	FilePathType = reflect.TypeOf(filePath)
	IntType      = reflect.TypeOf(int(1))
	StringType   = reflect.TypeOf("")
)
View Source
var (
	ZeroStringer fmt.Stringer = bytes.NewBufferString("")
	ZeroReader   io.Reader    = bytes.NewBufferString("")
)
View Source
var (
	StringerType = typesValue.FieldByName("stringer").Type()
	ReaderType   = typesValue.FieldByName("reader").Type()
	ErrorType    = typesValue.FieldByName("error").Type()
	ResponseType = typesValue.FieldByName("response").Type()
	RequestType  = typesValue.FieldByName("request").Type()
)

Functions

func Any

func Any(_ *http.Response) bool

func ContentTypeConflictError

func ContentTypeConflictError(former string, latter string) error

func DuplicatedPathKeyError

func DuplicatedPathKeyError(key string) error

func EmptyRequiredVariableError

func EmptyRequiredVariableError(name string) error

func MustPassPtrToImplError

func MustPassPtrToImplError(p reflect.Type) error

func NoUnmarshalerFoundForResponseError

func NoUnmarshalerFoundForResponseError(response *http.Response) error

func ParamTypeMustBePtrOfStructError

func ParamTypeMustBePtrOfStructError(p reflect.Type) error

func ServiceMustBeStructError

func ServiceMustBeStructError(p reflect.Type) error

func SomePathVarHasNoValueError

func SomePathVarHasNoValueError(list PathKeyList) error

func UnrecognizedHTTPMethodError

func UnrecognizedHTTPMethodError(method string) error

func UnrecognizedPathKeyError

func UnrecognizedPathKeyError(key string) error

func UnsupportedFieldTypeError

func UnsupportedFieldTypeError(fieldType reflect.Type, valueType string) error

func UnsupportedFuncTypeError added in v1.0.1

func UnsupportedFuncTypeError(p reflect.Type) error

func UnsupportedValueTypeError

func UnsupportedValueTypeError(valueType string) error

func ValueIsNotIntError

func ValueIsNotIntError(p reflect.Type) error

func ValueIsNotStringError

func ValueIsNotStringError(p reflect.Type) error

Types

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func (*Builder) AddCookie

func (builder *Builder) AddCookie(cookie *http.Cookie) *Builder

func (*Builder) AddCookies

func (builder *Builder) AddCookies(cookies []*http.Cookie) *Builder

func (*Builder) AddHeader

func (builder *Builder) AddHeader(key, value string) *Builder

func (*Builder) AddReadUnmarshalFunc

func (builder *Builder) AddReadUnmarshalFunc(unmarshaler ReadUnmarshalFunc, checker Checker) *Builder

func (*Builder) AddReaderUnmarshaler

func (builder *Builder) AddReaderUnmarshaler(unmarshaler ReadUnmarshaler, checker Checker) *Builder

func (*Builder) AddUnmarshalFunc

func (builder *Builder) AddUnmarshalFunc(unmarshaler UnmarshalFunc, checker Checker) *Builder

func (*Builder) AddUnmarshaler

func (builder *Builder) AddUnmarshaler(unmarshaler Unmarshaler, checker Checker) *Builder

func (*Builder) Build

func (builder *Builder) Build() (creator *Creator, err error)

func (*Builder) SetBaseUrl

func (builder *Builder) SetBaseUrl(url string) *Builder

func (*Builder) SetClient

func (builder *Builder) SetClient(client Client) *Builder

func (*Builder) SetHeader

func (builder *Builder) SetHeader(key, value string) *Builder

type Checker

type Checker interface {
	Check(*http.Response) bool
}

type CheckerFactory

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

func (*CheckerFactory) Create

func (factory *CheckerFactory) Create() (checker CheckerFunc)

func (*CheckerFactory) When

func (factory *CheckerFactory) When(key string, values ...string) *CheckerFactory

func (*CheckerFactory) WhenContentType

func (factory *CheckerFactory) WhenContentType(values ...string) *CheckerFactory

func (*CheckerFactory) WhenStatuses

func (factory *CheckerFactory) WhenStatuses(statuses ...int) *CheckerFactory

type CheckerFunc

type CheckerFunc func(*http.Response) bool

func (CheckerFunc) Check

func (fn CheckerFunc) Check(resp *http.Response) bool

type Client

type Client interface {
	Do(r *http.Request) (*http.Response, error)
}

type ConditionalUnmarshaler

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

type ConditionalUnmarshalers

type ConditionalUnmarshalers []*ConditionalUnmarshaler

func (ConditionalUnmarshalers) Check

func (unmarshalers ConditionalUnmarshalers) Check(response *http.Response) (unmarshaler ReadUnmarshaler, exist bool)

type Creator

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

func (*Creator) Impl

func (creator *Creator) Impl(service interface{}) (err error)

func(*params) (*http.Request, error) || func(*params) (gotten.Response, error)

type Field

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

TypePath, TypeQuery, TypeForm, TypeHeader, TypeCookie, TypeMultipart(except io.Reader)

type FilePath

type FilePath string

type HeaderSet

type HeaderSet map[string]map[string]bool

type IOField

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

TypeJSON, TypeXML, TypeMultipart(io.Reader)

type MultipartReader

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

type PathKeyList

type PathKeyList map[string]bool

type ReadCloser

type ReadCloser interface {
	io.ReadCloser
	Empty() bool
}

type ReadUnmarshalFunc

type ReadUnmarshalFunc func(reader io.ReadCloser, header http.Header, v interface{}) error

func (ReadUnmarshalFunc) Unmarshal

func (fn ReadUnmarshalFunc) Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) error

type ReadUnmarshaler

type ReadUnmarshaler interface {
	Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) error
}

func NewReaderAdapter

func NewReaderAdapter(unmarshaler Unmarshaler) ReadUnmarshaler

type Reader

type Reader interface {
	io.Reader
	Empty() bool
}

type ReaderAdapter

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

func (*ReaderAdapter) Unmarshal

func (adapter *ReaderAdapter) Unmarshal(reader io.ReadCloser, header http.Header, v interface{}) (err error)

type ReaderImpl

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

func (ReaderImpl) Close

func (reader ReaderImpl) Close() (err error)

func (ReaderImpl) Empty

func (reader ReaderImpl) Empty() bool

func (ReaderImpl) Read

func (reader ReaderImpl) Read(p []byte) (n int, err error)

type Response

type Response interface {
	StatusCode() int
	Header() http.Header
	Body() io.ReadCloser
	ContentType() string

	Cookies() []*http.Cookie

	// Location returns the URL of the response's "Location" header,
	// if present. Relative redirects are resolved relative to
	// the Response's Request. ErrNoLocation is returned if no
	// Location header is present.
	Location() (*url.URL, error)

	// ProtoAtLeast reports whether the HTTP protocol used
	// in the response is at least major.minor.
	ProtoAtLeast(major, minor int) bool

	Unmarshal(ptr interface{}) error
}

type ResponseImpl

type ResponseImpl struct {
	*http.Response
	// contains filtered or unexported fields
}

func (ResponseImpl) Body

func (resp ResponseImpl) Body() io.ReadCloser

func (ResponseImpl) ContentType

func (resp ResponseImpl) ContentType() string

func (ResponseImpl) Header

func (resp ResponseImpl) Header() http.Header

func (ResponseImpl) StatusCode

func (resp ResponseImpl) StatusCode() int

func (ResponseImpl) Unmarshal

func (resp ResponseImpl) Unmarshal(ptr interface{}) error

type StatusSet

type StatusSet map[int]bool

type Types

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

type UnmarshalFunc

type UnmarshalFunc func(data []byte, v interface{}) error

func (UnmarshalFunc) Unmarshal

func (fn UnmarshalFunc) Unmarshal(data []byte, v interface{}) error

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data []byte, v interface{}) error
}

func UnmarshalAdapter

func UnmarshalAdapter(fn UnmarshalFunc) Unmarshaler

type UrlCtr

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

type VarsController

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

type VarsCtr

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

type VarsParser

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

func (*VarsParser) Build

func (parser *VarsParser) Build() VarsController

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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