mcias/client/client.go

75 lines
1.6 KiB
Go

// Package client provides a Go client for interacting with the MCIAS (Metacircular Identity and Access System).
package client
import (
"net/http"
"time"
)
// Client is the main struct for interacting with the MCIAS API.
type Client struct {
// BaseURL is the base URL of the MCIAS server.
BaseURL string
// HTTPClient is the HTTP client used for making requests.
HTTPClient *http.Client
// Token is the authentication token.
Token string
// Username is the authenticated username.
Username string
}
// ClientOption is a function that configures a Client.
type ClientOption func(*Client)
// WithBaseURL sets the base URL for the client.
func WithBaseURL(baseURL string) ClientOption {
return func(c *Client) {
c.BaseURL = baseURL
}
}
// WithHTTPClient sets the HTTP client for the client.
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *Client) {
c.HTTPClient = httpClient
}
}
// WithToken sets the authentication token for the client.
func WithToken(token string) ClientOption {
return func(c *Client) {
c.Token = token
}
}
// WithUsername sets the username for the client.
func WithUsername(username string) ClientOption {
return func(c *Client) {
c.Username = username
}
}
// NewClient creates a new MCIAS client with the given options.
func NewClient(options ...ClientOption) *Client {
client := &Client{
BaseURL: "http://localhost:8080",
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
}
for _, option := range options {
option(client)
}
return client
}
// IsAuthenticated returns true if the client has a token.
func (c *Client) IsAuthenticated() bool {
return c.Token != ""
}