rest

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 13 Imported by: 1

README

Go Reference Bitbucket Pipelines

What is this project about?

The rest package is a collection of tools to help you consume a Fundamentum REST API in Go.

Usage

filter := NewFilter().
    Where("project_id", "=", "1").
    Where("name", "=", "test")

route := filter.String()
//output: "?wheres=project_id:=:1,name:=:test"

License

Licensed under Apache License, Version 2.0 LICENSE.

Documentation

Overview

Package rest provide the implementation of http query parser

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultItemPerPage = 25
View Source
var DefaultWithTrashed = false

Functions

func Delete

func Delete[Model any](route string, httpClient http.Client) (Model, error)

func Get

func Get[Model any](route string, httpClient http.Client) (Model, error)

func Post

func Post[Input any, Model any](route string, httpClient http.Client, input Input) (Model, error)

func Put

func Put[Input any, Model any](route string, httpClient http.Client, input Input) (Model, error)

Types

type Filter

type Filter struct {
	Wheres      []where.Input `json:"wheres"`
	OrWheres    []where.Input `json:"or_wheres"`
	OrderBy     orderBy.Input `json:"order_by"`
	With        with.Input    `json:"with"`
	Page        int           `json:"page"`
	ItemPerPage int           `json:"item_per_page"`
	WithTrashed bool          `json:"with_trashed"`
}

func NewFilter

func NewFilter() *Filter

func (*Filter) AddOrderBy

func (f *Filter) AddOrderBy(field string, order string) *Filter

AddOrderBy define order by clause, map of field and order direction as value ("asc" or "desc")

func (*Filter) FromRouteParams

func (f *Filter) FromRouteParams(v url.Values) *Filter

func (*Filter) Load

func (f *Filter) Load(names ...string) *Filter

Load add relations to query (eager load)

func (*Filter) OrWhere

func (f *Filter) OrWhere(name string, compareWith string, compareTo string) *Filter

func (*Filter) SetItemPerPage

func (f *Filter) SetItemPerPage(itemPerPage int) *Filter

func (*Filter) SetPage

func (f *Filter) SetPage(page int) *Filter

func (*Filter) SetWith

func (f *Filter) SetWith(w []string) *Filter

func (*Filter) SetWithTrashed

func (f *Filter) SetWithTrashed(withTrashed bool) *Filter

func (*Filter) String

func (f *Filter) String() string

ToString convert filter to route params

Example
filter := NewFilter().
	Where("project_id", "=", "1").
	Where("name", "=", "test")

route := filter.String()
fmt.Println(route)
Output:

?wheres=project_id:=:1,name:=:test

func (*Filter) Where

func (f *Filter) Where(name string, compareWith string, compareTo string) *Filter

Where add where clause to query

type Pager

type Pager struct {
	CurrentPage  int    `json:"current_page"`
	From         int    `json:"from"`
	To           int    `json:"to"`
	Total        int    `json:"total"`
	LastPage     int    `json:"last_page"`
	PerPage      int    `json:"per_page"`
	FirstPageUrl string `json:"first_page_url"`
	LastPageUrl  string `json:"last_page_url"`
	NextPageUrl  string `json:"next_page_url"`
	PrevPageUrl  string `json:"prev_page_url"`
}

type Response

type Response[Data any] struct {
	Status  string `json:"status"`
	Data    Data   `json:"data"`
	Message string `json:"message"`
	Pager   Pager  `json:"pager"`
}

func List

func List[Model any](route string, httpClient http.Client, filters *Filter) (*Response[[]Model], error)

func (*Response[Data]) IsSuccess

func (r *Response[Data]) IsSuccess() bool

type ValidationErrorResponse

type ValidationErrorResponse map[string][]string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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