Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

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 higher

Add library using maven:
```xml

com.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).