Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Bisnode/opa-java-client
https://github.com/Bisnode/opa-java-client
java javaclient opa openpolicyagent
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/Bisnode/opa-java-client
- Owner: Bisnode
- License: apache-2.0
- Created: 2020-01-29T09:56:34.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-31T10:47:07.000Z (3 months ago)
- Last Synced: 2024-07-31T11:39:55.989Z (3 months ago)
- Topics: java, javaclient, opa, openpolicyagent
- Language: Java
- Size: 200 KB
- Stars: 36
- Watchers: 8
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-opa - OPA Java Client - Generic Java client to query OPA's REST API (Language and Platform Integrations / Java)
README
# opa-java-client
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.bisnode.opa/opa-java-client/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.bisnode.opa/opa-java-client) ![build](https://github.com/Bisnode/opa-java-client/workflows/build/badge.svg)OPA java client is a wrapper for [OPA REST API](https://www.openpolicyagent.org/docs/latest/rest-api/). The goal was to create client that is lightweight and framework independent. It's built for current Bisnode needs, including:
- creating documents
- creating policies
- querying for documents
## Installation
**Prerequisites:** Java 11 or higherAdd library using maven:
```xmlcom.bisnode.opa
opa-java-client
{version}```
or Gradle
```groovy
implementation 'com.bisnode.opa:opa-java-client:{version}'
```## Usage
Our library is using Jackson for (de)serialization so, objects that you are passing/retrieving using this client should have either proper Jackson-friendly configuration or - the solution working in most cases - getters and setters for fields you want to pass/retrieve to/from OPA.
[More information about Jackson](https://github.com/FasterXML/jackson-docs).### Query for document
```java
OpaQueryApi client = OpaClient.builder()
.opaConfiguration("http://localhost:8181")
.build();DesiredResponse response = client.queryForDocument(new QueryForDocumentRequest(yourDTO, "path/to/document"), DesiredResponse.class);
// Do whatever you like with the response
```### Query for a list of documents
This requires [commons-lang3](https://mvnrepository.com/artifact/org.apache.commons/commons-lang3) to be present on your classpath.
```java
OpaQueryApi client = OpaClient.builder()
.opaConfiguration("http://localhost:8181")
.build();
ParameterizedType type = TypeUtils.parameterize(List.class, DesiredResponse.class);
List response = client.queryForDocument(new QueryForDocumentRequest(yourDTO, "path/to/document"), type);// Do whatever you like with the response
```####Example
Example project is in `examples/query-for-document` directory.
### Create policy
```java
OpaPolicyApi client = OpaClient.builder()
.opaConfiguration("http://localhost:8181")
.build();void createOrUpdatePolicy(new OpaPolicy("your_policy_id", "content of the policy"));
```
### Create document
```java
OpaDataApi client = OpaClient.builder()
.opaConfiguration("http://localhost:8181")
.build();void createOrOverwriteDocument(new OpaDocument("path/to/document", "content of document (json)"));
```### Error handling
Error handling is done via exceptions. This means that if any error occurs, runtime exception which is subclass of `OpaClientException` is thrown. For now, there is simple error message returned.`OpaServerConnectionException` is thrown when connection problems with OPA server occur.
### Interface segregation
Every OPA (Data, Policy, Query) API has it's own interface. So, for example, if you want to use client only for querying, you can use `OpaQueryApi` as projection. Thanks to that, in your code there will be exposed only methods that are needed by you, while not allowing to mess with policies and data.Available interface projections:
- `OpaQueryApi`
- `OpaPolicyApi`
- `OpaDataApi`## Developing and building
Build process and dependency management is done using Gradle.
Tests are written in spock.## Contribution
Interested in contributing? Please, start by reading [this document](https://github.com/Bisnode/opa-java-client/blob/master/CONTRIBUTING.md).