rest

package module
v0.0.0-...-d7082e0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

Coverage Status

Go REST client library

Simple Go REST client library using generics. Minimum golang version requirement is 1.18

Install

Installation can be done with a normal go get:

go get github.com/enverbisevac/go-rest-client

Usage

First import dependency:

import "github.com/enverbisevac/go-rest-client"

You can access package functions with rest or if you like aliased imports then use alias. Library provide three basic top level functions: Modify, Get and Delete.

func Modify[T any](ctx context.Context, method string, requestURL string, options ...Option) (val T, err error)
func Get[T any](ctx context.Context, requestURL string, options ...Option) (result T, err error)
func Delete(ctx context.Context, requestURL string, options ...Option) (err error)

Available options:

func WithBody(body any) Option
func WithHeaders(header http.Header) Option

Simple GET request example:

type Article struct {
   Title string `json:"title"`
   Body  string `json:"body"`
}

article, err := rest.Get[Article](context.Background(), "your resource url")

For creating or modifying resource (POST, PUT, PATCH):

article := Article{
	Title: "some title",
	Body: "some body"
}

res, err := rest.Modify[Article](context.Background(), http.MethodPost, "your resource url", rest.WithBody(&article))

Delete a resource example:

article, err := rest.Delete(context.Background(), "your resource url")

if you need to provide custom headers or body for any of the functions (get resource example):

article, err := rest.Get[Article](context.Background(), "your resource url", 
	rest.WithHeaders(http.Header{
	    // custom headers 
	})
)

or modify a resource:

res, err := rest.Modify[Article](context.Background(), http.MethodPost, "your resource url", 
	rest.WithBody(&article), rest.WithHeaders(... some header))

there is also helper function for creating headers:

func Header(opts ...Func) http.Header

and option functions:

func WithHeader(header http.Header) HeaderOption
func WithAuth(auth AuthType, token string) HeaderOption
func WithContent(value ContentType) HeaderOption
func With(name string, value ...string) HeaderOption

for example:

res, err := rest.Modify[Article](context.Background(), http.MethodPost, "your resource url", 
	rest.WithBody(&article), rest.WithHeaders(
		rest.Headers(
			rest.WithAuth(rest.Bearer, key),
		),
	),
)

Documentation

Index

Constants

View Source
const (
	Content       = "Content-Type"
	Authorization = "Authorization"
)

Variables

View Source
var (
	// ErrMethodNotAllowed ...
	ErrMethodNotAllowed = errors.New("method not allowed")
	// ErrMarshallerFuncNotFound ...
	ErrMarshallerFuncNotFound = errors.New("marshaller function not found in map")
	// ErrUnmarshalerFuncNotFound ...
	ErrUnmarshalerFuncNotFound = errors.New("unmarshaler function not found in map")
)
View Source
var (
	// Marshaller maps some basic content types with top level encoding functions from stdlib
	Marshaller = map[ContentType]func(v any) ([]byte, error){
		ApplicationJSON: json.Marshal,
		ApplicationXML:  xml.Marshal,
	}

	// Unmarshaler maps some basic content types with top level decoding functions from stdlib
	Unmarshaler = map[ContentType]func(data []byte, v any) error{
		ApplicationJSON: json.Unmarshal,
		ApplicationXML:  xml.Unmarshal,
	}

	// DefaultContentType will be used if no content type specified in headers
	DefaultContentType = ApplicationJSON
)

Functions

func Delete

func Delete(ctx context.Context, requestURL string, options ...Option) (err error)

Delete resource from requestedURL with options WithBody, WithHeaders if error occurred T will be zero value

func Get

func Get[T any](ctx context.Context, requestURL string, options ...Option) (result T, err error)

Get resource T from requestedURL with options: WithBody, WithHeaders if error occurred T will be zero value

func Header(opts ...HeaderOption) http.Header

func Modify

func Modify[T any](ctx context.Context, method string, requestURL string, options ...Option) (val T, err error)

Modify resource on requestURL with options WithBody, WithHeaders and return T method can be POST, PUT, DELETE if error occurred T will be zero value

Types

type AuthType

type AuthType string
const (
	Bearer AuthType = "Bearer"
)

type ContentType

type ContentType string
const (
	TextPlain       ContentType = "text/plain"
	ApplicationJSON ContentType = "application/json"
	ApplicationXML  ContentType = "application/xml"
)

func (ContentType) String

func (t ContentType) String() string

type Error

type Error struct {
	StatusCode int
	Message    string
}

Error is custom type for displaying information like status and body of the response message

func (Error) Error

func (e Error) Error() string

type HeaderOption

type HeaderOption func(headers http.Header)

func With

func With(name string, value ...string) HeaderOption

func WithAuth

func WithAuth(auth AuthType, token string) HeaderOption

func WithContent

func WithContent(value ContentType) HeaderOption

func WithHeader

func WithHeader(header http.Header) HeaderOption

type Option

type Option func(r *requestData)

func WithBody

func WithBody(body any) Option

WithBody option set body for request

func WithHeaders

func WithHeaders(header http.Header) Option

WithHeaders option set headers for request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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