cherry

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 9 Imported by: 0

README

cherry

cherry implements a simple HTTP client with JSON and type validation suport.

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	validation "github.com/go-ozzo/ozzo-validation"
	"github.com/onur1/cherry"
)

type Entry struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Slug        string `json:"slug"`
}

func (e *Entry) Validate() error {
	return validation.ValidateStruct(
		e,
		validation.Field(&e.Title, validation.Required),
		validation.Field(&e.Description, validation.Required),
		validation.Field(&e.Slug, validation.Required),
	)
}

type Index struct {
	Entries []*Entry `json:"entries"`
}

func main() {
	req := cherry.Get[Index]("https://ogu.nz/index.json", nil)

	resp, index, err := cherry.Send(http.DefaultClient, req)
	if err != nil {
		log.Fatalf("send failed: %v", err)
	}

	fmt.Println(resp.StatusCode)

	fmt.Println(len(index.Entries) > 0)

	for _, v := range index.Entries {
		fmt.Println(v.Title)
	}
}

Output:

200
true
flixbox
binproto
tango
couchilla
This site

Documentation

Overview

package cherry implements a simple HTTP client with JSON and type validation support.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout represents a timeout error.
	ErrTimeout = errors.New("timeout")
	// ErrBadURL represents a 404 not found response.
	ErrBadURL = errors.New("bad url")
	// ErrBadStatus represents a response with a status code which is not 2xx.
	ErrBadStatus = errors.New("bad status")
	// ErrBadRequest wraps any error that might happen while converting a Request
	// into an http.Request.
	ErrBadRequest = errors.New("bad request")
)

Functions

func Send

func Send[A any](c Client, r *Request[A]) (resp *http.Response, a *A, e error)

Send creates and sends a new http.Request, returning an HTTP response and a pointer to a value of type A along with an error if any encountered.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	validation "github.com/go-ozzo/ozzo-validation"
	"github.com/onur1/cherry"
)

type Entry struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Slug        string `json:"slug"`
}

func (e *Entry) Validate() error {
	return validation.ValidateStruct(
		e,
		validation.Field(&e.Title, validation.Required),
		validation.Field(&e.Description, validation.Required),
		validation.Field(&e.Slug, validation.Required),
	)
}

type Index struct {
	Entries []*Entry `json:"entries"`
}

func (e *Index) Print() {
	for _, entry := range e.Entries {
		fmt.Printf(
			"%s [https://ogu.nz/%s.html]\n%s\n\n",
			entry.Title, entry.Slug, entry.Description,
		)
	}
}

func main() {
	req := cherry.Get[Index]("https://ogu.nz/index.json", nil)

	resp, index, err := cherry.Send(http.DefaultClient, req)
	if err != nil {
		log.Fatalf("send failed: %v", err)
	}

	fmt.Println(resp.StatusCode)

	fmt.Println(len(index.Entries) > 0)

	// index.Print()

}
Output:

200
true

func SendWithContext

func SendWithContext[A any](ctx context.Context, c Client, r *Request[A]) (resp *http.Response, a *A, e error)

SendWithContext creates and sends a new context-aware http.Request, returning an HTTP response and a pointer to a value of type A along with an error if any encountered.

Types

type Client

type Client interface {
	// Do sends an HTTP request and returns an HTTP response, following policy
	// (such as redirects, cookies, auth) as configured on the client.
	Do(*http.Request) (*http.Response, error)
}

A Client manages the HTTP connection.

type Request

type Request[A any] struct {
	// URL specifies the URI being requested.
	URL string
	// Method specifies the HTTP method (GET, POST, PUT, etc.).
	Method string
	// Headers contain the request header fields to be sent.
	Headers map[string]string
	// Body is request's body.
	Body any
}

A Request represents an HTTP request to be sent by a client.

func Get

func Get[A any](url string, headers map[string]string) *Request[A]

Get creates a new GET a return value of type A.

func Patch

func Patch[A, I any](url string, body *I, headers map[string]string) *Request[A]

Patch creates a new PATCH request with the specified payload and with a return value of type A.

func Post

func Post[A, I any](url string, body *I, headers map[string]string) *Request[A]

Post creates a new POST request with the specified payload and with a return value of type A.

func Put

func Put[A, I any](url string, body *I, headers map[string]string) *Request[A]

Put creates a new PUT request with the specified payload and with a return value of type A.

type ValidationError

type ValidationError = validation.Errors

ValidationError is an alias to validation.Errors from the ozzo-validation library.

Example
package main

import (
	"fmt"
	"net/http"

	validation "github.com/go-ozzo/ozzo-validation"
	"github.com/onur1/cherry"
)

type unknownEntryType struct {
	ID string `json:"id"`
}

func (e *unknownEntryType) Validate() error {
	return validation.ValidateStruct(
		e,
		validation.Field(&e.ID, validation.Required, validation.Length(2, 8)),
	)
}

func main() {
	req := cherry.Get[unknownEntryType]("https://ogu.nz/index.json", nil)

	resp, _, err := cherry.Send(http.DefaultClient, req)
	if err != nil {
		validationErrors, ok := err.(validation.Errors)
		fmt.Println(ok)
		fmt.Println(validationErrors)
	}

	fmt.Println(resp.StatusCode)

}
Output:

true
id: cannot be blank.
200

Jump to

Keyboard shortcuts

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