Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miquido/jebatch
The project was made by Miquido. https://www.miquido.com/
https://github.com/miquido/jebatch
Last synced: 5 days ago
JSON representation
The project was made by Miquido. https://www.miquido.com/
- Host: GitHub
- URL: https://github.com/miquido/jebatch
- Owner: miquido
- License: apache-2.0
- Created: 2017-11-27T19:32:25.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-10T11:38:55.000Z (over 6 years ago)
- Last Synced: 2024-11-09T11:34:55.901Z (2 months ago)
- Language: Kotlin
- Homepage:
- Size: 76.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
JeBatch is a simple library for handling lightweight batch requests on serverside.
It allows handling collections of requests of mixed types with single call.
Designed for handling operations on collection of resources.For use with Kotlin and Java.
# Use cases and limitations
JeBatch is designed for handling REST-compliant batch requests on collection of resources: getting collection or a single resource with GET, creating a resource with POST, replacing it with PUT, updating with PATCH or deleting with DELETE.Supported HTTP methods are GET, POST, PUT, PATCH and DELETE.
Only GET allows returning response body to the caller.
# Usage
Creating a JeBatch instance:
```java
val jeBatch = JeBatch.builder()
.forGet { getResourcesList() }
.and().forPost { requestBody -> createResourceAndReturnId(requestBody) }
.and().forPut { requestBody, id -> putResource(requestBody, id) }
.and().forPatch { requestBody, id -> patchResource(requestBody, id) }
.and().forDelete { id -> deleteResource(id) }
.and().build()
```
REQUEST_BODY_TYPE is the type that is expected to come as body in POST, PUT and PATCH requests.RESPONSE_BODY_TYPE is the type of items in collection returned by GET.
ID_TYPE is the type of identifiers of resources that are appended to path for PUT, PATCH and DELETE requests and returned in path in result of POST request.
JeBatch can translate Exceptions thrown from your methods into HTTP response statuses. To define error translating add withError calls on builder after defining methods:
```java
val jeBatch = JeBatch.builder()
.forPatch { requestBody, id -> patchResource(requestBody, id) }
.withError(NotFoundException::class.java, 404)
.withError(BadRequestException::class.java, 400)
.withError(ValidationException::class.java, 422)
.and()
.forPut { requestBody, id -> putResource(requestBody, id) }
.withError(BadRequestException::class.java, 400)
.withError(ValidationException::class.java, 422)
.and()
.build()
```Methods that are not defined in builder will cause a 405 response to be returned upon being called. Message of exceptions is returned in message of the response.
JeBatch accepts request of type BatchRequest. These contain list of BatchRequestElements, each with defined HTTP method (operation), optional (depending on method) body and id.
Analogously, it returns BatchResponse containg a list of BatchResponseElements, each with status, resourcePath (except for GET), message (if there was an error) and body (if it was a GET).
Ideally, your service should accept requests and respond in these formats. Then, usage of JeBatch is trivial:
```java
val response = batch.process("PATH_TO_RESOURCES_COLLECTION", batchRequest)
```
Since base path is passed on each process() invocation you can use one instance of JeBatch for handling multiple paths, as long as request and response bodies and id remain the same type.# Further development
* Support request params (usecase: limit and offset in GET on collection).
* Support returning body from any method. Not very RESTy but useful.