Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: various changes related to documentation and getting java build in order #12

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Flipt Server SDKs

This repository centralizes the server-side SDKs for [Flipt](https://github.com/flipt-io/flipt).
This repository is a monorepo which contains the source code for SDKs in various languages. Each client interacts directly with an upstream server and can perform one of three of the following operations via HTTP:

1. [Variant](https://github.com/flipt-io/flipt/blob/main/internal/server/evaluation/evaluation.go#L24)
2. [Boolean](https://github.com/flipt-io/flipt/blob/main/internal/server/evaluation/evaluation.go#L90)
3. [Batch](https://github.com/flipt-io/flipt/blob/main/internal/server/evaluation/evaluation.go#L248)

## Language Support

This list is highly subject to change as our list of clients will grow over time. Currently, we support the following languages:

1. [Python](./flipt-client-python)
2. [NodeJS](./flipt-client-node)
3. [Java](./flipt-client-java)
4. [Rust](./flipt-client-rust)

## Installation

Please refer to each individual language client's README to see how you can install and use it.
53 changes: 39 additions & 14 deletions build/main.go
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ var (
"python": pythonBuild,
"rust": rustBuild,
"node": nodeBuild,
"java": javaBuild,
}
sema = make(chan struct{}, 4)
)

func init() {
@@ -75,25 +75,14 @@ func run() error {

for _, fn := range tests {
fn := fn
g.Go(take(func() error {
g.Go(func() error {
return fn(ctx, client, dir)
}))
})
}

return g.Wait()
}

func take(fn func() error) func() error {
return func() error {
// insert into semaphore channel to maintain
// a max concurrency
sema <- struct{}{}
defer func() { <-sema }()

return fn()
}
}

func pythonBuild(ctx context.Context, client *dagger.Client, hostDirectory *dagger.Directory) error {
container := client.Container().From("python:3.11-bookworm").
WithExec([]string{"pip", "install", "poetry==1.7.0"}).
@@ -180,3 +169,39 @@ func nodeBuild(ctx context.Context, client *dagger.Client, hostDirectory *dagger

return err
}

func javaBuild(ctx context.Context, client *dagger.Client, hostDirectory *dagger.Directory) error {
container := client.Container().From("gradle:8.5.0-jdk11").
WithDirectory("/src", hostDirectory.Directory("flipt-client-java")).
WithWorkdir("/src").
WithExec([]string{"./gradlew", "build"})

var err error

if !push {
_, err = container.Sync(ctx)
return err
}

if os.Getenv("MAVEN_USERNAME") == "" {
return fmt.Errorf("MAVEN_USERNAME is not set")
}
if os.Getenv("MAVEN_PASSWORD") == "" {
return fmt.Errorf("MAVEN_PASSWORD is not set")
}
if os.Getenv("MAVEN_PUBLISH_REGISTRY_URL") == "" {
return fmt.Errorf("MAVEN_PUBLISH_REGISTRY_URL is not set")
}

mavenUsername := client.SetSecret("maven-username", os.Getenv("MAVEN_USERNAME"))
mavenPassword := client.SetSecret("maven-password", os.Getenv("MAVEN_PASSWORD"))
mavenRegistryUrl := client.SetSecret("maven-registry-url", os.Getenv("MAVEN_PUBLISH_REGISTRY_URL"))

_, err = container.WithSecretVariable("MAVEN_USERNAME", mavenUsername).
WithSecretVariable("MAVEN_PASSWORD", mavenPassword).
WithSecretVariable("MAVEN_PUBLISH_REGISTRY_URL", mavenRegistryUrl).
WithExec([]string{"./gradlew", "publish"}).
Sync(ctx)

return err
}
31 changes: 31 additions & 0 deletions flipt-client-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Flipt Client Java

This directory contains the Java source code for the Java server side SDK.

## Installation

### Gradle

Add the dependency in your `build.gradle`:

```groovy
dependencies {
implementation 'io.flipt-io:flipt-java:0.x.x'
}
```

### Maven

Add the dependency in your `pom.xml`:

```xml
<dependency>
<groupId>io.flipt</groupId>
<artifactId>flipt-java</artifactId>
<version>0.x.x</version>
</dependency>
```

## Usage

In the [examples](./examples) directory, there is an example Java program which imports in the flipt client, and uses it appropriately, please refer to that for how to use the client.
5 changes: 5 additions & 0 deletions flipt-client-java/build.gradle
Original file line number Diff line number Diff line change
@@ -11,6 +11,11 @@ repositories {
mavenCentral()
}

java {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
Original file line number Diff line number Diff line change
@@ -5,14 +5,18 @@
import okhttp3.OkHttpClient;

public class FliptClient {
public Evaluation evaluation;
private Evaluation evaluation;

public FliptClient(String url, String token, int timeout) {
OkHttpClient httpClient =
new OkHttpClient.Builder().callTimeout(Duration.ofSeconds(timeout)).build();
this.evaluation = new Evaluation(httpClient, url, token);
}

public Evaluation evaluation() {
return evaluation;
}

public static FliptClientBuilder builder() {
return new FliptClientBuilder();
}
9 changes: 5 additions & 4 deletions flipt-client-java/src/main/java/example/Main.java
Original file line number Diff line number Diff line change
@@ -24,12 +24,13 @@ public static void main(String[] args) {
evaluationRequests.add(errorEvaluationRequest);

VariantEvaluationResponse variantEvaluationResponse =
fliptClient.evaluation.variant(variantEvaluationRequest);
fliptClient.evaluation().variant(variantEvaluationRequest);
BooleanEvaluationResponse booleanEvaluationResponse =
fliptClient.evaluation.booleanEvaluation(booleanEvaluationRequest);
fliptClient.evaluation().booleanEvaluation(booleanEvaluationRequest);

BatchEvaluationResponse batchEvaluationResponse =
fliptClient.evaluation.batch(
new BatchEvaluationRequest(Optional.of(""), evaluationRequests));
fliptClient
.evaluation()
.batch(new BatchEvaluationRequest(Optional.of(""), evaluationRequests));
}
}
14 changes: 7 additions & 7 deletions flipt-client-java/src/test/java/TestFliptClient.java
Original file line number Diff line number Diff line change
@@ -18,13 +18,13 @@ void testVariant() {
Map<String, String> context = new HashMap<>();
context.put("fizz", "buzz");
VariantEvaluationResponse variant =
fc.evaluation.variant(new EvaluationRequest("default", "flag1", "entity", context));
fc.evaluation().variant(new EvaluationRequest("default", "flag1", "entity", context));

Assertions.assertTrue(variant.isMatch());
Assertions.assertEquals("flag1", variant.getFlagKey());
Assertions.assertEquals("MATCH_EVALUATION_REASON", variant.getReason());
Assertions.assertEquals("variant1", variant.getVariantKey());
Assertions.assertEquals("segment1", variant.getSegmentKeys().getFirst());
Assertions.assertEquals("segment1", variant.getSegmentKeys().get(0));
}

@Test
@@ -41,8 +41,8 @@ void testBoolean() {
context.put("fizz", "buzz");

BooleanEvaluationResponse booleanEvaluation =
fc.evaluation.booleanEvaluation(
new EvaluationRequest("default", "flag_boolean", "entity", context));
fc.evaluation()
.booleanEvaluation(new EvaluationRequest("default", "flag_boolean", "entity", context));

Assertions.assertTrue(booleanEvaluation.isEnabled());
Assertions.assertEquals("flag_boolean", booleanEvaluation.getFlagKey());
@@ -75,18 +75,18 @@ void testBatch() {
evaluationRequests.add(errorEvaluationRequest);

BatchEvaluationResponse batch =
fc.evaluation.batch(new BatchEvaluationRequest(Optional.of(""), evaluationRequests));
fc.evaluation().batch(new BatchEvaluationRequest(Optional.of(""), evaluationRequests));

// Variant
EvaluationResponse first = batch.getResponses().getFirst();
EvaluationResponse first = batch.getResponses().get(0);
Assertions.assertEquals("VARIANT_EVALUATION_RESPONSE_TYPE", first.getType());

VariantEvaluationResponse variant = first.getVariantResponse().get();
Assertions.assertTrue(variant.isMatch());
Assertions.assertEquals("flag1", variant.getFlagKey());
Assertions.assertEquals("MATCH_EVALUATION_REASON", variant.getReason());
Assertions.assertEquals("variant1", variant.getVariantKey());
Assertions.assertEquals("segment1", variant.getSegmentKeys().getFirst());
Assertions.assertEquals("segment1", variant.getSegmentKeys().get(0));

// Boolean
EvaluationResponse second = batch.getResponses().get(1);
10 changes: 10 additions & 0 deletions flipt-client-node/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Flipt Client Node

This directory contains the TypeScript source code for the TypeScript server side SDK.

## Installation

```sh
npm i @flipt-io/flipt@{version}
```

## Usage

In the [example](./example) directory, there is an example TypeScript program which imports in the flipt client, and uses it appropriately, please refer to that for how to use the client.
24 changes: 12 additions & 12 deletions flipt-client-node/example/index.ts
Original file line number Diff line number Diff line change
@@ -4,31 +4,31 @@ const fliptClient = new FliptClient();

async function example() {
const variantEvaluationResponse = await fliptClient.evaluation.variant({
namespace_key: "default",
flag_key: "flagll",
entity_id: "entity",
namespaceKey: "default",
flagKey: "flag1",
entityId: "entity",
context: { fizz: "buzz" }
});

const booleanEvaluationResponse = await fliptClient.evaluation.boolean({
namespace_key: "default",
flag_key: "flag_boolean",
entity_id: "entity",
namespaceKey: "default",
flagKey: "flag_boolean",
entityId: "entity",
context: { fizz: "buzz" }
});

const batchEvaluationResponse = await fliptClient.evaluation.batch({
requests: [
{
namespace_key: "default",
flag_key: "flag1",
entity_id: "entity",
namespaceKey: "default",
flagKey: "flag1",
entityId: "entity",
context: { fizz: "buzz" }
},
{
namespace_key: "default",
flag_key: "flag_boolean",
entity_id: "entity",
namespaceKey: "default",
flagKey: "flag_boolean",
entityId: "entity",
context: { fizz: "buzz" }
}
]
4 changes: 2 additions & 2 deletions flipt-client-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions flipt-client-node/src/evaluation/models.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface EvaluationRequest {
namespace_key: string;
flag_key: string;
entity_id: string;
namespaceKey: string;
flagKey: string;
entityId: string;
context: object;
}

38 changes: 32 additions & 6 deletions flipt-client-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import { Evaluation } from "./evaluation";

interface FliptApiClientOptions {
url?: string;
token?: string;
timeout?: number;
}

const defaultFliptClientOptions: FliptApiClientOptions = {
url: "http://localhost:8080",
token: "",
timeout: 60
};

export class FliptClient {
public evaluation: Evaluation;

public constructor(
url: string = "http://localhost:8080",
token: string = "",
timeout: number = 60
) {
this.evaluation = new Evaluation(url, token, timeout);
public constructor(options?: FliptApiClientOptions) {
const clientOptions = {
...defaultFliptClientOptions
};

if (options?.url !== undefined) {
clientOptions.url = options.url;
}
if (options?.token !== undefined) {
clientOptions.token = options.token;
}
if (options?.timeout !== undefined) {
clientOptions.timeout = options.timeout;
}

this.evaluation = new Evaluation(
clientOptions.url,
clientOptions.token,
clientOptions.timeout
);
}
}
Loading