Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jiachengzhang1/newsapi-java
A Java wrapper for News API
https://github.com/jiachengzhang1/newsapi-java
Last synced: about 17 hours ago
JSON representation
A Java wrapper for News API
- Host: GitHub
- URL: https://github.com/jiachengzhang1/newsapi-java
- Owner: jiachengzhang1
- License: mit
- Created: 2021-01-04T19:01:02.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-12T18:56:27.000Z (almost 4 years ago)
- Last Synced: 2023-07-28T09:16:21.106Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# newsapi-java
A flexible and easy-to-use Java wrapper for [News API](https://newsapi.org). (It only supports for Java 11 and above at
the moment)[![Maven](https://img.shields.io/maven-central/v/com.jzhangdeveloper.newsapi/newsapi-java.svg?label=Maven)](https://search.maven.org/search?q=g:%22com.jzhangdeveloper.newsapi%22%20AND%20a:%22newsapi-java%22)
[![Unit Test](https://github.com/jiachengzhang1/newsapi-java/workflows/Unit%20Tests/badge.svg)](https://github.com/jiachengzhang1/newsapi-java)## Installation
### Maven
Add the dependency to the `pom.xml` file
```xmlcom.jzhangdeveloper.newsapi
newsapi-java
1.0.0```
## Quick Start
**First, create a News API client**
```java
NewsAPIClient client = NewsAPI.newClientBuilder()
.setApiKey(key)
.build();
```**Then, build request parameters**
```java
// an example for "Top Headlines" endpoint
Map topHeadlineParams = TopHeadlinesParams.newBuilder()
.setCountry("us")
.setPageSize(10)
...
.build();// an example for "Everything" endpoint
Map everythingParams = EverythingParams.newBuilder()
.setPageSize(100)
.setSearchQuery("spacex")
...
.build();// an example for "Source" endpoint
Map sourcesParams = SourcesParams.newBuilder()
.setCountry("us")
.setLanguage("en")
...
.build();
```**Finally, get recourses**
```java
NewAPIResponse response = client.getSources(sourcesParams);// get status code
response.getStatusCode()// get response body as a Java object and Json object (use Gson)
Sources sources = response.getBody();
JsonObject sourcesJson = response.getBodyAsJson();// get headers
Map>headers = response.getHeaders();
```## Documentation
To build query parameters, `TopHeadlinesParams.newBuilder()`, `EverythingParams.newBuilder()` and `SourcesParams.newBuilder()` are avaliable to use. There are setters for each builder to help with creating proper params for a specific endpoint. Alternatively, you can build one your own with `Map` type. Read [News API Documentation](https://newsapi.org/docs/endpoints) for proper naming.`NewsAPI.newClientBuilder()`: Initiate a `NewsAPI.Builder` that configures the Http request, settings are following,
- `setApiKey(String apiKey)`: Set api key for News API, this setting uses Bearer authorization header
- `setAuthorization (AuthTypes authType, String apiKey)`: Set api key and authorization type, options are `AuthTypes.API_KEY_PARAM` (pass api key as a query param), `AuthTypes.AUTHORIZATION_HEADER` (use Bearer authorization header) and `AuthTypes.X_API_KEY_HEADER` (use X-API-KEY header)
- `setHttpVersion (HttpVersions httpVersion)`: Set the http version, `HttpVersions.HTTP` (HTTP 1.1) or `HttpVersions.HTTP_2` (HTTP 2)
- `setNoCache (boolean noCache)`: No cache on News API when it's set to `true`
- `build()`: Build a `NewsAPIClient` instance`NewsAPIClient`: It's an interface, an instance can be created through `NewsAPI.newClientBuilder().build()`.
- `get (Endpoints endpoint, Map params)`: Make a `GET` request to the `endpoint` passed in, query params are `params`
- `getEverything (Map params)`: `GET /everything`, query params are `params`
- `getTopHeadlines (Map params)`: `GET /top-headlines`, query params are `params`
- `getSources (Map params)`: `GET /sources`, query params are `params``NewsAPIResponse`: It's an interface, an instance is returned after making an api call, get the response details by,
- `getStatusCode()`: Get response status code
- `getBodyAsString()`: Get response body as `String`
- `getBodyAsJson()`: Get response body as a JSON object
- `getBody()`: Get response body as a Java object, `Everything`, `TopHeadlines` or `Sources` depending on the endpoint
- `getHeaders()`: Get response headers as a Map**Exceptions are throwed if the response status code is not `2xx`**
See Javadoc for more details.