# README
NewsAPI Go Client
Go client for communicating with the newsapi api.
Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
go get github.com/barthr/newsapi
Next up register for free at (https://newsapi.org/register) get yourself a free api key and keep it somewhere save.
Examples
Retrieving all the sources
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
sources, err := c.GetSources(context.Background(), nil)
if err != nil {
panic(err)
}
for _, s := range sources.Sources {
fmt.Println(s.Description)
}
}
Retrieving all the sources for a specific country (Great britian in this case)
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
sources, err := c.GetSources(context.Background(), &newsapi.SourceParameters{
Country: "gb",
})
if err != nil {
panic(err)
}
for _, s := range sources.Sources {
fmt.Println(s.Name)
}
}
Retrieving the top headlines
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
articles, err := c.GetTopHeadlines(context.Background(), &newsapi.TopHeadlineParameters{
Sources: []string{ "cnn", "time" },
})
if err != nil {
panic(err)
}
for _, s := range articles.Articles {
fmt.Printf("%+v\n\n", s)
}
}
Retrieving all the articles
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
articles, err := c.GetEverything(context.Background(), &newsapi.EverythingParameters{
Sources: []string{ "cnn", "time" },
})
if err != nil {
panic(err)
}
for _, s := range articles.Articles {
fmt.Printf("%+v\n\n", s)
}
}
License
This project is licensed under the MIT License
Acknowledgments
- Inspiration from github golang client
# Functions
APIError returns if the given err is of type `newsapi.Error`.
NewClient returns a new newsapi client to query the newsapi API.
WithBaseURL sets the baseurl for the newsapi.
WithHTTPClient sets the http client to use when making requests.
WithUserAgent sets the user agent of the client to userAgent.
# Structs
Article is a single article from the newsapi article response See http://newsapi.org/docs for more details on the property's.
ArticleResponse is the response from the newsapi article endpoint.
A Client manages communication with the NewsAPI API.
Error defines an API error from newsapi.
EverythingParameters are the parameters used for the newsapi everything endpoint.
Source is a source from newsapi it contains all the information provided by the api.
SourceParameters are the parameters which can be used in the source request to newsapi.
SourceResponse is the response from the source request.
TopHeadlineParameters are the parameters which can be used to tweak to request for the top headlines.
# Type aliases
OptionFunc is function which modifies the client.