Documentation
¶
Overview ¶
Package lcsc provides an unofficial Go client for LCSC component data.
Endpoints are organized into service groups:
- client.Search - keyword search
- client.Product - product details
LCSC does not provide an official public API for this data. This package uses undocumented endpoints discovered from the web application and they can change without notice.
Index ¶
- Constants
- Variables
- type APIError
- type Cache
- type CacheConfig
- type Client
- type ClientOption
- func WithBaseURL(baseURL string) ClientOption
- func WithCache(cache Cache) ClientOption
- func WithCacheConfig(config CacheConfig) ClientOption
- func WithCurrency(currency string) ClientOption
- func WithHTTPClient(httpClient *http.Client) ClientOption
- func WithRateLimit(rps float64) ClientOption
- func WithRetryConfig(config RetryConfig) ClientOption
- func WithoutCache() ClientOption
- func WithoutRetry() ClientOption
- type FlexFloat64
- type MemoryCache
- type Parameter
- type PriceBreak
- type Product
- type ProductService
- type RateLimiter
- type RetryConfig
- type SearchRequest
- type SearchResponse
- type SearchService
Constants ¶
const Version = "1.0.0"
Version is the current version of the go-lcsc package.
Variables ¶
var ( // ErrInvalidRequest indicates an invalid client request. ErrInvalidRequest = errors.New("lcsc: invalid request") // ErrNotFound indicates the requested resource was not found. ErrNotFound = errors.New("lcsc: not found") // ErrRateLimited indicates the API rate limit has been exceeded. ErrRateLimited = errors.New("lcsc: rate limit exceeded") // ErrServer indicates a server-side API failure. ErrServer = errors.New("lcsc: server error") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Get(key string) ([]byte, bool)
Set(key string, value []byte, ttl time.Duration)
Delete(key string)
}
Cache defines the interface for caching API responses.
type CacheConfig ¶
type CacheConfig struct {
Enabled bool
SearchTTL time.Duration // TTL for search results (default 5 min)
DetailsTTL time.Duration // TTL for product details (default 10 min)
}
CacheConfig configures caching behavior.
func DefaultCacheConfig ¶
func DefaultCacheConfig() CacheConfig
DefaultCacheConfig returns the default cache configuration.
type Client ¶
type Client struct {
Search *SearchService
Product *ProductService
// contains filtered or unexported fields
}
Client is an LCSC API client.
func NewClient ¶
func NewClient(opts ...ClientOption) *Client
NewClient creates a new LCSC API client.
func (*Client) ClearCache ¶ added in v1.0.0
func (c *Client) ClearCache()
ClearCache clears all cached responses in the default memory cache.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithBaseURL ¶
func WithBaseURL(baseURL string) ClientOption
WithBaseURL sets a custom base URL.
func WithCache ¶
func WithCache(cache Cache) ClientOption
WithCache sets a custom cache implementation.
func WithCacheConfig ¶ added in v1.0.0
func WithCacheConfig(config CacheConfig) ClientOption
WithCacheConfig sets the cache configuration.
func WithCurrency ¶
func WithCurrency(currency string) ClientOption
WithCurrency sets the currency for price responses.
func WithHTTPClient ¶
func WithHTTPClient(httpClient *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client.
func WithRateLimit ¶
func WithRateLimit(rps float64) ClientOption
WithRateLimit sets a custom rate limit (requests per second).
func WithRetryConfig ¶
func WithRetryConfig(config RetryConfig) ClientOption
WithRetryConfig sets the retry configuration.
func WithoutCache ¶ added in v1.0.0
func WithoutCache() ClientOption
WithoutCache disables response caching.
type FlexFloat64 ¶
type FlexFloat64 float64
FlexFloat64 handles JSON values that may be either a number or a string.
func (*FlexFloat64) UnmarshalJSON ¶
func (f *FlexFloat64) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for FlexFloat64.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is a simple in-memory cache with TTL support.
func NewMemoryCache ¶
func NewMemoryCache(defaultTTL time.Duration) *MemoryCache
NewMemoryCache creates a new in-memory cache with the specified default TTL.
func (*MemoryCache) Close ¶ added in v0.1.2
func (c *MemoryCache) Close()
Close stops the cleanup goroutine.
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(key string)
Delete removes a value from the cache.
func (*MemoryCache) Get ¶
func (c *MemoryCache) Get(key string) ([]byte, bool)
Get retrieves a value from the cache.
func (*MemoryCache) Set ¶
func (c *MemoryCache) Set(key string, value []byte, ttl time.Duration)
Set stores a value in the cache with the specified TTL. If ttl is 0, the default TTL is used.
func (*MemoryCache) Size ¶
func (c *MemoryCache) Size() int
Size returns the number of entries in the cache.
type Parameter ¶
type Parameter struct {
ParamNameEn string `json:"paramNameEn"`
ParamValueEn string `json:"paramValueEn"`
}
Parameter represents a product specification.
type PriceBreak ¶
type PriceBreak struct {
Ladder int `json:"ladder"`
ProductPrice FlexFloat64 `json:"productPrice"`
CurrencySymbol string `json:"currencySymbol"`
}
PriceBreak represents a quantity-based price tier.
type Product ¶
type Product struct {
ProductCode string `json:"productCode"`
ProductModel string `json:"productModel"`
BrandNameEn string `json:"brandNameEn"`
ProductIntroEn string `json:"productIntroEn"`
PdfURL string `json:"pdfUrl"`
ProductImages []string `json:"productImages"`
ProductImageURL string `json:"productImageUrl"`
StockNumber int `json:"stockNumber"`
MinPacketNumber int `json:"minPacketNumber"`
ProductPriceList []PriceBreak `json:"productPriceList"`
ParamVOList []Parameter `json:"paramVOList"`
EncapStandard string `json:"encapStandard"`
ParentCatalogName string `json:"parentCatalogName"`
CatalogName string `json:"catalogName"`
Weight float64 `json:"weight"`
}
Product represents an LCSC component.
func (*Product) GetProductURL ¶
GetProductURL returns the LCSC product page URL.
type ProductService ¶ added in v1.0.0
type ProductService service
ProductService handles product-detail operations.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a token bucket rate limiter.
func NewRateLimiter ¶
func NewRateLimiter(requestsPerSecond float64) *RateLimiter
NewRateLimiter creates a new rate limiter with the specified requests per second.
type RetryConfig ¶
type RetryConfig struct {
MaxRetries int // Maximum number of retry attempts (default 3)
InitialBackoff time.Duration // Initial backoff duration (default 500ms)
MaxBackoff time.Duration // Maximum backoff duration (default 30s)
Multiplier float64 // Backoff multiplier (default 2.0)
Jitter float64 // Random jitter factor 0-1 (default 0.1)
}
RetryConfig configures retry behavior.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns the default retry configuration.
type SearchRequest ¶
type SearchRequest struct {
Keyword string
}
SearchRequest contains parameters for product search.
type SearchResponse ¶
SearchResponse contains product search results.
type SearchService ¶ added in v1.0.0
type SearchService service
SearchService handles product search operations.
func (*SearchService) Keyword ¶ added in v1.0.0
func (s *SearchService) Keyword(ctx context.Context, req *SearchRequest) (*SearchResponse, error)
Keyword searches for products by keyword.