Skip to content

Ouest-France/bp-contract-generator-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bp-contract-generator-java

We are aware that writing a block provider contract is clearly a pain in the ass. Therefore, to limit this frustration, and to let you focus on your business, we offer you a simple fluid API to help you.

This is an easy and programmatic way to generate block provider contracts. Contracts are valid by design and will fit perfectly with the BMS.

This API helps you to declare multiple block types with theirs endpoint, parameters and associated templates.

You simply need to generate the object and let your favorite JSON framework serialize it.

How to install

<dependency>
    <groupId>io.github.ouest-france</groupId>
    <artifactId>bp-contract-generator</artifactId>
    <version>1.0.0</version>
</dependency>
Note
the lib require Java 8 and does not provide any serialization tool

Getting started

Here is an example with a block type that takes an input text, sends it to a remote service (for demonstration only), and outputs the bolded text with different templates.

The following example assumes Spring is available on project, and Jackson has been properly configured.

@RequestMapping("/block-provider")
@RestController
public class BlockProviderResource {

    @GetMapping("/configurations")
    public ResponseEntity<List<BlockType>> configurations() {
        return ResponseEntity.ok(
                BlockProviderGenerator.create()
                        // the block type
                        .addType("bold").withVersion(1, 0, 0)

                        // declare the endpoint url
                        .withEndpoint("https://localhost/block-configurations", "GET", "pure")

                        // the input text
                        .addParameter("text")
                        .configurableAsString()
                        .required()
                        .withTitle("Text")
                        .withDescription("Text to display as bold")

                        // send this parameter to the endpoint as a query parameter
                        .inEndpoint(QUERY, STRING)
                        .registerParameter()

                        // first template to output using html tag
                        .addTemplate("html-tag")
                        .withSource("<b>{{ text }}<b>")
                        .registerTemplate()

                        // second template to output using css style
                        .addTemplate("html-css")
                        .withSource("<span style=\"font-weight: bold;\">{{ text }}</span>")
                        .registerTemplate()

                        .registerType()
                        .generate()
        );
    }

}

You might find many other exampls in the test directory link:src/test/java/sipa/blockprovider/examples/