Skip to content

Commit

Permalink
Merge pull request #149 from smartcar/Service-History-Update-SDKs-jav…
Browse files Browse the repository at this point in the history
…a-2-1207118124320958

include a new method serviceHistory
  • Loading branch information
aytekin-smartcar authored May 9, 2024
2 parents ef42277 + dc0acfa commit b7ef1ee
Show file tree
Hide file tree
Showing 11 changed files with 494 additions and 152 deletions.
14 changes: 7 additions & 7 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:4.4.0"
compile "com.smartcar.sdk:java-sdk:4.5.0"
```

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

### Jar Direct Download
* [java-sdk-4.4.0.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.4.0/java-sdk-4.4.0.jar)
* [java-sdk-4.4.0-sources.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.4.0/java-sdk-4.4.0-sources.jar)
* [java-sdk-4.4.0-javadoc.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.4.0/java-sdk-4.4.0-javadoc.jar)
* [java-sdk-4.5.0.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.5.0/java-sdk-4.5.0.jar)
* [java-sdk-4.5.0-sources.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.5.0/java-sdk-4.5.0-sources.jar)
* [java-sdk-4.5.0-javadoc.jar](https://repo1.maven.org/maven2/com/smartcar/sdk/java-sdk/4.5.0/java-sdk-4.5.0-javadoc.jar)

Signatures and other downloads available at [Maven Central](https://central.sonatype.com/artifact/com.smartcar.sdk/java-sdk/4.4.0).
Signatures and other downloads available at [Maven Central](https://central.sonatype.com/artifact/com.smartcar.sdk/java-sdk/4.5.0).

## Usage

Expand Down Expand Up @@ -136,7 +136,7 @@ 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-4.4.0-brightgreen.svg
[javadoc-image]: https://img.shields.io/badge/javadoc-4.5.0-brightgreen.svg
[javadoc-url]: https://smartcar.github.io/java-sdk
[maven-image]: https://img.shields.io/maven-central/v/com.smartcar.sdk/java-sdk.svg?label=Maven%20Central
[maven-url]: https://central.sonatype.com/artifact/com.smartcar.sdk/java-sdk
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=4.4.0
libVersion=4.5.0
libDescription=Smartcar Java SDK
102 changes: 68 additions & 34 deletions src/main/java/com/smartcar/sdk/ApiClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.smartcar.sdk;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.smartcar.sdk.data.ApiData;
import com.smartcar.sdk.data.Meta;
import okhttp3.*;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand All @@ -15,7 +24,8 @@ abstract class ApiClient {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

/**
* Retrieves the SDK version, falling back to DEVELOPMENT if we're not running from a jar.
* Retrieves the SDK version, falling back to DEVELOPMENT if we're not running
* from a jar.
*
* @return the SDK version
*/
Expand All @@ -29,35 +39,33 @@ private static String getSdkVersion() {
return version;
}

protected static final String USER_AGENT =
String.format(
"Smartcar/%s (%s; %s) Java v%s %s",
getSdkVersion(),
System.getProperty("os.name"),
System.getProperty("os.arch"),
System.getProperty("java.version"),
System.getProperty("java.vm.name"));
protected static final String USER_AGENT = String.format(
"Smartcar/%s (%s; %s) Java v%s %s",
getSdkVersion(),
System.getProperty("os.name"),
System.getProperty("os.arch"),
System.getProperty("java.version"),
System.getProperty("java.vm.name"));

private static final OkHttpClient client =
new OkHttpClient.Builder().readTimeout(310, TimeUnit.SECONDS).build();
private static final OkHttpClient client = new OkHttpClient.Builder().readTimeout(310, TimeUnit.SECONDS).build();


static GsonBuilder gson =
new GsonBuilder().setFieldNamingStrategy((field) -> Utils.toCamelCase(field.getName()));
static GsonBuilder gson = new GsonBuilder().setFieldNamingStrategy((field) -> Utils.toCamelCase(field.getName()));

/**
* Builds a request object with common headers, using provided request parameters
* @param url url for the request, including the query parameters
* @param method http method
* @param body request body
* Builds a request object with common headers, using provided request
* parameters
*
* @param url url for the request, including the query parameters
* @param method http method
* @param body request body
* @param headers additional headers to set for the request
* @return
*/
protected static Request buildRequest(HttpUrl url, String method, RequestBody body, Map<String, String> headers) {
Request.Builder request = new Request.Builder()
.url(url)
.addHeader("User-Agent", ApiClient.USER_AGENT)
.method(method, body);
.url(url)
.addHeader("User-Agent", ApiClient.USER_AGENT)
.method(method, body);

headers.forEach(request::addHeader);

Expand Down Expand Up @@ -86,45 +94,71 @@ protected static Response execute(Request request) throws SmartcarException {
}

/**
* Sends the specified request, parsing the response into the specified type. Wraps the request
* Sends the specified request, parsing the response into the specified type.
* Wraps the request
* with the unitSystem and age meta data.
*
* @param <T> the data container for the parsed response JSON
* @param request the desired request to transmit
* @param <T> the data container for the parsed response JSON
* @param request the desired request to transmit
* @param dataType the type into which the response will be parsed
* @return the wrapped response
* @throws SmartcarException if the request is unsuccessful
*/
protected static <T extends ApiData> T execute(
Request request, Class<T> dataType) throws SmartcarException {
Response response = ApiClient.execute(request);
T data;
T data = null;
Meta meta;
String bodyString = "";

try {
bodyString = response.body().string();
data = ApiClient.gson.create().fromJson(bodyString, dataType);

JsonElement jsonElement = JsonParser.parseString(bodyString);
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

if (jsonElement.isJsonArray()) {
Field itemsField = dataType.getDeclaredField("items");
itemsField.setAccessible(true);

Type genericFieldType = itemsField.getGenericType();
if (genericFieldType instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) genericFieldType;
Type[] fieldArgTypes = pType.getActualTypeArguments();
if (fieldArgTypes.length > 0) {
Type listTypeArgument = fieldArgTypes[0];

Type listType = TypeToken.getParameterized(List.class, listTypeArgument).getType();
List<?> list = gson.fromJson(jsonElement, listType);

T dataInstance = dataType.getDeclaredConstructor().newInstance();
itemsField.set(dataInstance, list);
data = dataInstance; // Depending on your method's return type and needs
}
}
} else {
data = ApiClient.gson.create().fromJson(bodyString, dataType);
}

Headers headers = response.headers();
JsonObject headerJson = new JsonObject();
for (String header: response.headers().names()) {
for (String header : headers.names()) {
headerJson.addProperty(header.toLowerCase(), headers.get(header));
}
String headerJsonString = headerJson.toString();
meta = ApiClient.gson.create().fromJson(headerJsonString, Meta.class);
data.setMeta(meta);
return data;
} catch (Exception ex) {
if (bodyString.equals("")) {
bodyString = "Empty response body";
}
throw new SmartcarException.Builder()
.statusCode(response.code())
.description(bodyString)
.requestId(response.headers().get("sc-request-id"))
.type("SDK_ERROR")
.build();
.statusCode(response.code())
.description(bodyString)
.requestId(response.headers().get("sc-request-id"))
.type("SDK_ERROR")
.build();
}

return data;
}
}
Loading

0 comments on commit b7ef1ee

Please sign in to comment.