Code Monkey home page Code Monkey logo

joystick-java's Introduction

Java Client for Joystick Remote Config

This is a library that simplifies communicating with the Joystick API for using remote configurations with your Java project. With remote configurations, you can instantly change the behavior and appearance of your application without code changes.

Joystick is a modern remote config platform where you manage all of your configurable parameters. We are natively multi-environment, preserve your version history, have advanced work-flow & permissions management, and much more. Have one API to use for any JSON configs.

Provided client is supporting Java 8+.

Setup

Before using the Joystick Java SDK in your project, you need to add maven dependency:

<dependency>
    <groupId>com.getjoystick</groupId>
    <artifactId>joystick-sdk</artifactId>
    <version>0.1.0</version>
</dependency>

Usage

Using Joystick to get remote configurations in your Java project is a breeze.

// Add imports
import com.fasterxml.jackson.databind.JsonNode;
import com.getjoystick.sdk.Joystick;
import com.getjoystick.sdk.client.Client;
import com.getjoystick.sdk.client.ClientConfig;
import com.getjoystick.sdk.util.JoystickMapper;
.....

// Get apiKey from environment variables java
String joystickApiKey = System.getenv("JOYSTICK_API_KEY");

// Initialize a client with a Joystick API Key
ClientConfig config = ClientConfig.builder().setApiKey(joystickApiKey).build();
Client client = Joystick.create(config);

// Request a single configuration as com.fasterxml.jackson.databind.JsonNode object
JsonNode content = client.getContent("idOfMyContent");

// Transform joystick response to custom class
MyCustomClass myObj = JoystickMapper.toObject(content, MyCustomClass.class);

// Request multiple configurations at the same time
List<String> contentIds = ImmutableList.of("contentId1", "contentId2");
Map<String, JsonNode> contentsMap = client.getContents(contentIds);

Specifying Additional Parameters

When creating the Joystick client, you can specify additional parameters which will be used by all API calls to the Joystick API. These additional parameters are used for AB Testing (userId), segmentation (params), and backward-compatible version delivery (semVer).

For more details see API documentation.

// Initializing a client with options
ClientConfig config = ClientConfig.builder()
    .setApiKey("API_KEY")
    .setUserId("userId") // Any string
    .setSemVer("0.0.1") // String in the format of semantic versioning
    .setParams(ImmutableMap.of("Location", "Earth")) // Key-value pairs of strings, numbers, or booleans
    .build();

Various Ways of Getting Configuration Content

With our Joystick java SDK, you can easily get the configuration response in different ways.

Single Configuration

Response Config as JSON Config as Serialized String
Config content only getContent getContentSerialized
Config content with additional meta data getFullContent getFullContentSerialized

Multiple Configurations

Response Config as JSON Config as Serialized String
Multiple configs content only getContents getContentsSerialized
Multiple configs content with additional meta data getFullContents getFullContentsSerialized

Error handling

The client can raise different types of exceptions with the base class of JoystickException.

try {
    Map<String, JsonNode> contentsMap = client.getContents(
        ImmutableList.of("contentId1", "contentId2")
    );
}
catch (ApiHttpException e) {
    // Handle HTTP error (i.e. timeout, or invalid HTTP code)
}
catch (MultipleContentsApiException e) {
    // Handle API exception (i.e. content is not found, or some of the keys can't be retrieved)
}

Caching

By default, the client uses ApiCacheLRU, based on guava in memory Cache.

You can specify your own cache implementation by implementing the interface ApiCache. Sample code snippet could be found in examples

public class CaffeineCustomCache<K, V> implements ApiCache<K, V> {
    //implementation of base methods
}

// Setting custom cache
ClientConfig config = ClientConfig.builder()
    .setApiKey("API_KEY")
    .setCache(new CaffeineCustomCache<>())
    .build();

Default cache expiration is set to 300 seconds. It can be changed during ClientConfig build

// Setting CacheExpirationSeconds
ClientConfig config = ClientConfig.builder()
    .setApiKey("API_KEY")
    .setCacheExpirationSeconds(600)
    .build();

Refresh option

To ignore the existing cache when requesting a config, pass this option as true.

JoystickContentOptions options = new JoystickContentOptions(true);

JsonNode content = client.getContent("idOfMyContent", options);

// OR

Map<String, JsonNode> contentsMap = client.getContents(
        ImmutableList.of("contentId1", "contentId2"),
        options
    );

License

The MIT. Please see License File for more information.

joystick-java's People

Contributors

joystickteam avatar valeriymaslenikov avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.