example

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrUserNotFound indicates that a requested user was not found.
	// Typically returned when a user ID doesn't exist in the system.
	ErrUserNotFound sentinelError = "user not found"

	// ErrUnauthorized indicates that the request lacks valid authentication.
	// Typically returned for 401 Unauthorized HTTP responses.
	ErrUnauthorized sentinelError = "unauthorized"
)

Predefined sentinel errors for common API error conditions. These errors provide type-safe, comparable error values that can be used with errors.Is() for error handling and flow control.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client provides a high-level interface for interacting with a user management API. It encapsulates the httpclient.API instance and provides domain-specific methods for common operations like creating, retrieving, and deleting users.

The client is designed to be safe for concurrent use by multiple goroutines, as the underlying httpclient.API is thread-safe and creates new request builders for each operation.

func New

func New(serverURL url.URL) (*Client, error)

New creates a new Client instance configured to communicate with the API server at the specified URL. The client is configured with sensible defaults for common HTTP status codes and error handling patterns.

Default response handlers:

  • 200 OK: Treated as success (no error)
  • 401 Unauthorized: Returns ErrUnauthorized sentinel error
  • 404 Not Found: Returns ErrUserNotFound sentinel error

The client uses http.DefaultClient as the underlying HTTP client, which provides reasonable defaults for most use cases. For production applications, consider configuring a custom HTTP client with appropriate timeouts, connection pooling, and other settings.

The returned client is safe for concurrent use by multiple goroutines.

func (*Client) CreateUser

func (c *Client) CreateUser(ctx context.Context, userName string) (UserID, error)

CreateUser creates a new user in the system with the specified username. It sends a POST request to the /users endpoint with a JSON body containing the user creation data and returns the newly assigned user ID.

func (*Client) DeleteUserByID

func (c *Client) DeleteUserByID(ctx context.Context, userID UserID) error

DeleteUserByID removes a user from the system by their unique identifier. It sends a DELETE request to the /users/{userID} endpoint and relies on the default response handlers for error handling.

This method uses the Execute() convenience method instead of Do() because DELETE operations typically don't return response bodies that need parsing. The Execute() method applies all default response handlers and returns only the error result.

func (*Client) GetUserByID

func (c *Client) GetUserByID(ctx context.Context, userID UserID) (*User, error)

GetUserByID retrieves a user from the system by their unique identifier. It sends a GET request to the /users/{userID} endpoint and returns the user information as a domain model object.

type User

type User struct {
	ID   UserID `json:"id"`
	Name string `json:"name"`
}

User represents a user entity in the system, containing the essential information needed for user management operations through the API.

type UserID

type UserID uint64

UserID represents a unique identifier for a user in the system. It's defined as a custom type based on uint64 to provide type safety and prevent accidental mixing of user IDs with other numeric values.

func (UserID) String

func (id UserID) String() string

String implements the fmt.Stringer interface for UserID, providing a string representation of the user ID for logging, debugging, and API parameter formatting.

Jump to

Keyboard shortcuts

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