mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
- Fix Application struct mismatch in application_builder.go - Add global config.Cfg variable and BleveIndexPath field - Regenerate GraphQL code to fix ProcessArgField errors - Add search.InitBleve() call in main.go - Fix all errcheck issues (12 total) in main.go files and test files - Fix staticcheck issues (deprecated handler.NewDefaultServer, tagged switch) - Remove all unused code (50 unused items including mock implementations) - Fix GraphQL 'transport not supported' error in integration tests - Add comprehensive database cleanup for integration tests - Update GraphQL server setup with proper error presenter
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package openlibrary
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const baseURL = "https://openlibrary.org"
|
|
|
|
// Client is a client for the Open Library API.
|
|
type Client struct {
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// NewClient creates a new Open Library client.
|
|
func NewClient() *Client {
|
|
return &Client{
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
}
|
|
}
|
|
|
|
// Author represents the data returned from the Open Library Authors API.
|
|
type Author struct {
|
|
Name string `json:"name"`
|
|
PersonalName string `json:"personal_name"`
|
|
Bio interface{} `json:"bio"` // Bio can be a string or a struct
|
|
Wikipedia string `json:"wikipedia"`
|
|
}
|
|
|
|
// GetAuthor fetches author data from the Open Library API.
|
|
// The olid is the Open Library Author ID (e.g., "OL23919A").
|
|
func (c *Client) GetAuthor(ctx context.Context, olid string) (*Author, error) {
|
|
url := fmt.Sprintf("%s/authors/%s.json", baseURL, olid)
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "TerculEnrichmentTool/1.0 (contact@tercul.com)")
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
}
|
|
|
|
var author Author
|
|
if err := json.NewDecoder(resp.Body).Decode(&author); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &author, nil
|
|
}
|