api

package
v1.136.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package api provides common helpers related to API calls.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallAndCheckSuccess

func CallAndCheckSuccess[T any](ctx context.Context, errorContext string, errorExtractFunc errors.ExtractAPIErrorDescriptionFunc, apiCallFunc APICallFunc[T]) (result *T, err error)

CallAndCheckSuccess is a wrapper for making an API call and then checking success with `CheckAPICallSuccess` errorContext corresponds to the description of what led to the error if error there is e.g. `Failed adding a user`. apiCallFunc corresponds to a generic function that will be called to make the API call. errorExtractFunc is a function for extracting an error message from an API error response.

func CheckAPICallSuccess

func CheckAPICallSuccess(ctx context.Context, errorContext string, errorExtract errors.ExtractAPIErrorDescriptionFunc, resp *http.Response, apiErr error) (err error)

CheckAPICallSuccess verifies whether an API response is successful or not and if not, populates an error with all the information needed. errorContext corresponds to the description of what led to the error if error there is e.g. `Failed adding a user`. resp corresponds to the HTTP response from a certain endpoint. The body of such response is not closed by this function. apiErr corresponds to the error which may be returned by the HTTP client when calling the endpoint.

func GenericCallAndCheckSuccess

func GenericCallAndCheckSuccess[T any](ctx context.Context, errorContext string, errorExtractFunc errors.ExtractAPIErrorDescriptionFunc, apiCallFunc GenericAPICallFunc[T]) (result T, err error)

GenericCallAndCheckSuccess is similar to CallAndCheckSuccess but for functions returning interfaces rather than concrete types. T must be an interface. A conflict error will be returned otherwise. errorContext corresponds to the description of what led to the error if error there is e.g. `Failed adding a user`. apiCallFunc corresponds to a generic function that will be called to make the API call. errorExtractFunc is a function for extracting an error message from an API error response.

func IsCallSuccessful

func IsCallSuccessful(r *http.Response) bool

IsCallSuccessful determines whether an API response is successful or not

Types

type APICallFunc

type APICallFunc[T any] func(ctx context.Context) (*T, *http.Response, error)

APICallFunc defines a generic Web API call with the typical signature of functions generated by the [OpenAPITools generator](https://github.com/OpenAPITools/openapi-generator)

type GenericAPICallFunc

type GenericAPICallFunc[T any] func(ctx context.Context) (T, *http.Response, error)

GenericAPICallFunc is similar to APICallFunc but for types which are more generic such as interfaces rather than concrete types.

Jump to

Keyboard shortcuts

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