Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danvergara/newsapigo
A Go client for the News API
https://github.com/danvergara/newsapigo
api-client golang hacktoberfest newsapi-org
Last synced: about 2 months ago
JSON representation
A Go client for the News API
- Host: GitHub
- URL: https://github.com/danvergara/newsapigo
- Owner: danvergara
- License: mit
- Created: 2018-07-18T21:57:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-05T05:44:21.000Z (over 4 years ago)
- Last Synced: 2024-06-20T16:32:00.703Z (6 months ago)
- Topics: api-client, golang, hacktoberfest, newsapi-org
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.TXT
Awesome Lists containing this project
README
# News API package for Go
> A Golang client for the News API *https://newsapi.org*
This library provides a convenient way to consume the News API service easily.
For more info about the service, you can check the documentation out.
*https://newsapi.org/docs*##### Provided under MIT License by Daniel Vergara.
## Installation
```shell
go get github.com/danvergara/newsapigo
```## Usage
After the installation, import the client into your project:
```go
import (
"github.com/danvergara/newsapigo"
)
```
## InitializationFirst, all what you need is to instantiate an newsapigo client, for that purpose, we implement a convenient function called New:
```go
import (
"net/url"
"os""github.com/danvergara/newsapigo"
)// Instantiates the Client provided by this library
// Don't forget provide your API Key
// You can get one from the official site: https://newsapi.org/register
c := newsapigo.NewClient(os.Getenv("API_KEY"))
```This client only has three methods (one for each endpoint provided by the API):
TopHeadlines, Everything and Sources.If you want to customize the request, for convenience, use the corresponding structs for each method. Those structs are converted to url.Vlaues under the hood, using the common public API *QueryParams*.
Each method receives a struct object as shown below:
## Top Headlines
```go
// This corresponds to '/v2/top-headlines'queryParams := TopHeadlinesArgs{
Sources: []string{"bbc-news"},
}response, err := c.TopHeadlines(queryParams)
```## Everything
```go
// This corresponds to '/v2/everything'queryParams := EverythingArgs{
Q: "bitcoin",
}response, err := c.Everything(queryParams)
```## Sources
```go
// This corresponds to '/v2/sources'queryParams := SourcesArgs{
Language: "eng",
Country: "us",
}response, err := c.Sources(queryParams)
```
For more information on what fields the structs contain, check the file queryparams.go at the root of the project.