Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thewaterfall/fluent-request
Fluent Request is a Java library designed to simplify and streamline the process of making HTTP requests. It follows a fluent builder pattern, allowing users to construct complex HTTP requests in a readable and expressive manner
https://github.com/thewaterfall/fluent-request
http http-client java java-http-client java-http-request request request-builder
Last synced: 5 days ago
JSON representation
Fluent Request is a Java library designed to simplify and streamline the process of making HTTP requests. It follows a fluent builder pattern, allowing users to construct complex HTTP requests in a readable and expressive manner
- Host: GitHub
- URL: https://github.com/thewaterfall/fluent-request
- Owner: thewaterfall
- License: mit
- Created: 2023-11-14T12:53:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-14T11:40:42.000Z (3 months ago)
- Last Synced: 2024-08-14T12:46:37.476Z (3 months ago)
- Topics: http, http-client, java, java-http-client, java-http-request, request, request-builder
- Language: Java
- Homepage:
- Size: 92.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluent Request
Fluent Request is a powerful and flexible Java library for constructing HTTP requests in a fluent and expressive manner. It simplifies the process of creating and sending HTTP requests by providing a chainable API for building requests with ease.
## Features
- Clean, readable and fluent syntax.
- Customizable request parameters and headers.
- Support various HTTP methods, including GET, POST, PUT, DELETE, and more.
- Support for JSON, multipart, form data, and other content types.
- [OkHttp](https://github.com/square/okhttp) library as the base HTTP client
- [Jackson](https://github.com/FasterXML/jackson) library as JSON parser to return expected response## Installation
Fluent Request can be easily installed using JitPack, see Gradle and Maven examples below.### Gradle
Add the following to your build.gradle file:```
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}dependencies {
implementation 'com.github.thewaterfall:fluent-request:1.3.1'
}
```### Maven
Add the following to your pom.xml file:```
jitpack.io
https://jitpack.io
com.github.thewaterfall
fluent-request
1.3.1
```
## Usage
The `FluentRequest` class is the core of the library, providing a fluent interface for constructing and sending HTTP requests. It supports various HTTP methods, request body types, headers, and authentication methods. Additionally, it allows customization of the OkHttp client and Jackson ObjectMapper.
### Example 1: Fetching an entity
Let's assume you want to fetch a single article by its ID. The endpoint for this example is `https://example.com/articles/1`.
```
FluentRequest.request("https://example.com/articles/1", Article.class)
.get();
```### Example 2: Creating an entity
Suppose you want to create a new article by sending a POST request with the article data. Let's assume the endpoint is https://example.com/articles.```
Article newArticle = new Article("New Article Title", "Content goes here");FluentRequest.request("https://example.com/articles", Article.class)
.bearer("YOUR_ACCESS_TOKEN") // Assuming you need authentication
.body(newArticle)
.post();
```### Example 3: Updating an entity
Let's say you want to update an existing article with ID 1. The endpoint for this example is https://example.com/articles/1.```
Article updatedArticle = new Article("Updated Title", "Updated content");FluentRequest.request("https://example.com/articles/1", Article.class)
.bearer("YOUR_ACCESS_TOKEN")
.body(updatedArticle)
.patch();
```### Example 4: Uploading a file
```
File file = new File("path/to/file.txt");FluentRequest.request("https://example.com/upload", String.class)
.multipart()
.add("username", "john_doe") // Additional form field
.add("fileKey", "filename.txt", fileToUpload) // File to upload
.build()
.post();
```### Example 5: Using variables and parameters
The below example will result in building the following URL https://example.com/articles/1/comments?sort=asc.```
FluentRequest.request("https://example.com/articles/{articleId}/comments", Comment[].class)
.variable("articleId", 1)
.parameter("sort", "asc")
.get();
```### Example 6: Using generic type as a response body type
```
FluentRequest.request("https://example.com/articles", new TypeReference>() {})
.get();
```