rest

package
v2.7.4 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: LGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Funciones de utilidad para consumir servicios REST

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoRestDelete added in v2.7.2

func DoRestDelete(host, endpoint string, params url.Values, headers ...string) (code int, err error)
Example
package main

import (
	"fmt"
	"net/url"

	"github.com/horus-es/go-util/v2/errores"
	"github.com/horus-es/go-util/v2/rest"
)

func main() {
	// Servicio REST publico de prueba que devuelve lo mismo que se le envía
	host := "https://httpbin.org"
	endpoint := "anything"
	params := url.Values{}
	params.Set("action", "getHolidaysForYear")
	params.Set("year", "2023")
	params.Set("country", "esp")
	code, err := rest.DoRestDelete(host, endpoint, params)
	errores.PanicIfError(err, "Error de red")
	errores.PanicIfTrue(code < 200 || code > 299, "Recibido código de respuesta http: %d", code)
	fmt.Println(code)
}
Output:

200

func DoRestGet

func DoRestGet[T any](host, endpoint string, params url.Values, headers ...string) (response T, code int, err error)
Example
package main

import (
	"fmt"
	"net/url"

	"github.com/horus-es/go-util/v2/errores"
	"github.com/horus-es/go-util/v2/rest"
)

func main() {
	// Servicio REST publico que devuelve los festivos en una determinada región
	host := "https://kayaposoft.com"
	endpoint := "/enrico/json/v2.0"
	params := url.Values{}
	params.Set("action", "getHolidaysForYear")
	params.Set("year", "2023")
	params.Set("country", "esp")
	type tFestivos []struct {
		Date struct {
			Day       int
			Month     int
			Year      int
			DayofWeek int
		}
		Name []struct {
			Lang string
			Text string
		}
		HolidayType string
	}
	festivos, code, err := rest.DoRestGet[tFestivos](host, endpoint, params)
	errores.PanicIfError(err, "Error de red")
	errores.PanicIfTrue(code < 200 || code > 299, "Recibido código de respuesta http: %d", code)
final:
	for _, festivo := range festivos {
		for _, name := range festivo.Name {
			if name.Lang == "es" {
				fmt.Printf("El primer festivo del año %d en España es %s (%02d/%02d)", festivo.Date.Year, name.Text, festivo.Date.Day, festivo.Date.Month)
				break final
			}
		}
	}
}
Output:

El primer festivo del año 2023 en España es Año Nuevo (01/01)

func DoRestPost

func DoRestPost[T any](host, endpoint string, request any, headers ...string) (response T, code int, err error)
Example
package main

import (
	"fmt"

	"github.com/horus-es/go-util/v2/errores"
	"github.com/horus-es/go-util/v2/rest"
)

func main() {
	// Servicio REST publico de prueba que devuelve lo mismo que se le envía
	host := "https://httpbin.org"
	endpoint := "anything"
	type tEstructura struct {
		Uno  string
		Dos  string
		Tres string
	}
	request := tEstructura{Uno: "1", Dos: "2", Tres: "3"}
	response, code, err := rest.DoRestPost[map[string]any](host, endpoint, request)
	errores.PanicIfError(err, "Error de red")
	errores.PanicIfTrue(code < 200 || code > 299, "Recibido código de respuesta http: %d", code)
	fmt.Println(response["data"])
}
Output:

{"Uno":"1","Dos":"2","Tres":"3"}

func DoRestPut added in v2.5.3

func DoRestPut[T any](host, endpoint string, request any, headers ...string) (response T, code int, err error)
Example
package main

import (
	"fmt"

	"github.com/horus-es/go-util/v2/errores"
	"github.com/horus-es/go-util/v2/rest"
)

func main() {
	// Servicio REST publico de prueba que devuelve lo mismo que se le envía
	host := "https://httpbin.org"
	endpoint := "anything"
	type tEstructura struct {
		Uno  string
		Dos  string
		Tres string
	}
	request := tEstructura{Uno: "1", Dos: "2", Tres: "3"}
	response, code, err := rest.DoRestPut[map[string]any](host, endpoint, request)
	errores.PanicIfError(err, "Error de red")
	errores.PanicIfTrue(code < 200 || code > 299, "Recibido código de respuesta http: %d", code)
	fmt.Println(response["data"])
}
Output:

{"Uno":"1","Dos":"2","Tres":"3"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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