room

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 1

README

Room

⭐️Create your http client wrapper in a few minutes.

⚡️ Roadmap

⭐️ This package is still in development. We welcome suggestions for changes that will bring it closer to compliance without overly complicating the code, or useful test cases to add to the test suite.

  • init project as mvp
  • add auth example
  • remove panics from request.go file
  • create default exceptions for fail status code
  • make better readme
  • add request debugger
  • write unit tests
  • add caching mechanism
  • add concurrent request functionality to rooms
  • add response normalizer
  • make sure struct alignment is in a strict line

Name

Room

Description

Create your clients in a minute

Installation

go get -u github.com/WEG-Technology/room

📚 Usage

There are many examples in examples folder go look for it.

💫 Most Basic Example
response, err := room.NewRequest("https://jsonplaceholder.typicode.com/posts/1").Send()
Connection

We are going to wrap our base connection struct to get its skills.

Requests

All of your request should implement IRequest.

Responses

The responses literarily is a DTO of your response of your endpoint.

Room

Your client wrapper should implement IRoom. You can implement your custom interfaces by wrapping base Room struct.

There is an example of this explanation in ./examples/room_example

Step By Step Usage

Currently ./examples folder is the best place to look for examples.

TODO

📫 Contributing

TODO

🧬 Clients Developed with Room

TODO Please, feel free to add your client to this list.

Documentation

Index

Constants

