{"id":26426619,"url":"https://github.com/yarlson/hnapi","last_synced_at":"2025-09-11T01:12:14.946Z","repository":{"id":282932808,"uuid":"950138809","full_name":"yarlson/hnapi","owner":"yarlson","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-17T17:52:41.000Z","size":4502,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T18:39:52.712Z","etag":null,"topics":["go","hackernews","hackernews-api","sdk"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yarlson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-17T17:32:17.000Z","updated_at":"2025-03-17T17:52:44.000Z","dependencies_parsed_at":"2025-03-17T18:41:19.505Z","dependency_job_id":"11130c36-13ce-45bb-9424-c8da779d433f","html_url":"https://github.com/yarlson/hnapi","commit_stats":null,"previous_names":["yarlson/hnapi"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarlson%2Fhnapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarlson%2Fhnapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarlson%2Fhnapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarlson%2Fhnapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yarlson","download_url":"https://codeload.github.com/yarlson/hnapi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244152300,"owners_count":20406967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","hackernews","hackernews-api","sdk"],"created_at":"2025-03-18T03:33:59.756Z","updated_at":"2025-03-18T03:34:00.350Z","avatar_url":"https://github.com/yarlson.png","language":"Go","readme":"# Hacker News API SDK for Go\n\n[![GoDoc](https://godoc.org/github.com/yarlson/hnapi?status.svg)](https://godoc.org/github.com/yarlson/hnapi)\n[![CI](https://github.com/yarlson/hnapi/actions/workflows/ci.yml/badge.svg)](https://github.com/yarlson/hnapi/actions/workflows/ci.yml)\n\n**hnapi** is a Go SDK for interacting with the [Hacker News API](https://github.com/HackerNews/API). It provides an easy-to-use interface to retrieve stories, comments, jobs, polls, and user profiles. The package also offers helper functions for batch retrieval and a real-time updates mechanism using Go channels.\n\n## Features\n\n- **Complete Coverage:** Fetch items (stories, comments, jobs, polls, etc.), user profiles, and lists (top, new, best, Ask, Show, Job).\n- **Strongly Typed:** JSON responses are automatically parsed into Go structs.\n- **Batch Retrieval:** Efficiently fetch multiple items concurrently with a configurable concurrency limit.\n- **Real-Time Updates:** Subscribe to updates from the `/v0/updates` endpoint via a channel-based API.\n- **Configurable \u0026 Extensible:** Customize timeouts, base URL, retry strategies, polling intervals, concurrency limits, and even inject a custom `http.Client`.\n- **Context-Aware:** All methods accept `context.Context` for cancellation and deadlines.\n\n## Installation\n\nTo install **hnapi**, run:\n\n```bash\ngo get github.com/yarlson/hnapi\n```\n\nThen, import it in your project:\n\n```go\nimport \"github.com/yarlson/hnapi\"\n```\n\n## Quick Start\n\nBelow is a simple example to get you started:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/yarlson/hnapi\"\n)\n\nfunc main() {\n\t// Initialize the client with custom options if needed\n\tclient := hnapi.NewClient(\n\t\thnapi.WithRequestTimeout(15*time.Second),\n\t\thnapi.WithConcurrency(5),\n\t\thnapi.WithPollInterval(30*time.Second), // Default is 30 seconds\n\t)\n\n\t// Create a context with a timeout\n\tctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)\n\tdefer cancel()\n\n\t// Fetch top stories\n\ttopStories, err := client.GetTopStories(ctx)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error fetching top stories: %v\", err)\n\t}\n\tfmt.Printf(\"Top Stories: %v\\n\", topStories)\n\n\t// Fetch an individual item\n\tif len(topStories) \u003e 0 {\n\t\titem, err := client.GetItem(ctx, topStories[0])\n\t\tif err != nil {\n\t\t\tlog.Fatalf(\"Error fetching item: %v\", err)\n\t\t}\n\t\tfmt.Printf(\"Item Details:\\n Title: %s\\n By: %s\\n Score: %d\\n\", item.Title, item.By, item.Score)\n\t}\n\n\t// Start real-time updates\n\tupdatesCh, err := client.StartUpdates(ctx)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error starting updates: %v\", err)\n\t}\n\n\t// Process updates for a short time\n\tgo func() {\n\t\tfor update := range updatesCh {\n\t\t\tfmt.Printf(\"Updates: %d items, %d profiles changed\\n\", len(update.Items), len(update.Profiles))\n\t\t}\n\t}()\n\n\t// Wait to observe some updates (or press Ctrl+C to cancel)\n\ttime.Sleep(1 * time.Minute)\n}\n```\n\n## Configuration Options\n\n**hnapi** uses the \"with options\" pattern. You can customize the client by providing various options:\n\n- **WithBaseURL(url string):** Set a custom base URL. (Default: `https://hacker-news.firebaseio.com/v0/`)\n- **WithRequestTimeout(timeout time.Duration):** Set the request timeout. (Default: 10 seconds)\n- **WithMaxRetries(retries int):** Set the maximum number of retries for failed requests. (Default: 3)\n- **WithBackoffInterval(interval time.Duration):** Set the backoff interval between retries. (Default: 2 seconds)\n- **WithPollInterval(interval time.Duration):** Set the polling interval for real-time updates. (Default: 30 seconds)\n- **WithConcurrency(concurrency int):** Set the concurrency limit for batch retrieval. (Default: 10)\n- **WithHTTPClient(client \\*http.Client):** Inject a custom HTTP client for advanced use cases.\n\nExample:\n\n```go\nclient := hnapi.NewClient(\n\thnapi.WithBaseURL(\"https://custom-api.example.com/\"),\n\thnapi.WithRequestTimeout(15*time.Second),\n\thnapi.WithConcurrency(5),\n)\n```\n\n## Testing\n\nThe **hnapi** package is fully tested with unit and integration tests. To run tests, simply execute:\n\n```bash\ngo test -v ./...\n```\n\nFor integration tests that make real API calls, consider running them in an environment where such calls are allowed, or skip them with `-short`.\n\n## Documentation\n\nFull documentation is available via [GoDoc](https://godoc.org/github.com/yarlson/hnapi). Inline comments and examples are provided to ensure a smooth development experience.\n\n## Contributing\n\nContributions are welcome! Please open issues or submit pull requests on [GitHub](https://github.com/yarlson/hnapi).\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyarlson%2Fhnapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyarlson%2Fhnapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyarlson%2Fhnapi/lists"}