fedeoasi / news-api-client   0.5

MIT License GitHub

An HTTP client written in Scala for the News API

Scala versions: 2.12

News API Client

Maven Central Build Status

An HTTP client written in Scala for the News API.

Setup

Add the following line to your SBT build definition:

libraryDependencies += "com.github.fedeoasi" %% "news-api-client" % "0.4"

You can get an API key by registering at News API.

Both a synchronous and an asynchronous version of the client are provided.

Synchronous client:

import com.github.fedeoasi.newsapi._
val client = NewsApiClient("<NEWS_API_KEY>")

Asynchronous client:

import com.github.fedeoasi.newsapi._
import scala.concurrent.ExecutionContext.Implicits.global
val client = AsyncNewsApiClient("<NEWS_API_KEY>")

The asynchronous client requires an ExecutionContext at construction time. You can choose whether you want to provide your own execution context or bring the default one into scope.

Usage

All responses returned by the client are wrapped in an Either object. You can retrieve the response using pattern matching as follows:

client.topHeadlines() match {
  case Right(response) => //do something with response
  case Left(message) => //Something went wrong
}

The asynchronous client wraps the above result type into a scala Future. Other than that the method names and parameters are identical.

Top Headlines

Provides live top and breaking headlines as described in its News API documentation.

The following code fragment fetches all headlines from US news sources:

import com.neovisionaries.i18n.CountryCode
client.topHeadlines(country = Some(CountryCode.US))

See all the supported parameters here.

Everything

Search through millions of articles as described in its News API documentation.

You can find all articles about BitCoin from the Wall Street Journal as follows:

client.everything(
  query = Some("bitcoin"),
  sources = Seq("the-wall-street-journal")
)

See all the supported parameters here.

Sources

Returns a subset of the news publishers that top headlines are available from as described in its News API documentation.

You can find all sources as follows:

client.sources()

See all the supported parameters here.

Examples