http

package
v0.0.0-...-37076da Latest Latest
Warning

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

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

Documentation

Overview

Package http has the meta data for the http response.

What is the meta data?

Meta data is used to define the pagination for the response It contains the meta data format and the builder to create the meta data

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/jeanmolossi/vigilant-waddle/src/infra/http"
)

func main() {
	m := http.NewMeta(
		http.WithBaseURL("http://example.com/api/resource"),
		http.WithPage(2),
		http.WithPageSize(10),
		http.WithItems(make([]interface{}, 10)),
	)

	fmt.Println(toJson(m))
}

// toJson is a helper function to convert a struct to json
//
// It will return the struct converted as json string
func toJson(m *http.HttpMeta) string {
	b := new(bytes.Buffer)
	encoder := json.NewEncoder(b)
	encoder.SetEscapeHTML(false)
	encoder.SetIndent("", "\t")
	encoder.Encode(m)

	return b.String()
}
Output:

{
	"page": 2,
	"items_per_page": 10,
	"next_page": "http://example.com/api/resource?page=3&items_per_page=10",
	"prev_page": "http://example.com/api/resource?page=3&items_per_page=10",
	"total_count": 10
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpMeta

type HttpMeta struct {
	Page       uint16 `json:"page" example:"2"`
	PageSize   int    `json:"items_per_page" example:"10"`
	NextPage   string `json:"next_page,omitempty" example:"http://example.com/api/resource?page=3&items_per_page=10"`
	PrevPage   string `json:"prev_page,omitempty" example:"http://example.com/api/resource?page=1&items_per_page=10"`
	TotalCount int    `json:"total_count" example:"10"`
	// contains filtered or unexported fields
}

HttpMeta is the meta data for http response

func NewMeta

func NewMeta(opts ...MetaOption) *HttpMeta

NewMeta creates a new meta data

Look at the example for more details

type MetaOption

type MetaOption func(*HttpMeta)

MetaOption is a function that will be called on the meta data

Used to create options for the meta data like

WithBaseURL, WithPage, WithPageSize, WithItems

Example:

// WithBaseURL will define the base url for the meta data
func WithBaseURL(baseURL string) MetaOptions {
	return func(m *HttpMeta) {
		m.baseURL = baseURL
	}
}

func WithBaseURL

func WithBaseURL(baseURL string) MetaOption

WithBaseURL will define the base url for the meta data

func WithItems

func WithItems(items []interface{}) MetaOption

WithItems will define the total items for the meta data

func WithPage

func WithPage(page uint16) MetaOption

WithPage will define the page for the meta data

func WithPageSize

func WithPageSize(pageSize int) MetaOption

WithPageSize will define the page size for the meta data Also known as items per page

Jump to

Keyboard shortcuts

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