Skip to content

Commit

Permalink
feat(smartcar): added support for simulated mode and feature flags (#111
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mdheri authored Jul 25, 2022
1 parent ef0ae82 commit 0ab21cf
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 36 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ The recommended method for obtaining the SDK is via Gradle or Maven through the

### Gradle
```groovy
compile "com.smartcar.sdk:java-sdk:3.2.3"
compile "com.smartcar.sdk:java-sdk:3.3.0"
```

### Maven
```xml
<dependency>
<groupId>com.smartcar.sdk</groupId>
<artifactId>java-sdk</artifactId>
<version>3.2.3</version>
<version>3.3.0</version>
</dependency>
```

### Jar Direct Download
* [java-sdk-3.2.3.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.2.3/java-sdk-3.2.3.jar)
* [java-sdk-3.2.3-sources.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.2.3/java-sdk-3.2.3-sources.jar)
* [java-sdk-3.2.3-javadoc.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.2.3/java-sdk-3.2.3-javadoc.jar)
* [java-sdk-3.3.0.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.3.0/java-sdk-3.3.0.jar)
* [java-sdk-3.3.0-sources.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.3.0/java-sdk-3.3.0-sources.jar)
* [java-sdk-3.3.0-javadoc.jar](https://search.maven.org/remotecontent?filepath=com/smartcar/sdk/java-sdk/3.3.0/java-sdk-3.3.0-javadoc.jar)

Signatures and other downloads available at [Maven Central](https://search.maven.org/artifact/com.smartcar.sdk/java-sdk/3.2.3/jar).
Signatures and other downloads available at [Maven Central](https://search.maven.org/artifact/com.smartcar.sdk/java-sdk/3.3.0/jar).

## Usage

Expand All @@ -53,14 +53,14 @@ a valid access token for the target vehicle.
String clientSecret = "";
String redirectUri = "";
String[] scope = {};
boolean testMode = true;
String mode = "test";

// Initialize a new AuthClient with your credentials.
AuthClient authClient = new AuthClient.Builder
.clientId(clientId)
.clientSecret(clientSecret)
.redirectUri(redirectUri)
.testMode(testMode);
.mode(mode);

// Retrieve the auth URL to start the OAuth flow.
String authUrl = authClient.authUrlBuilder(scope)
Expand Down Expand Up @@ -136,5 +136,5 @@ In accordance with the Semantic Versioning specification, the addition of suppor
[ci-url]: https://travis-ci.com/smartcar/java-sdk
[coverage-image]: https://codecov.io/gh/smartcar/java-sdk/branch/master/graph/badge.svg?token=nZAITx7w3X
[coverage-url]: https://codecov.io/gh/smartcar/java-sdk
[javadoc-image]: https://img.shields.io/badge/javadoc-3.2.3-brightgreen.svg
[javadoc-image]: https://img.shields.io/badge/javadoc-3.3.0-brightgreen.svg
[javadoc-url]: https://smartcar.github.io/java-sdk
4 changes: 2 additions & 2 deletions README.mdt
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ a valid access token for the target vehicle.
String clientSecret = "";
String redirectUri = "";
String[] scope = {};
boolean testMode = true;
String mode = "test";

// Initialize a new AuthClient with your credentials.
AuthClient authClient = new AuthClient.Builder
.clientId(clientId)
.clientSecret(clientSecret)
.redirectUri(redirectUri)
.testMode(testMode);
.mode(mode);

// Retrieve the auth URL to start the OAuth flow.
String authUrl = authClient.authUrlBuilder(scope)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
libGroup=com.smartcar.sdk
libName=java-sdk
libVersion=3.2.3
libVersion=3.3.0
libDescription=Smartcar Java SDK
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public static String getWebhookId() {
* Creates an AuthClient builder and sets Client ID, Client Secret, and Redirect URI and also
* enables test mode.
*/
public static AuthClient.Builder getConfiguredAuthClientBuilder() {
public static AuthClient.Builder getConfiguredAuthClientBuilder() throws Exception {
return new AuthClient.Builder()
.clientId(getClientId())
.clientSecret(getClientSecret())
.redirectUri("https://example.com/auth")
.testMode(true);
.mode("test");
}

public static String runAuthFlow(String authorizeURL) {
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/smartcar/sdk/AuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Smartcar OAuth 2.0 Authentication Client */
public class AuthClient {
Expand Down Expand Up @@ -43,7 +45,7 @@ public Auth deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
private final String clientId;
private final String clientSecret;
private final String redirectUri;
private final boolean testMode;
private final String mode;

/**
* Builds a new AuthClient.
Expand All @@ -53,13 +55,14 @@ public static class Builder {
private String clientId;
private String clientSecret;
private String redirectUri;
private boolean testMode;
private String mode;
private final Set<String> validModes = Stream.of("test", "live", "simulated").collect(Collectors.toSet());

public Builder() {
this.clientId = System.getenv("SMARTCAR_CLIENT_ID");
this.clientSecret = System.getenv("SMARTCAR_CLIENT_SECRET");
this.redirectUri = System.getenv("SMARTCAR_REDIRECT_URI");
this.testMode = false;
this.mode = "live";
}

public Builder clientId(String clientId) {
Expand All @@ -77,11 +80,27 @@ public Builder redirectUri(String redirectUri) {
return this;
}

/**
* @deprecated use {@link Builder#mode(String)} instead.
*/
@Deprecated
public Builder testMode(boolean testMode) {
this.testMode = testMode;
this.mode = testMode ? "test" : "live";
return this;
}

public Builder mode(String mode) throws Exception {
if (!this.validModes.contains(mode)) {
throw new Exception(
"The \"mode\" parameter MUST be one of the following: \"test\", \"live\", \"simulated\""
);
}

this.mode = mode;
return this;
}


public AuthClient build() throws Exception {
if (this.clientId == null) {
throw new Exception("clientId must be defined");
Expand All @@ -100,7 +119,7 @@ private AuthClient(Builder builder) {
this.clientId = builder.clientId;
this.clientSecret = builder.clientSecret;
this.redirectUri = builder.redirectUri;
this.testMode = builder.testMode;
this.mode = builder.mode;

ApiClient.gson.registerTypeAdapter(Auth.class, new AuthDeserializer());
}
Expand All @@ -120,6 +139,7 @@ public AuthUrlBuilder authUrlBuilder(String[] scope) {
*/
public class AuthUrlBuilder {
private HttpUrl.Builder urlBuilder;
private String mode = AuthClient.this.mode;
private List<String> flags = new ArrayList<>();

public AuthUrlBuilder(String[] scope) {
Expand All @@ -133,7 +153,7 @@ public AuthUrlBuilder(String[] scope) {
.addQueryParameter("response_type", "code")
.addQueryParameter("client_id", AuthClient.this.clientId)
.addQueryParameter("redirect_uri", AuthClient.this.redirectUri)
.addQueryParameter("mode", AuthClient.this.testMode ? "test" : "live")
.addQueryParameter("mode", AuthClient.this.mode)
.addQueryParameter("scope", Utils.join(scope, " "));
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/smartcar/sdk/Smartcar.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public static Compatibility getCompatibility(SmartcarCompatibilityRequest compat
if (compatibilityRequest.getFlags() != null) {
urlBuilder.addQueryParameter("flags", compatibilityRequest.getFlags());
}
if (compatibilityRequest.getTestMode()) {
urlBuilder.addQueryParameter("mode", "test");
if (compatibilityRequest.getMode() != null) {
urlBuilder.addQueryParameter("mode", compatibilityRequest.getMode());
}
if (compatibilityRequest.getTestModeCompatibilityLevel() != null) {
urlBuilder.addQueryParameter("test_mode_compatibility_level", compatibilityRequest.getTestModeCompatibilityLevel());
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/com/smartcar/sdk/SmartcarCompatibilityRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Class encompassing optional arguments for Smartcar compatibility requests
Expand All @@ -14,7 +17,7 @@ public final class SmartcarCompatibilityRequest {
private final String flags;
private final String clientId;
private final String clientSecret;
private final boolean testMode;
private final String mode;
private final String testModeCompatibilityLevel;

public static class Builder {
Expand All @@ -25,8 +28,9 @@ public static class Builder {
private final List<String> flags;
private String clientId;
private String clientSecret;
private boolean testMode;
private String mode;
private String testModeCompatibilityLevel;
private final Set<String> validModes = Stream.of("test", "live", "simulated").collect(Collectors.toSet());

public Builder() {
this.vin = "";
Expand All @@ -36,7 +40,7 @@ public Builder() {
this.flags = new ArrayList<>();
this.clientId = System.getenv("SMARTCAR_CLIENT_ID");
this.clientSecret = System.getenv("SMARTCAR_CLIENT_SECRET");
this.testMode = false;
this.mode = null;
this.testModeCompatibilityLevel = null;
}

Expand Down Expand Up @@ -80,13 +84,28 @@ public Builder clientSecret(String clientSecret) {
return this;
}

/**
* @deprecated use {@link #mode(String)} instead.
*/
@Deprecated
public Builder testMode(boolean testMode) {
this.testMode = testMode;
this.mode = testMode ? "test" : "live";
return this;
}

public Builder mode(String mode) throws Exception {
if (!this.validModes.contains(mode)) {
throw new Exception(
"The \"mode\" parameter MUST be one of the following: \"test\", \"live\", \"simulated\""
);
}

this.mode = mode;
return this;
}

public Builder testModeCompatibilityLevel(String level) {
this.testMode = true;
this.mode = "test";
this.testModeCompatibilityLevel = level;
return this;
}
Expand Down Expand Up @@ -115,7 +134,7 @@ private SmartcarCompatibilityRequest(Builder builder) {
}
this.clientId = builder.clientId;
this.clientSecret = builder.clientSecret;
this.testMode = builder.testMode;
this.mode = builder.mode;
this.testModeCompatibilityLevel = builder.testModeCompatibilityLevel;
}

Expand All @@ -135,7 +154,13 @@ public String getVin() {

public String getClientSecret() { return this.clientSecret; }

public boolean getTestMode() { return this.testMode; }
/**
* @deprecated use {@link Builder#getMode()} which returns the mode as a String.
*/
@Deprecated
public boolean getTestMode() { return this.mode.equals("test"); }

public String getMode() { return this.mode; }

public String getTestModeCompatibilityLevel() { return this.testModeCompatibilityLevel; }
}
}
26 changes: 25 additions & 1 deletion src/main/java/com/smartcar/sdk/SmartcarVehicleOptions.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.smartcar.sdk;

import java.util.ArrayList;
import java.util.List;

/** Class encompassing any optional arguments for constructing a new Vehicle instance */
public final class SmartcarVehicleOptions {
private final String version;
private final Vehicle.UnitSystem unitSystem;
private final String origin;
private final String flags;

public static class Builder {
private String version;
private Vehicle.UnitSystem unitSystem;
private String origin;
private final List<String> flags;

public Builder() {
this.version = "2.0";
this.unitSystem = Vehicle.UnitSystem.METRIC;
this.origin = Smartcar.getApiOrigin();
this.flags = new ArrayList<>();
}

public Builder version(String version) {
Expand All @@ -27,6 +33,16 @@ public Builder unitSystem(Vehicle.UnitSystem unitSystem) {
return this;
}

public Builder addFlag(String key, String value) {
this.flags.add(key + ":" + value);
return this;
}

public Builder addFlag(String key, boolean value) {
this.flags.add(key + ":" + value);
return this;
}

public Builder origin(String origin) {
this.origin = origin;
return this;
Expand All @@ -41,6 +57,11 @@ private SmartcarVehicleOptions(Builder builder) {
this.version = builder.version;
this.unitSystem = builder.unitSystem;
this.origin = builder.origin;
if (!builder.flags.isEmpty()) {
this.flags = String.join(" ", builder.flags);
} else {
this.flags = null;
}
}

public String getVersion() {
Expand All @@ -51,8 +72,11 @@ public Vehicle.UnitSystem getUnitSystem() {
return this.unitSystem;
}

public String getFlags() {
return this.flags;
}

public String getOrigin() {
return this.origin;
}
}

Loading

0 comments on commit 0ab21cf

Please sign in to comment.