View Source
const (
	ErrAuthRoomCanNotFoundKey = "authToken can not found in response"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessTokenAuth

type AccessTokenAuth struct{}

func (AccessTokenAuth) Apply

func (a AccessTokenAuth) Apply(connector *Connector, response Response)

type AuthRoom

type AuthRoom struct {
	*Room
	AuthRequest *Request
	AuthToken   string
}

func (*AuthRoom) Send

func (r *AuthRoom) Send(request *Request) (Response, error)

type Connector

type Connector struct {
	Header IHeader
	// contains filtered or unexported fields
}

func NewConnector

func NewConnector(baseUrl string, opts ...OptionConnector) *Connector

func (*Connector) Do

func (c *Connector) Do(request *Request) (Response, error)

func (*Connector) Send

func (c *Connector) Send(path string) (Response, error)

type Context

type Context struct {
	Ctx    context.Context
	Cancel context.CancelFunc
}

type ContextBuilder

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

func NewContextBuilder

func NewContextBuilder(timeout time.Duration) ContextBuilder

func (ContextBuilder) Build

func (b ContextBuilder) Build() Context

type FormURLEncodedBody

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

func (FormURLEncodedBody) Parse

func (f FormURLEncodedBody) Parse() *bytes.Buffer

type HTTPMethod

type HTTPMethod string
const (
	GET    HTTPMethod = "GET"
	POST   HTTPMethod = "POST"
	PUT    HTTPMethod = "PUT"
	PATCH  HTTPMethod = "PATCH"
	DELETE HTTPMethod = "DELETE"
	HEAD   HTTPMethod = "HEAD"
)

func (HTTPMethod) String

func (s HTTPMethod) String() string

type HTTPProtocol

type HTTPProtocol int
const (
	Http HTTPProtocol = iota + 1
	Https
)

func (HTTPProtocol) String

func (h HTTPProtocol) String() string
type Header struct {
	// contains filtered or unexported fields
}

func (*Header) Add

func (h *Header) Add(key string, value string) IHeader

func (*Header) Get

func (h *Header) Get(key string) string

func (*Header) Merge

func (h *Header) Merge(header IHeader) IHeader

func (*Header) Properties

func (h *Header) Properties() store.IMap

func (*Header) String

func (h *Header) String() (str string)

type IAuth

type IAuth interface {
	Apply(connector *Connector, response Response)
}

func NewAccessTokenAuth added in v1.0.1

func NewAccessTokenAuth() IAuth

type IBodyParser

type IBodyParser interface {
	Parse() *bytes.Buffer
}

func NewFormURLEncodedBodyParser

func NewFormURLEncodedBodyParser(v any) IBodyParser

func NewJsonBodyParser

func NewJsonBodyParser(v any) IBodyParser

type IContextBuilder

type IContextBuilder interface {
	Build() Context
}

type IDTOFactory

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

IDTOFactory declares the interface for creating DTOs.

func NewDTOFactory

func NewDTOFactory(contentType ...string) IDTOFactory

NewDTOFactory creates a concrete factory based on content type.

type IHeader

type IHeader interface {
	Properties() store.IMap
	Add(key string, value string) IHeader
	Get(key string) string
	Merge(header IHeader) IHeader
	String() string
}

func NewHeader

func NewHeader(properties ...store.IMap) IHeader

type IMapQuery

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

func (IMapQuery) String

func (q IMapQuery) String() string

type IQuery

type IQuery interface {
	String() string
}

func NewQuery

func NewQuery(v any) IQuery

type IRoom

type IRoom interface {
	Send(request *Request) (Response, error)
}

func NewAuthRoom

func NewAuthRoom(connector *Connector, authRequest *Request, authToken string) IRoom

type ISend

type ISend interface {
	Send() Response
}

type IUrlQuery

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

func (IUrlQuery) String

func (q IUrlQuery) String() string

type JsonBody

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

func (JsonBody) Parse

func (f JsonBody) Parse() *bytes.Buffer

type JsonDTOFactory

type JsonDTOFactory struct{}

JsonDTOFactory creates JSON DTOs.

type OptionConnector

type OptionConnector func(info *Connector)

func WithHeaderConnector

func WithHeaderConnector(header IHeader) OptionConnector

func WithHeaderContextBuilder

func WithHeaderContextBuilder(ctxBuilder IContextBuilder) OptionConnector

type OptionRequest

type OptionRequest func(request *Request)

func WithBody

func WithBody(bodyParser IBodyParser) OptionRequest

func WithContextBuilder

func WithContextBuilder(contextBuilder IContextBuilder) OptionRequest

func WithHeader

func WithHeader(header IHeader) OptionRequest

func WithMethod

func WithMethod(method HTTPMethod) OptionRequest

func WithQuery

func WithQuery(query IQuery) OptionRequest

type Query

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

type Request

type Request struct {
	URI        URI
	Method     HTTPMethod
	Header     IHeader
	Query      IQuery
	BodyParser IBodyParser
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(path string, opts ...OptionRequest) *Request

NewRequest creates a new request baseUrl: the base url ex: http://localhost:8080, http://localhost/path, path/, path?lorem=ipsum opts: options to configure the request

func (*Request) Send

func (r *Request) Send() (Response, error)

type RequestDTO added in v1.1.0

type RequestDTO struct {
	Header IHeader
	Data   []byte
	URI    URI
	Method string
}

type Response

type Response struct {
	StatusCode int
	Header     IHeader
	Data       []byte
	Request    RequestDTO
}

func NewErrorResponse added in v1.1.0

func NewErrorResponse(request *http.Request, err error) (Response, error)

func NewResponse

func NewResponse(response *http.Response, request *http.Request) Response

func (Response) DTO

func (r Response) DTO(v any) any

func (Response) DTOorFail added in v1.0.2

func (r Response) DTOorFail(v any) error

func (Response) OK

func (r Response) OK() bool

func (Response) RequestBody

func (r Response) RequestBody() map[string]any

func (Response) RequestBodyOrFail added in v1.1.0

func (r Response) RequestBodyOrFail() (map[string]any, error)

func (Response) RequestDTO added in v1.1.0

func (r Response) RequestDTO(v any) any

func (Response) RequestDTOorFail added in v1.1.0

func (r Response) RequestDTOorFail(v any) any

func (Response) ResponseBody added in v1.0.2

func (r Response) ResponseBody() map[string]any

func (Response) ResponseBodyOrFail added in v1.0.2

func (r Response) ResponseBodyOrFail() (map[string]any, error)

type Room

type Room struct {
	Connector *Connector
}

func NewRoom

func NewRoom(connector *Connector) *Room

func (*Room) Send

func (r *Room) Send(request *Request) (Response, error)

type URI

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

func NewURI

func NewURI(fullUrl string) URI

func (URI) Authority

func (u URI) Authority() string

func (URI) Path

func (u URI) Path() string

func (URI) Query

func (u URI) Query() string

func (URI) Scheme

func (u URI) Scheme() string

func (URI) String

func (u URI) String() string

type XMLDTOFactory

type XMLDTOFactory struct{}

XMLDTOFactory creates XML DTOs.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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