internal

package
v0.0.0-...-ab73821 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package internal contains functionality that is only accessible from within the Admin SDK.

Index

Constants

This section is empty.

Variables

View Source
var FirebaseScopes = []string{
	"https://www.googleapis.com/auth/cloud-platform",
	"https://www.googleapis.com/auth/datastore",
	"https://www.googleapis.com/auth/devstorage.full_control",
	"https://www.googleapis.com/auth/firebase",
	"https://www.googleapis.com/auth/identitytoolkit",
	"https://www.googleapis.com/auth/userinfo.email",
}

FirebaseScopes is the set of OAuth2 scopes used by the Admin SDK.

View Source
var SystemClock = &systemClock{}

SystemClock is a clock that returns local time of the system.

Functions

func HasPlatformErrorCode

func HasPlatformErrorCode(err error, code ErrorCode) bool

HasPlatformErrorCode checks if the given error contains a specific error code.

func HasSuccessStatus

func HasSuccessStatus(r *Response) bool

HasSuccessStatus returns true if the response status code is in the 2xx range.

Types

type AuthConfig

type AuthConfig struct {
	Opts             []option.ClientOption
	ProjectID        string
	ServiceAccountID string
	Version          string
}

AuthConfig represents the configuration of Firebase Auth service.

type Clock

type Clock interface {
	Now() time.Time
}

Clock is used to query the current local time.

type CreateErrFn

type CreateErrFn func(r *Response) error

CreateErrFn is a function that creates an error from a given Response.

type DatabaseConfig

type DatabaseConfig struct {
	Opts         []option.ClientOption
	URL          string
	Version      string
	AuthOverride map[string]interface{}
}

DatabaseConfig represents the configuration of Firebase Database service.

type ErrorCode

type ErrorCode string

ErrorCode represents the platform-wide error codes that can be raised by Admin SDK APIs.

const (
	// InvalidArgument is a OnePlatform error code.
	InvalidArgument ErrorCode = "INVALID_ARGUMENT"

	// FailedPrecondition is a OnePlatform error code.
	FailedPrecondition ErrorCode = "FAILED_PRECONDITION"

	// OutOfRange is a OnePlatform error code.
	OutOfRange ErrorCode = "OUT_OF_RANGE"

	// Unauthenticated is a OnePlatform error code.
	Unauthenticated ErrorCode = "UNAUTHENTICATED"

	// PermissionDenied is a OnePlatform error code.
	PermissionDenied ErrorCode = "PERMISSION_DENIED"

	// NotFound is a OnePlatform error code.
	NotFound ErrorCode = "NOT_FOUND"

	// Conflict is a custom error code that represents HTTP 409 responses.
	//
	// OnePlatform APIs typically respond with ABORTED or ALREADY_EXISTS explicitly. But a few
	// old APIs send HTTP 409 Conflict without any additional details to distinguish between the two
	// cases. For these we currently use this error code. As more APIs adopt OnePlatform conventions
	// this will become less important.
	Conflict ErrorCode = "CONFLICT"

	// Aborted is a OnePlatform error code.
	Aborted ErrorCode = "ABORTED"

	// AlreadyExists is a OnePlatform error code.
	AlreadyExists ErrorCode = "ALREADY_EXISTS"

	// ResourceExhausted is a OnePlatform error code.
	ResourceExhausted ErrorCode = "RESOURCE_EXHAUSTED"

	// Cancelled is a OnePlatform error code.
	Cancelled ErrorCode = "CANCELLED"

	// DataLoss is a OnePlatform error code.
	DataLoss ErrorCode = "DATA_LOSS"

	// Unknown is a OnePlatform error code.
	Unknown ErrorCode = "UNKNOWN"

	// Internal is a OnePlatform error code.
	Internal ErrorCode = "INTERNAL"

	// Unavailable is a OnePlatform error code.
	Unavailable ErrorCode = "UNAVAILABLE"

	// DeadlineExceeded is a OnePlatform error code.
	DeadlineExceeded ErrorCode = "DEADLINE_EXCEEDED"
)

type FirebaseError

type FirebaseError struct {
	ErrorCode ErrorCode
	String    string
	Response  *http.Response
	Ext       map[string]interface{}
}

FirebaseError is an error type containing an error code string.

func NewFirebaseError

func NewFirebaseError(resp *Response) *FirebaseError

NewFirebaseError creates a new error from the given HTTP response.

func NewFirebaseErrorOnePlatform

func NewFirebaseErrorOnePlatform(resp *Response) *FirebaseError

NewFirebaseErrorOnePlatform parses the response payload as a GCP error response and create an error from the details extracted.

If the response failes to parse, or otherwise doesn't provide any useful details NewFirebaseErrorOnePlatform creates an error with some sensible defaults.

func (*FirebaseError) Error

func (fe *FirebaseError) Error() string

type HTTPClient

type HTTPClient struct {
	Client      *http.Client
	RetryConfig *RetryConfig
	CreateErrFn CreateErrFn
	SuccessFn   SuccessFn
	Opts        []HTTPOption
}

HTTPClient is a convenient API to make HTTP calls.

This API handles repetitive tasks such as entity serialization and deserialization when making HTTP calls. It provides a convenient mechanism to set headers and query parameters on outgoing requests, while enforcing that an explicit context is used per request. Responses returned by HTTPClient can be easily unmarshalled as JSON.

HTTPClient also handles automatically retrying failed HTTP requests.

func NewHTTPClient

