callable

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 10 Imported by: 0

README

Firebase Callable

GoPkg Widget build Go Report Card

A simple implementation of the Firebase https.onCall protocol for Go cloud functions.

Install

go get -u github.com/lonebits/callable

Usage

package greeting

import (
	"context"
	"github.com/lonebits/callable"
	"net/http"
)

// Create types to represent input and output data:
//

type greetingRequest struct {
	Who string `json:"who"`
}

type greetingResponse struct {
	Greeting string `json:"greeting"`
}

// Implement the callable.Request interface:
//

func (r *greetingRequest) Handle(ctx context.Context, call callable.Call) (interface{}, error) {
	return greetingResponse{
		Greeting: "Hello " + r.Who,
	}, nil
}

// Use callable to implement the function:
//

func Greeting(w http.ResponseWriter, r *http.Request) {
	callable.New(&greetingRequest{}).ServeHTTP(w, r)
}
Authentication

The WithAuth option can be used to enable ID Token validation. Firebase Auth user ID is then passed inside callable.Call.UID.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(code StatusCode, format string, a ...interface{}) error

Error creates a new callable error with the specified status and message.

Types

type Call

type Call struct {
	// UID is the client's Firebase Auth user ID. See WithAuth to set up
	// Firebase Auth token validation.
	UID string
	// IID is the Firebase Instance ID token (the FCM registration token).
	// Can be used to target push notifications.
	IID string
}

Call carries contextual information on the current function call.

type Callable

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

Callable encapsulates a Firebase 'onCall' request with its handler method.

func New

func New(request Request, opts ...Option) *Callable

New creates a new Callable for the given Request object. When serving a HTTP request, the Callable will use the Request object as a target for JSON decoder. After the request body is unmarshalled, the Handle method is called to process the call. Returned value is encoded as JSON and sent back to the client.

func (*Callable) ServeHTTP

func (c *Callable) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Callable.

func WithAuth

func WithAuth(client *auth.Client, required bool) Option

WithAuth specifies the Firebase Auth client that will be used to validate ID tokens. The `required` flag controls whether a valid ID token is required or not.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger adds a logger that will be used to log errors.

type Request

type Request interface {
	// Handle is called after the JSON request body is decoded into this
	// Request. Returned values are encoded to JSON response.
	//
	// Use the Error function to create errors with a specified status code.
	// Generic errors are encoded as an 'INTERNAL' error response.
	Handle(context.Context, Call) (interface{}, error)
}

Request represents the call request data and provides a method to handle it.

type StatusCode

type StatusCode int

StatusCode distinguishes between different error causes. See https://cloud.google.com/apis/design/errors#http_mapping.

const (
	InvalidArgument StatusCode = iota
	FailedPrecondition
	OutOfRange
	Unauthenticated
	PermissionDenied
	NotFound
	Aborted
	AlreadyExists
	ResourceExhausted
	Cancelled
	DataLoss
	Unknown
	Internal
	NotImplemented
	Unavailable
	DeadlineExceeded
)

Jump to

Keyboard shortcuts

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