{"id":20281199,"url":"https://github.com/jiachengzhang1/newsapi-java","last_synced_at":"2025-07-10T07:35:04.270Z","repository":{"id":57725806,"uuid":"326780907","full_name":"jiachengzhang1/newsapi-java","owner":"jiachengzhang1","description":"A Java wrapper for News API","archived":false,"fork":false,"pushed_at":"2021-01-12T18:56:27.000Z","size":76,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-14T07:27:53.609Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jiachengzhang1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-04T19:01:02.000Z","updated_at":"2024-11-13T01:25:35.000Z","dependencies_parsed_at":"2022-09-26T21:51:11.406Z","dependency_job_id":null,"html_url":"https://github.com/jiachengzhang1/newsapi-java","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiachengzhang1%2Fnewsapi-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiachengzhang1%2Fnewsapi-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiachengzhang1%2Fnewsapi-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiachengzhang1%2Fnewsapi-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jiachengzhang1","download_url":"https://codeload.github.com/jiachengzhang1/newsapi-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241773555,"owners_count":20018123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T13:39:30.768Z","updated_at":"2025-03-04T03:19:13.399Z","avatar_url":"https://github.com/jiachengzhang1.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# newsapi-java\n\nA flexible and easy-to-use Java wrapper for [News API](https://newsapi.org). (It only supports for Java 11 and above at\nthe moment)\n\n[![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)\n[![Unit Test](https://github.com/jiachengzhang1/newsapi-java/workflows/Unit%20Tests/badge.svg)](https://github.com/jiachengzhang1/newsapi-java)\n\n## Installation\n\n### Maven\nAdd the dependency to the `pom.xml` file\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.jzhangdeveloper.newsapi\u003c/groupId\u003e\n  \u003cartifactId\u003enewsapi-java\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Quick Start\n\n**First, create a News API client**\n\n```java\nNewsAPIClient client = NewsAPI.newClientBuilder()\n        .setApiKey(key)\n        .build();\n```\n\n**Then, build request parameters**\n\n```java\n// an example for \"Top Headlines\" endpoint\nMap\u003cString, String\u003e topHeadlineParams = TopHeadlinesParams.newBuilder()\n        .setCountry(\"us\")\n        .setPageSize(10)\n        ...\n        .build();\n\n// an example for \"Everything\" endpoint\nMap\u003cString, String\u003e everythingParams = EverythingParams.newBuilder()\n        .setPageSize(100)\n        .setSearchQuery(\"spacex\")\n        ...\n        .build();\n\n// an example for \"Source\" endpoint\nMap\u003cString, String\u003e sourcesParams = SourcesParams.newBuilder()\n        .setCountry(\"us\")\n        .setLanguage(\"en\")\n        ...\n        .build();\n```\n\n**Finally, get recourses**\n\n```java\nNewAPIResponse response = client.getSources(sourcesParams);\n\n// get status code\nresponse.getStatusCode()\n\n// get response body as a Java object and Json object (use Gson)\nSources sources = response.getBody();\nJsonObject sourcesJson = response.getBodyAsJson();\n\n// get headers\nMap\u003cString, List\u003cString\u003e\u003eheaders = response.getHeaders();\n```\n\n## Documentation\nTo 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\u003cString, String\u003e` type. Read [News API Documentation](https://newsapi.org/docs/endpoints) for proper naming.\n\n`NewsAPI.newClientBuilder()`: Initiate a `NewsAPI.Builder` that configures the Http request, settings are following,\n- `setApiKey(String apiKey)`: Set api key for News API, this setting uses Bearer authorization header\n- `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)\n- `setHttpVersion (HttpVersions httpVersion)`: Set the http version, `HttpVersions.HTTP` (HTTP 1.1) or `HttpVersions.HTTP_2` (HTTP 2)\n- `setNoCache (boolean noCache)`: No cache on News API when it's set to `true`\n- `build()`: Build a `NewsAPIClient` instance\n\n`NewsAPIClient`: It's an interface, an instance can be created through `NewsAPI.newClientBuilder().build()`.\n- `get (Endpoints endpoint, Map\u003cString, String\u003e params)`: Make a `GET` request to the `endpoint` passed in, query params are `params`\n- `getEverything (Map\u003cString, String\u003e params)`: `GET /everything`, query params are `params`\n- `getTopHeadlines (Map\u003cString, String\u003e params)`: `GET /top-headlines`, query params are `params`\n- `getSources (Map\u003cString, String\u003e params)`: `GET /sources`, query params are `params`\n\n`NewsAPIResponse`: It's an interface, an instance is returned after making an api call, get the response details by,\n- `getStatusCode()`: Get response status code\n- `getBodyAsString()`: Get response body as `String`\n- `getBodyAsJson()`: Get response body as a JSON object\n- `getBody()`: Get response body as a Java object, `Everything`, `TopHeadlines` or `Sources` depending on the endpoint\n- `getHeaders()`: Get response headers as a Map\n\n**Exceptions are throwed if the response status code is not `2xx`**\n\nSee Javadoc for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiachengzhang1%2Fnewsapi-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjiachengzhang1%2Fnewsapi-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiachengzhang1%2Fnewsapi-java/lists"}