func NewHTTPClient(ctx context.Context, opts ...option.ClientOption) (*HTTPClient, string, error)

NewHTTPClient creates a new HTTPClient using the provided client options and the default RetryConfig.

NewHTTPClient returns the created HTTPClient along with the target endpoint URL. The endpoint is obtained from the client options passed into the function.

func WithDefaultRetryConfig

func WithDefaultRetryConfig(hc *http.Client) *HTTPClient

WithDefaultRetryConfig creates a new HTTPClient using the provided client and the default RetryConfig.

The default RetryConfig retries requests on all low-level network errors as well as on HTTP ServiceUnavailable (503) error. Repeatedly failing requests are retried up to 4 times with exponential backoff. Retry delay is never longer than 2 minutes.

func (*HTTPClient) Do

func (c *HTTPClient) Do(ctx context.Context, req *Request) (*Response, error)

Do executes the given Request, and returns a Response.

If a RetryConfig is specified on the client, Do attempts to retry failing requests.

If SuccessFn is set on the client or on the request, the response is validated against that function. If this validation fails, returns an error. These errors are created using the CreateErrFn on the client or on the request. If neither is set, CreatePlatformError is used as the default error function.

func (*HTTPClient) DoAndUnmarshal

func (c *HTTPClient) DoAndUnmarshal(ctx context.Context, req *Request, v interface{}) (*Response, error)

DoAndUnmarshal behaves similar to Do, but additionally unmarshals the response payload into the given pointer.

Unmarshal takes place only if the response does not represent an error (as determined by the Do function) and v is not nil. If the unmarshal fails, an error is returned even if the original response indicated success.

type HTTPEntity

type HTTPEntity interface {
	Bytes() ([]byte, error)
	Mime() string
}

HTTPEntity represents a payload that can be included in an outgoing HTTP request.

func NewJSONEntity

func NewJSONEntity(v interface{}) HTTPEntity

NewJSONEntity creates a new HTTPEntity that will be serialized into JSON.

type HTTPOption

type HTTPOption func(*http.Request)

HTTPOption is an additional parameter that can be specified to customize an outgoing request.

func WithHeader

func WithHeader(key, value string) HTTPOption

WithHeader creates an HTTPOption that will set an HTTP header on the request.

func WithQueryParam

func WithQueryParam(key, value string) HTTPOption

WithQueryParam creates an HTTPOption that will set a query parameter on the request.

func WithQueryParams

func WithQueryParams(qp map[string]string) HTTPOption

WithQueryParams creates an HTTPOption that will set all the entries of qp as query parameters on the request.

type HashConfig

type HashConfig map[string]interface{}

HashConfig represents a hash algorithm configuration used to generate password hashes.

type InstanceIDConfig

type InstanceIDConfig struct {
	Opts      []option.ClientOption
	ProjectID string
}

InstanceIDConfig represents the configuration of Firebase Instance ID service.

type MessagingConfig

type MessagingConfig struct {
	Opts      []option.ClientOption
	ProjectID string
	Version   string
}

MessagingConfig represents the configuration of Firebase Cloud Messaging service.

type MockClock

type MockClock struct {
	Timestamp time.Time
}

MockClock can be used to mock current time during tests.

func (*MockClock) Now

func (m *MockClock) Now() time.Time

Now returns the timestamp set in the MockClock.

type MockTokenSource

type MockTokenSource struct {
	AccessToken string
}

MockTokenSource is a TokenSource implementation that can be used for testing.

func (*MockTokenSource) Token

func (ts *MockTokenSource) Token() (*oauth2.Token, error)

Token returns the test token associated with the TokenSource.

type Request

type Request struct {
	Method      string
	URL         string
	Body        HTTPEntity
	Opts        []HTTPOption
	SuccessFn   SuccessFn
	CreateErrFn CreateErrFn
}

Request contains all the parameters required to construct an outgoing HTTP request.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
	// contains filtered or unexported fields
}

Response contains information extracted from an HTTP response.

func (*Response) LowLevelResponse

func (r *Response) LowLevelResponse() *http.Response

LowLevelResponse returns an http.Response that represents the underlying low-level HTTP response.

This always returns a buffered copy of the original HTTP response. Body can be read from the returned response with no impact on the underlying HTTP connection. Closing the Body on the returned response is a No-op.

type RetryCondition

type RetryCondition func(resp *http.Response, networkErr error) bool

RetryCondition determines if an HTTP request should be retried depending on its last outcome.

type RetryConfig

type RetryConfig struct {
	MaxRetries       int
	CheckForRetry    RetryCondition
	ExpBackoffFactor float64
	MaxDelay         *time.Duration
}

RetryConfig specifies how the HTTPClient should retry failing HTTP requests.

A request is never retried more than MaxRetries times. If CheckForRetry is nil, all network errors, and all 400+ HTTP status codes are retried. If an HTTP error response contains the Retry-After header, it is always respected. Otherwise retries are delayed with exponential backoff. Set ExpBackoffFactor to 0 to disable exponential backoff, and retry immediately after each error.

If MaxDelay is set, retries delay gets capped by that value. If the Retry-After header requires a longer delay than MaxDelay, retries are not attempted.

type StorageConfig

type StorageConfig struct {
	Opts   []option.ClientOption
	Bucket string
}

StorageConfig represents the configuration of Google Cloud Storage service.

type SuccessFn

type SuccessFn func(r *Response) bool

SuccessFn is a function that checks if a Response indicates success.

Jump to

Keyboard shortcuts

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