request

package
v0.0.35 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package request exposes types and helper methods to create, add, and retrieve request-scoped attributes to context.Context.

Request-scoped attributes include identifiers like the request and correlation IDs. When the request is authenticated, user identifiers like the account and user aggregate IDs can also be added to the context.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextHasAuthenticatedUser

func ContextHasAuthenticatedUser(ctx context.Context) bool

ContextHasAuthenticatedUser returns whether the given context contains an AuthenticatedUser value.

Example
package main

import (
	"context"
	"fmt"

	"github.com/cultureamp/ca-go/x/request"
)

func main() {
	authenticatedUser := request.AuthenticatedUser{
		CustomerAccountID: "123",
		UserID:            "456",
		RealUserID:        "789",
	}
	ctx := context.Background()

	ctx = request.ContextWithAuthenticatedUser(ctx, authenticatedUser)

	ok := request.ContextHasAuthenticatedUser(ctx)
	fmt.Println(ok)

}
Output:

true

func ContextHasRequestIDs

func ContextHasRequestIDs(ctx context.Context) bool

ContextHasRequestIDs returns whether the given context contains a RequestIDs value.

Example
package main

import (
	"context"
	"fmt"

	"github.com/cultureamp/ca-go/x/request"
)

func main() {
	requestIDs := request.RequestIDs{
		RequestID:     "123",
		CorrelationID: "456",
	}
	ctx := context.Background()

	ctx = request.ContextWithRequestIDs(ctx, requestIDs)

	ok := request.ContextHasRequestIDs(ctx)
	fmt.Println(ok)
}
Output:

true

func ContextWithAuthenticatedUser

func ContextWithAuthenticatedUser(parent context.Context, user AuthenticatedUser) context.Context

ContextWithAuthenticatedUser returns a new context with the given user embedded as a value.

Example
package main

import (
	"context"
	"fmt"

	"github.com/cultureamp/ca-go/x/request"
)

func main() {
	authenticatedUser := request.AuthenticatedUser{
		CustomerAccountID: "123",
		UserID:            "456",
		RealUserID:        "789",
	}
	ctx := context.Background()

	ctx = request.ContextWithAuthenticatedUser(ctx, authenticatedUser)

	if authenticatedUserFromContext, ok := request.AuthenticatedUserFromContext(ctx); ok {
		fmt.Println(authenticatedUserFromContext.CustomerAccountID)
		fmt.Println(authenticatedUserFromContext.UserID)
		fmt.Println(authenticatedUserFromContext.RealUserID)

	}
}
Output:

123
456
789

func ContextWithRequestIDs

func ContextWithRequestIDs(ctx context.Context, fields RequestIDs) context.Context

ContextWithRequestIDs returns a new context with the given RequestIDs embedded as a value.

Example
package main

import (
	"context"
	"fmt"

	"github.com/cultureamp/ca-go/x/request"
)

func main() {
	requestIDs := request.RequestIDs{
		RequestID:     "123",
		CorrelationID: "456",
	}
	ctx := context.Background()

	ctx = request.ContextWithRequestIDs(ctx, requestIDs)

	if requestIDsFromContext, ok := request.RequestIDsFromContext(ctx); ok {
		fmt.Println(requestIDsFromContext.RequestID)
		fmt.Println(requestIDsFromContext.CorrelationID)

	}
}
Output:

123
456

Types

type AuthenticatedUser

type AuthenticatedUser struct {
	// CustomerAccountID is the ID of the currently logged in user's parent
	// account/organization, sometimes known as the "account_aggregate_id".
	CustomerAccountID string
	// UserID is the ID of the currently authenticated user, and will
	// generally be a "user_aggregate_id".
	UserID string
	// RealUserID, when supplied, is the ID of the user who is currently
	// impersonating the current "UserID". This value is optional.
	RealUserID string
}

AuthenticatedUser holds the identifiers of a user making an authenticated request.

func AuthenticatedUserFromContext

func AuthenticatedUserFromContext(ctx context.Context) (AuthenticatedUser, bool)

AuthenticatedUserFromContext attempts to retrieve an AuthenticatedUser from the given context, returning an AuthenticatedUser along with a boolean signalling whether the retrieval was successful.

type RequestIDs

type RequestIDs struct {
	RequestID     string
	CorrelationID string
}

RequestIDs represent the set of unique identifiers for a request.

func RequestIDsFromContext

func RequestIDsFromContext(ctx context.Context) (RequestIDs, bool)

RequestIDsFromContext attempts to retrieve a RequestIDs struct from the given context, returning a RequestIDs struct along with a boolean signalling whether the retrieval was successful.

Jump to

Keyboard shortcuts

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