项目作者: CCInCharge

项目描述 :
Java Wrapper for NewsAPI
高级语言: Java
项目地址: git://github.com/CCInCharge/NewsApi-Java-Wrapper.git
创建时间: 2017-12-24T01:18:13Z
项目社区:https://github.com/CCInCharge/NewsApi-Java-Wrapper

开源协议:MIT License

下载


NewsApi Java Wrapper

Build Status
codecov

A Java wrapper for V2 of the NewsApi.

Installation

Insert the following Maven dependency in your pom.xml:

  1. <dependency>
  2. <groupId>io.github.ccincharge</groupId>
  3. <artifactId>newsapi</artifactId>
  4. <version>1.0</version>
  5. </dependency>

Usage

Instantiate a new instance of the NewsApi class with your API key.

  1. NewsApi newsApi = new NewsApi(<API_KEY>);

Requests

Build up a request with desired parameters. For a full listing of available request parameters, consult the official
NewsAPI docs:

https://newsapi.org/docs/endpoints/top-headlines

https://newsapi.org/docs/endpoints/everything

https://newsapi.org/docs/endpoints/sources

For this example, we’ll search for English articles about bitcoin.

  1. RequestBuilder bitcoinRequest = new RequestBuilder()
  2. .setQ("bitcoin")
  3. .setLanguage("en");

There are numerous parameters available. Q and Language are an example of two of the parameters. For the full set of
available parameters, consult the RequestBuilder Javadoc.

Top Headlines Endpoint

The top-headlines endpoint returns a list of top headlines that match the given request parameters.

  1. RequestBuilder bitcoinRequest = new RequestBuilder()
  2. .setQ("bitcoin")
  3. .setLanguage("en");
  4. ApiArticlesResponse apiArti = newsApi.sendTopRequest(bitcoinRequest);

Everything Endpoint

The everything endpoint returns a list of all articles that match the given request parameters.
It is possible that there are more articles that match given query criteria than the REST API will return in one
response.
To page through different pages of results, use the .setPage() method for the request.

  1. RequestBuilder bitcoinRequest = new RequestBuilder()
  2. .setQ("bitcoin")
  3. .setLanguage("en")
  4. .setPage(2);
  5. ApiArticlesResponse apiArticlesResponse = newsApi.sendEverythingRequest(bitcoinRequest);

Sources Endpoint

The sources endpoint returns a list of all news sources that match the given query criteria.

  1. RequestBuilder sourcesRequest = new RequestBuilder().setLanguage("en");
  2. ApiSourcesResponse apiSourcesResponse = newsApi.sendSourcesRequest(sourcesRequest);

ApiArticlesResponse Object

The top-headlines and the everything endpoints return an ApiArticlesResponse object. To work with this object, simply
issue a method call with the name of the desired response attribute, in order to access the value associated with that
attribute. Some examples are provided below:

  1. RequestBuilder bitcoinRequest = new RequestBuilder()
  2. .setQ("bitcoin")
  3. .setLanguage("en");
  4. ApiArticlesResponse apiArticlesResponse = newsApi.sendTopRequest(bitcoinRequest);
  5. String responseStatus = apiArticlesResponse.status();
  6. // .articles() method returns an ArrayList of articles
  7. ArrayList<Article> newsArticles = apiArticlesResponse.articles();
  8. Article firstArticle = newsArticles.get(0);
  9. String firstArticleTitle = firstArticle.title();

For the full list of available method calls, consult the Javadoc.

ApiSourcesResponse Object

The sources endpoint returns an ApiSourcesResponse object. To work with this object, simply issue a method call with
the name of the desired response attribute, in order to access the value associated with that attribute. Some examples
are provided below:

  1. RequestBuilder sourcesRequest = new RequestBuilder().setLanguage("en");
  2. ApiSourcesResponse apiSourcesResponse = newsApi.sendSourcesRequest(sourcesRequest);
  3. // .sources() method returns an ArrayList of sources
  4. ArrayList<Source> newsSources = apiSourcesResponse.sources();
  5. Source firstSource = newsSources.get(0);
  6. String firstSourceName = firstSource.name();

Contributing

Create a fork of this repo, and commit your changes to a branch of your fork. Issue a PR to this repository when done.

Before beginning any PR, see if your changes correspond to an existing issue. If not, create a new issue. It is best
to leave a comment asking to be assigned to an issue before starting your PR. Attach your PR to an issue before
tarting.

Your PR must pass all tests and must not break the CI build to be merged.