Files
job-search/internal/findatender/findatender.go
T
bwilliams 18db462d89 Made the fetcher a Fetcher type with per-source config, and ran it in job-search serve
fetcher.Run(ctx, conf, since) becomes New(&conf.Sources, opts...) and
Run(ctx, since). Each worker now takes only its own source's config, and
Run starts each one through a closure that binds it. Cancelling ctx stops
the workers through the Fetcher's background context, and Run always waits
for every worker before returning their joined errors.

The findatender client no longer holds the configuration: New takes only
the API root, ReloadConfig is gone, and request tracing follows
logging.IsDebugEnabled(). The FindAOpportunity worker is now FindATender.

job-search serve runs the fetcher alongside the HTTP server. A source that
fails stops the server, and the server stopping stops the sources; with no
sources enabled the server keeps running. fetcher service and fetcher
fts-once use the new APIs.
2026-10-16 10:44:38 +01:00

110 lines
2.8 KiB
Go

package findatender
import (
"context"
"log/slog"
"time"
resty "resty.dev/v3"
"git.avsdev.uk/Go/job-search/internal/types"
)
// FindATender is a client for the Find a Tender OCDS API (or the mock
// server). Create it with New and call Shutdown when finished with it.
type FindATender struct {
httpClient *resty.Client
apiRoot string
ctx context.Context
log *slog.Logger
}
// Option configures a FindATender client.
type Option func(*FindATender)
// WithLogger makes the client log to logger instead of slog.Default().
func WithLogger(logger *slog.Logger) Option {
return func(ft *FindATender) {
ft.log = logger
}
}
// New returns a client for the OCDS API at apiRoot. Requests time out after
// 60 seconds and are retried once. Call Shutdown when finished with it. It
// logs to slog.Default() unless given WithLogger.
func New(apiRoot string, opts ...Option) *FindATender {
findATender := &FindATender{
apiRoot: apiRoot,
}
for _, opt := range opts {
opt(findATender)
}
findATender.httpClient = resty.New().
SetTimeout(60 * time.Second).
SetRetryCount(1)
return findATender
}
// logger returns the logger the client logs to, tagged with its component.
func (ft *FindATender) logger() *slog.Logger {
log := ft.log
if log == nil {
log = slog.Default()
}
return log.With("component", "findatender")
}
// With returns a copy of the client whose requests use ctx, so they stop
// when it is cancelled. A nil ctx returns the client unchanged.
func (ft *FindATender) With(ctx context.Context) *FindATender {
if ctx == nil {
return ft
}
copied := *ft
copied.ctx = ctx
return &copied
}
// Shutdown closes the client's HTTP connections. A failure to close is
// logged, as there is nothing a caller could do about it.
func (ft *FindATender) Shutdown() {
if err := ft.httpClient.Close(); err != nil {
ft.logger().Warn("closing http client failed", "error", err)
}
}
// FetchOnce fetches the first page of releases updated since since, and
// converts them to opportunities. If allowRateLimit is true, a 429 response
// is waited out and retried; otherwise it fails with ErrRateLimitExceeded.
func (ft *FindATender) FetchOnce(
since time.Time, allowRateLimit bool,
) ([]*types.Opportunity, error) {
releases, err := ft.fetchOnce(RequestOptions{
Since: since,
AllowRateLimit: allowRateLimit,
})
if err != nil {
return nil, err
}
return Convert(releases), nil
}
// FetchAll fetches every page of releases updated since since, following
// each page's next link, and converts them to opportunities. allowRateLimit
// behaves as for FetchOnce.
func (ft *FindATender) FetchAll(
since time.Time, allowRateLimit bool,
) ([]*types.Opportunity, error) {
releases, err := ft.fetchAll(RequestOptions{
Since: since,
AllowRateLimit: allowRateLimit,
})
if err != nil {
return nil, err
}
return Convert(releases), nil
}