alpinebits

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

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 17 Imported by: 0

README

github.com/HGV/alpinebits-http

Go Reference github.com/HGV/alpinebits

A Go library that supports all modern versions of the AlpineBits standard, providing seamless integration and interoperability.

Installation

go get github.com/HGV/alpinebits

Usage

Routing
package main

import (
    "net/http"

    "github.com/HGV/alpinebits/v_2018_10"
    "github.com/HGV/alpinebits/v_2020_10"
    "github.com/go-chi/chi/v5"
)

func main() {
    r := chi.NewRouter()
    r.Mount("/", alpinebitsRouter())
    http.ListenAndServe(":8080", r)
}

func alpinebitsRouter() http.Handler {
    r := alpinebits.NewRouter()

    v201810, _ := v_2018_10.NewVersion()
    r.Version(v201810, func(s *Subrouter) {
        s.Action(v_2018_10.ActionHotelAvailNotif, pushHotelAvailNotif)
    })

    v202010, _ := v_2020_10.NewVersion()
    r.Version(v202010, func(s *Subrouter) {
        s.Action(v_2020_10.ActionHotelInvCountNotif, pushHotelInvCountNotif, alpinebits.WithCapabilities(
            v_2020_10.CapabilityHotelInvCountNotifAcceptRooms,
            v_2020_10.CapabilityHotelInvCountNotifAcceptDeltas,
            v_2020_10.CapabilityHotelInvCountNotifAcceptOutOfOrder,
            v_2020_10.CapabilityHotelInvCountNotifAcceptOutOfMarket,
            v_2020_10.CapabilityHotelInvCountNotifAcceptClosingSeasons,
        ))
    })
}

func pushHotelAvailNotif(r Request) (any, error) {
    return nil, nil
}

func pushHotelInvCountNotif(r Request) (any, error) {
    return nil, nil
}
Validation
import "github.com/HGV/alpinebits/v_2018_10/freerooms"

validator := freerooms.NewHotelAvailNotifValidator(
    freerooms.WithRooms(true, &map[string]map[string]struct{}{
        "DZ": {"101": {}, "102": {}},
    }),
    freerooms.WithDeltas(true),
    freerooms.WithBookingThreshold(true),
)
err := validator.Validate(hotelAvailNotifRQ)
Handshake & Client Request
handshakeConfig := alpinebits.HandshakeClientConfig{
    // client supported versions, actions and capabilities
    HandshakeData: HandshakeData{
        "2020-10": map[string][]string{
            "action_OTA_Ping": nil,
            "action_OTA_HotelInvCountNotif": {
                "OTA_HotelInvCountNotif_accept_rooms",
                "OTA_HotelInvCountNotif_accept_deltas",
                "OTA_HotelInvCountNotif_accept_out_of_order",
                "OTA_HotelInvCountNotif_accept_out_of_market",
                "OTA_HotelInvCountNotif_accept_closing_seasons",
            },
        },
        "2018-10": map[string][]string{
            "action_OTA_Ping": nil,
        },
    },
}
handshakeClient, _ := alpinebits.NewHandshakeClient(handshakeConfig)
handshakeData, _, _ := handshakeClient.Ping(context.TODO())

// Use one of the versions specified in `handshakeData` as needed.
// `NegotiatedVersion()` selects the highest version supported by both
// the client and server.
switch version, actions := handshakeData.NegotiatedVersion(); version {
case "2020-10":
    client, _ := v_2020_10.NewClient(v_2020_10.ClientConfig{
        NegotiatedVersion: actions,
    })
case "2018-10":
    client, _ := v_2018_10.NewClient(v_2018_10.ClientConfig{
        NegotiatedVersion: actions,
    })
}

Testing

[!IMPORTANT] Ensure libxml2 and libxml2-dev are installed before running the tests. You can install them using:

sudo apt install libxml2 libxml2-dev

Run all tests:

go test ./...

Documentation

Index

Constants

View Source
const (
	HeaderServerAcceptEncoding  = "X-AlpineBits-Server-Accept-Encoding"
	HeaderClientID              = "X-AlpineBits-ClientID"
	HeaderClientProtocolVersion = "X-AlpineBits-ClientProtocolVersion"
)

Variables

This section is empty.

Functions

func WithRouteContext

func WithRouteContext(ctx context.Context, rctx RouteContext) context.Context

Types

type HandlerFunc

type HandlerFunc func(r Request) (any, error)

type HandshakeClient

type HandshakeClient struct {
	// contains filtered or unexported fields
}

func NewHandshakeClient

func NewHandshakeClient(config HandshakeClientConfig) (*HandshakeClient, error)

func (*HandshakeClient) Ping

type HandshakeClientConfig

type HandshakeClientConfig struct {
	URL           string
	Username      string
	Password      string
	ClientID      string
	HandshakeData HandshakeData
}

type HandshakeData

type HandshakeData map[string]map[string][]string

func NewHandshakeDataFromRouter

func NewHandshakeDataFromRouter(r Router) HandshakeData

func (HandshakeData) Intersect

func (h HandshakeData) Intersect(other HandshakeData) HandshakeData

func (HandshakeData) MarshalJSON

func (h HandshakeData) MarshalJSON() ([]byte, error)

func (HandshakeData) NegotiatedVersion

func (h HandshakeData) NegotiatedVersion() (string, map[string][]string)

func (*HandshakeData) UnmarshalJSON

func (h *HandshakeData) UnmarshalJSON(data []byte) error

type Request

type Request struct {
	Context      context.Context
	ClientID     string
	Data         any
	Capabilities []string
	// contains filtered or unexported fields
}

func (Request) HandshakeData

func (r Request) HandshakeData() HandshakeData

type Route

type Route struct {
	// contains filtered or unexported fields
}

type RouteContext

type RouteContext struct {
	HandshakeDataOverride HandshakeData
}

func RouteContextFrom

func RouteContextFrom(ctx context.Context) (RouteContext, bool)

type RouteFunc

type RouteFunc func(*Route)

func WithCapabilities

func WithCapabilities[C ~string](caps ...C) RouteFunc

func WithExcludeFromHandshake

func WithExcludeFromHandshake() RouteFunc

type Router

type Router struct {
	http.Handler
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) Version

func (r *Router) Version(version version.Version[version.Action], fn func(s *Subrouter)) *Router

type Routes

type Routes struct {
	// contains filtered or unexported fields
}

type Subrouter

type Subrouter struct {
	// contains filtered or unexported fields
}

func (*Subrouter) Action

func (s *Subrouter) Action(action version.Action, handlerFn HandlerFunc, opts ...RouteFunc)

Jump to

Keyboard shortcuts

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