Documentation
¶
Overview ¶
Package collection provides an anonymous, read-only client for Bangumi public user collections.
A Client is safe for concurrent use after construction. Fetch retrieves all pages and returns a canonical order; FetchPage preserves one upstream page's order. Requests never send Authorization or Cookie headers.
Bangumi collection records require tags but may omit comment and the nested subject projection. An omitted comment becomes the empty string; an omitted subject keeps ID equal to SubjectID and leaves Name and NameCn empty. Present optional fields must still contain a valid non-null value.
Subject types follow the official mapping: Book 1, Anime 2, Music 3, Game 4, and Real 6. Music and Game intentionally correct the reversed names in the untagged prototype before the first public version.
The first public contract retains NewClient, Fetch, FetchPage, collection enum values, and existing non-authentication options. It intentionally removes WithAccessToken, rejects an empty Fetch collection-type list, extends Subject to the complete collection DTO, and keeps HTTPError.Body empty. New options configure a test endpoint, a shared rate limit, and a maximum retry delay.
This repository is preparing the first v0.1.0 contract. The package has not been tagged or published by this change.
Index ¶
- Variables
- type Client
- type CollectionType
- type DecodeError
- type HTTPError
- type NetworkError
- type Option
- func WithConcurrencyLimit(limit int) Option
- func WithEndpoint(endpoint string) Option
- func WithHTTPClient(client *http.Client) Option
- func WithMaxRetries(maxRetries int) Option
- func WithMaxRetryDelay(delay time.Duration) Option
- func WithRateLimit(requestsPerSecond float64, burst int) Option
- func WithRequestTimeout(timeout time.Duration) Option
- func WithRetryInterval(interval time.Duration) Option
- type PageResult
- type ProtocolError
- type RetryError
- type Subject
- type SubjectType
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidUserID = errors.New("invalid user id") ErrForbidden = errors.New("forbidden") ErrRateLimited = errors.New("rate limited") ErrServerError = errors.New("server error") ErrEmptyUserID = errors.New("user id cannot be empty") ErrNilContext = errors.New("nil context") ErrNoCollectionTypes = errors.New("no collection types") ErrInvalidSubjectType = errors.New("invalid subject type") ErrInvalidCollectionType = errors.New("invalid collection type") ErrInvalidConfiguration = errors.New("invalid client configuration") ErrNotFound = errors.New("not found") ErrHTTPStatus = errors.New("unexpected http status") ErrTransport = errors.New("transport failure") ErrTimeout = errors.New("request timeout") ErrCanceled = errors.New("request canceled") ErrDecode = errors.New("response decode failure") ErrProtocol = errors.New("response protocol violation") ErrResponseTooLarge = errors.New("response too large") ErrRetryExhausted = errors.New("retry exhausted") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client retrieves Bangumi public collection data.
Configuration is fixed when NewClient returns. The same Client shares its request-rate and in-flight limits across all Fetch and FetchPage calls.
func NewClient ¶
NewClient creates an anonymous Bangumi public-collection client.
userAgent is required and must be valid UTF-8, contain no control rune, and be between 1 and 256 bytes. Since this constructor preserves its historical signature, invalid configuration is retained on the Client and returned as ErrInvalidConfiguration by every operation before transport.
func (*Client) Fetch ¶
func (c *Client) Fetch( ctx context.Context, userID string, subjectType SubjectType, collectionTypes ...CollectionType, ) ([]*Subject, error)
Fetch retrieves all planned pages for the requested collection states.
Repeated states are normalized. Results are deduplicated and sorted by (SubjectType, SubjectID, Type), independent of completion order.
func (*Client) FetchPage ¶
func (c *Client) FetchPage( ctx context.Context, userID string, subjectType SubjectType, collectionType CollectionType, limit int, offset int, ) (*PageResult, error)
FetchPage retrieves one validated page. limit is clamped to 1..50 and a negative offset is clamped to zero for compatibility.
type CollectionType ¶
type CollectionType int
CollectionType is a user's collection state.
const ( CollectionTypeWish CollectionType = 1 CollectionTypeDone CollectionType = 2 CollectionTypeDoing CollectionType = 3 CollectionTypeOnHold CollectionType = 4 CollectionTypeDropped CollectionType = 5 )
type DecodeError ¶
type DecodeError struct {
// contains filtered or unexported fields
}
DecodeError indicates that a bounded success body could not be read or was not exactly one JSON value. It intentionally exposes no upstream content.
func (*DecodeError) Error ¶
func (e *DecodeError) Error() string
func (*DecodeError) Unwrap ¶
func (e *DecodeError) Unwrap() error
type HTTPError ¶
HTTPError describes a non-200 response without retaining response content. Body remains for source compatibility and is always empty on returned errors.
type NetworkError ¶
NetworkError describes a sanitized runtime request failure.
Returned Err values are restricted to context.Canceled, context.DeadlineExceeded, or ErrTransport.
func (*NetworkError) Error ¶
func (e *NetworkError) Error() string
func (*NetworkError) Is ¶
func (e *NetworkError) Is(target error) bool
func (*NetworkError) Unwrap ¶
func (e *NetworkError) Unwrap() error
type Option ¶
type Option func(*Client)
Option configures a Client before it becomes available to callers.
func WithConcurrencyLimit ¶
WithConcurrencyLimit sets the maximum number of in-flight requests shared by all operations on one Client. Non-positive values preserve the default.
func WithEndpoint ¶
WithEndpoint replaces the API root. It accepts an absolute HTTPS root, or an HTTP loopback root for local tests. Invalid values poison the Client with ErrInvalidConfiguration.
func WithHTTPClient ¶
WithHTTPClient supplies a custom HTTP client.
The supplied value is shallow-cloned. The package-owned clone has no cookie jar, has no Client.Timeout, and refuses redirects at the first response. WithRequestTimeout remains authoritative for every attempt regardless of option order. A nil client preserves the default for compatibility.
func WithMaxRetries ¶
WithMaxRetries sets retries after the initial attempt. A negative value preserves the default; zero disables retries.
func WithMaxRetryDelay ¶
WithMaxRetryDelay caps every local and Retry-After-derived retry wait.
func WithRateLimit ¶
WithRateLimit sets the shared token-bucket rate and burst. Both values must be finite and positive.
func WithRequestTimeout ¶
WithRequestTimeout sets the timeout for each individual HTTP attempt. Non-positive values preserve the default.
func WithRetryInterval ¶
WithRetryInterval sets the base exponential-backoff interval. Non-positive values preserve the default.
type PageResult ¶
PageResult is one validated upstream page. Data preserves upstream order.
type ProtocolError ¶
type ProtocolError struct{}
ProtocolError indicates that decoded data violated the collection contract. It intentionally exposes no upstream values.
func (*ProtocolError) Error ¶
func (e *ProtocolError) Error() string
func (*ProtocolError) Unwrap ¶
func (e *ProtocolError) Unwrap() error
type RetryError ¶
RetryError reports exhaustion while preserving the last sanitized error.
func (*RetryError) Error ¶
func (e *RetryError) Error() string
func (*RetryError) Is ¶
func (e *RetryError) Is(target error) bool
func (*RetryError) Unwrap ¶
func (e *RetryError) Unwrap() error
type Subject ¶
type Subject struct {
ID int `json:"id"`
SubjectID int `json:"subject_id"`
SubjectType SubjectType `json:"subject_type"`
Type CollectionType `json:"type"`
Name string `json:"name"`
NameCn string `json:"name_cn"`
Rate int `json:"rate"`
Comment string `json:"comment"`
Tags []string `json:"tags"`
UpdatedAt time.Time `json:"updated_at"`
VolStatus int `json:"vol_status"`
EpStatus int `json:"ep_status"`
Private bool `json:"private"`
}
Subject is one complete public collection record.
ID is retained as a compatibility alias and always equals SubjectID.
type SubjectType ¶
type SubjectType int
SubjectType is a Bangumi subject category.
const ( SubjectTypeBook SubjectType = 1 SubjectTypeAnime SubjectType = 2 SubjectTypeMusic SubjectType = 3 SubjectTypeGame SubjectType = 4 SubjectTypeReal SubjectType = 6 )