Skip to content

Commit 7301610

Browse files
committed
freeze: factor out filterconfig assembly
1 parent 262561f commit 7301610

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

freeze/freeze.go

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -95,59 +95,22 @@ func FetchOrCached(fopts FolioOpts, copts CacheOpts) (string, error) {
9595
return cached, nil
9696
}
9797

98-
// Fetch fetches a filterconfig from FOLIO and writes a frozen zip to outputPath.
99-
func Fetch(opts FolioOpts, outputPath string) error {
100-
if opts.Token == "" {
101-
return fmt.Errorf("OKAPI token is required")
102-
}
103-
if opts.OkapiURL == "" {
104-
return fmt.Errorf("OKAPI URL is required")
105-
}
106-
if opts.Limit <= 0 {
107-
opts.Limit = 100000
108-
}
109-
var httpClient *http.Client
110-
if opts.NoProxy {
111-
httpClient = &http.Client{
112-
Transport: &http.Transport{
113-
Proxy: nil,
114-
DialContext: (&net.Dialer{Timeout: 30 * time.Second}).DialContext,
115-
TLSHandshakeTimeout: 10 * time.Second,
116-
ResponseHeaderTimeout: 30 * time.Second,
117-
},
118-
}
119-
} else {
120-
httpClient = http.DefaultClient
121-
}
122-
pesterClient := pester.New()
123-
if opts.NoProxy {
124-
pesterClient.Transport = httpClient.Transport
125-
}
126-
api := folio.API{
127-
Base: opts.OkapiURL,
128-
Tenant: opts.Tenant,
129-
Client: pesterClient,
130-
}
131-
api.SetToken(opts.Token)
132-
resp, err := api.MetadataCollections(folio.MetadataCollectionsOpts{
133-
CQL: `(selectedBy=("*"))`,
134-
Limit: opts.Limit,
135-
IncludeFilteredBy: true,
136-
})
137-
if err != nil {
138-
return fmt.Errorf("fetch metadata collections: %w", err)
139-
}
140-
log.Printf("fetched %d collections", len(resp.FincConfigMetadataCollections))
141-
// Build filterconfig: group collections by ISIL.
142-
type collectionInfo struct {
143-
sourceId string
144-
contentFiles []string
145-
filteredBy []folio.FilterEntry
146-
solrMegaCollections []string
147-
}
98+
// collectionInfo holds the fields needed to build a single filterconfig entry.
99+
type collectionInfo struct {
100+
sourceId string
101+
contentFiles []string
102+
filteredBy []folio.FilterEntry
103+
solrMegaCollections []string
104+
}
105+
106+
// buildFilterConfig groups FOLIO metadata collections by ISIL and assembles
107+
// the filterconfig map. It returns the config and the set of URLs referenced
108+
// by it (content files and filter files). Collections whose CollectionId
109+
// doesn't match sourceIdPattern are silently skipped.
110+
func buildFilterConfig(collections []folio.FincConfigMetadataCollection, okapiURL string) (map[string]any, map[string]bool) {
148111
isilCollections := make(map[string][]collectionInfo)
149112
skipped := 0
150-
for _, col := range resp.FincConfigMetadataCollections {
113+
for _, col := range collections {
151114
m := sourceIdPattern.FindStringSubmatch(col.CollectionId)
152115
if m == nil {
153116
skipped++
@@ -164,9 +127,7 @@ func Fetch(opts FolioOpts, outputPath string) error {
164127
}
165128
}
166129
log.Printf("found %d ISILs, skipped %d collections without source ID", len(isilCollections), skipped)
167-
// Track all URLs that need downloading.
168130
urlSet := make(map[string]bool)
169-
// Build the filterconfig blob.
170131
filterConfig := make(map[string]any)
171132
isils := slices.Sorted(maps.Keys(isilCollections))
172133
for _, isil := range isils {
@@ -192,7 +153,7 @@ func Fetch(opts FolioOpts, outputPath string) error {
192153
continue
193154
}
194155
for _, ff := range fb.FilterFiles {
195-
fileURL := fmt.Sprintf("%s/finc-config/files/%s", opts.OkapiURL, ff.FileId)
156+
fileURL := fmt.Sprintf("%s/finc-config/files/%s", okapiURL, ff.FileId)
196157
urlSet[fileURL] = true
197158
label := strings.ToLower(fb.Label)
198159
switch {
@@ -238,6 +199,53 @@ func Fetch(opts FolioOpts, outputPath string) error {
238199
}
239200
}
240201
}
202+
return filterConfig, urlSet
203+
}
204+
205+
// Fetch fetches a filterconfig from FOLIO and writes a frozen zip to outputPath.
206+
func Fetch(opts FolioOpts, outputPath string) error {
207+
if opts.Token == "" {
208+
return fmt.Errorf("OKAPI token is required")
209+
}
210+
if opts.OkapiURL == "" {
211+
return fmt.Errorf("OKAPI URL is required")
212+
}
213+
if opts.Limit <= 0 {
214+
opts.Limit = 100000
215+
}
216+
var httpClient *http.Client
217+
if opts.NoProxy {
218+
httpClient = &http.Client{
219+
Transport: &http.Transport{
220+
Proxy: nil,
221+
DialContext: (&net.Dialer{Timeout: 30 * time.Second}).DialContext,
222+
TLSHandshakeTimeout: 10 * time.Second,
223+
ResponseHeaderTimeout: 30 * time.Second,
224+
},
225+
}
226+
} else {
227+
httpClient = http.DefaultClient
228+
}
229+
pesterClient := pester.New()
230+
if opts.NoProxy {
231+
pesterClient.Transport = httpClient.Transport
232+
}
233+
api := folio.API{
234+
Base: opts.OkapiURL,
235+
Tenant: opts.Tenant,
236+
Client: pesterClient,
237+
}
238+
api.SetToken(opts.Token)
239+
resp, err := api.MetadataCollections(folio.MetadataCollectionsOpts{
240+
CQL: `(selectedBy=("*"))`,
241+
Limit: opts.Limit,
242+
IncludeFilteredBy: true,
243+
})
244+
if err != nil {
245+
return fmt.Errorf("fetch metadata collections: %w", err)
246+
}
247+
log.Printf("fetched %d collections", len(resp.FincConfigMetadataCollections))
248+
filterConfig, urlSet := buildFilterConfig(resp.FincConfigMetadataCollections, opts.OkapiURL)
241249
if opts.Expand != nil {
242250
ExpandFilterConfig(filterConfig, opts.Expand)
243251
log.Printf("expanded %d meta-ISIL(s)", len(opts.Expand))

0 commit comments

Comments
 (0)