|
| 1 | +package fetcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// DatasetAPIResponse is the decoded response from GET https://huggingface.co/api/datasets/:id |
| 12 | +type DatasetAPIResponse struct { |
| 13 | + ID string `json:"id"` |
| 14 | + Author string `json:"author"` |
| 15 | + SHA string `json:"sha"` |
| 16 | + LastMod string `json:"lastModified"` |
| 17 | + CreatedAt string `json:"createdAt"` |
| 18 | + Private bool `json:"private"` |
| 19 | + Gated BoolOrString `json:"gated"` |
| 20 | + Disabled bool `json:"disabled"` |
| 21 | + Tags []string `json:"tags"` |
| 22 | + Description string `json:"description"` |
| 23 | + Downloads int `json:"downloads"` |
| 24 | + Likes int `json:"likes"` |
| 25 | + UsedStorage int64 `json:"usedStorage"` |
| 26 | + CardData map[string]any `json:"cardData"` |
| 27 | +} |
| 28 | + |
| 29 | +// DatasetAPIFetcher fetches dataset metadata from the Hugging Face Hub API. |
| 30 | +type DatasetAPIFetcher struct { |
| 31 | + Client *http.Client |
| 32 | + Token string |
| 33 | + BaseURL string // optional; defaults to "https://huggingface.co" |
| 34 | +} |
| 35 | + |
| 36 | +// Fetch fetches dataset metadata for the given datasetID. |
| 37 | +func (f *DatasetAPIFetcher) Fetch(ctx context.Context, datasetID string) (*DatasetAPIResponse, error) { |
| 38 | + client := f.Client |
| 39 | + if client == nil { |
| 40 | + client = http.DefaultClient |
| 41 | + } |
| 42 | + |
| 43 | + trimmedDatasetID := strings.TrimPrefix(strings.TrimSpace(datasetID), "/") |
| 44 | + logf(datasetID, "GET /api/datasets/%s", trimmedDatasetID) |
| 45 | + |
| 46 | + baseURL := strings.TrimRight(strings.TrimSpace(f.BaseURL), "/") |
| 47 | + if baseURL == "" { |
| 48 | + baseURL = "https://huggingface.co" |
| 49 | + } |
| 50 | + |
| 51 | + url := fmt.Sprintf("%s/api/datasets/%s", baseURL, trimmedDatasetID) |
| 52 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + req.Header.Set("Accept", "application/json") |
| 57 | + if strings.TrimSpace(f.Token) != "" { |
| 58 | + req.Header.Set("Authorization", "Bearer "+strings.TrimSpace(f.Token)) |
| 59 | + } |
| 60 | + |
| 61 | + resp, err := client.Do(req) |
| 62 | + if err != nil { |
| 63 | + logf(datasetID, "request error (%v)", err) |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + defer resp.Body.Close() |
| 67 | + |
| 68 | + if resp.StatusCode != http.StatusOK { |
| 69 | + logf(datasetID, "non-200 status=%d", resp.StatusCode) |
| 70 | + return nil, fmt.Errorf("huggingface api status %d", resp.StatusCode) |
| 71 | + } |
| 72 | + |
| 73 | + var parsed DatasetAPIResponse |
| 74 | + if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil { |
| 75 | + logf(datasetID, "decode error (%v)", err) |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + logf(datasetID, "ok") |
| 79 | + return &parsed, nil |
| 80 | +} |
0 commit comments