Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Statuses #18

Closed
wants to merge 9 commits into from
Closed
2 changes: 1 addition & 1 deletion api-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.smartling</groupId>
<artifactId>smartling-api-sdk</artifactId>
<version>3.0.0</version>
<version>3.2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<scm>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
*/
package com.smartling.api.sdk;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.Data;
import com.smartling.api.sdk.util.DateTypeAdapter;
import com.smartling.api.sdk.util.HttpUtils;

import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
Expand All @@ -33,9 +29,9 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.smartling.api.sdk.dto.ApiCode.SUCCESS;
import static com.smartling.api.sdk.file.FileApiParams.API_KEY;
import static com.smartling.api.sdk.file.FileApiParams.PROJECT_ID;

Expand All @@ -52,8 +48,6 @@ public abstract class BaseApiClientAdapter

private HttpUtils httpUtils;

protected static final String SUCCESS_CODE = "SUCCESS";

protected String baseApiUrl;
protected String apiKey;
protected String projectId;
Expand Down Expand Up @@ -168,7 +162,7 @@ protected String buildParamsQuery(final NameValuePair... nameValuePairs)

protected List<NameValuePair> getRequiredParams()
{
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
final List<NameValuePair> qparams = new ArrayList<>();
qparams.add(new BasicNameValuePair(API_KEY, apiKey));
qparams.add(new BasicNameValuePair(PROJECT_ID, projectId));

Expand All @@ -185,7 +179,7 @@ protected List<BasicNameValuePair> getNameValuePairs(final String name, final Li
if (null == values || values.isEmpty())
return Collections.emptyList();

final List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
final List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
for (final String value : values)
nameValuePairs.add(new BasicNameValuePair(name, value));

Expand All @@ -196,20 +190,14 @@ protected String getApiResponseMessages(final ApiResponse<?> apiResponse)
{
String responseMessages = StringUtils.EMPTY;

if (!SUCCESS_CODE.equals(apiResponse.getCode()))
if (SUCCESS != apiResponse.getCode())
responseMessages = String.format(RESPONSE_MESSAGES, StringUtils.join(apiResponse.getMessages(), ", "));

return responseMessages;
}

protected <T extends Data> ApiResponse<T> getApiResponse(final String response, final TypeToken<ApiResponseWrapper<T>> responseType)
{
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

final Gson gson = builder.create();
final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

return responseWrapper.getResponse();
return JsonReader.getApiResponse(response, responseType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
*/
package com.smartling.api.sdk;

import static com.smartling.api.sdk.file.FileApiParams.CONDITIONS;
import static com.smartling.api.sdk.file.FileApiParams.FILE_TYPES;
import static com.smartling.api.sdk.file.FileApiParams.FILE_URI;
import static com.smartling.api.sdk.file.FileApiParams.LAST_MODIFIED_AFTER;
import static com.smartling.api.sdk.file.FileApiParams.LIMIT;
import static com.smartling.api.sdk.file.FileApiParams.LOCALE;
import static com.smartling.api.sdk.file.FileApiParams.OFFSET;
import static com.smartling.api.sdk.file.FileApiParams.ORDERBY;
import static com.smartling.api.sdk.file.FileApiParams.LAST_UPLOADED_AFTER;
import static com.smartling.api.sdk.file.FileApiParams.LAST_UPLOADED_BEFORE;
import static com.smartling.api.sdk.file.FileApiParams.URI_MASK;
import static com.smartling.api.sdk.file.FileApiParams.NEW_FILE_URI;

import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
Expand All @@ -38,19 +25,13 @@
import com.smartling.api.sdk.dto.file.StringResponse;
import com.smartling.api.sdk.dto.file.UploadFileData;
import com.smartling.api.sdk.exceptions.ApiException;
import com.smartling.api.sdk.util.DateFormatter;
import com.smartling.api.sdk.file.FileApiParams;
import com.smartling.api.sdk.file.FileListSearchParams;
import com.smartling.api.sdk.file.FileType;
import com.smartling.api.sdk.file.RetrievalType;
import com.smartling.api.sdk.file.parameters.FileUploadParameterBuilder;
import com.smartling.api.sdk.file.parameters.GetFileParameterBuilder;

import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;

import com.smartling.api.sdk.util.DateFormatter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -65,6 +46,26 @@
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.message.BasicNameValuePair;

import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.smartling.api.sdk.file.FileApiParams.CONDITIONS;
import static com.smartling.api.sdk.file.FileApiParams.FILE_TYPES;
import static com.smartling.api.sdk.file.FileApiParams.FILE_URI;
import static com.smartling.api.sdk.file.FileApiParams.LAST_MODIFIED_AFTER;
import static com.smartling.api.sdk.file.FileApiParams.LAST_UPLOADED_AFTER;
import static com.smartling.api.sdk.file.FileApiParams.LAST_UPLOADED_BEFORE;
import static com.smartling.api.sdk.file.FileApiParams.LIMIT;
import static com.smartling.api.sdk.file.FileApiParams.LOCALE;
import static com.smartling.api.sdk.file.FileApiParams.NEW_FILE_URI;
import static com.smartling.api.sdk.file.FileApiParams.OFFSET;
import static com.smartling.api.sdk.file.FileApiParams.ORDERBY;
import static com.smartling.api.sdk.file.FileApiParams.URI_MASK;

/**
* Base implementation of the {@link FileApiClientAdapter}.
*/
Expand Down Expand Up @@ -133,7 +134,7 @@ public StringResponse getFile(final GetFileParameterBuilder getFileParameterBuil
final HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_API_URL, params));

final StringResponse stringResponse = getHttpUtils().executeHttpCall(getRequest, proxyConfiguration);
logger.debug(String.format("Get file: %s", SUCCESS_CODE));
logger.debug("Get file: SUCCESS");

return stringResponse;
}
Expand Down Expand Up @@ -267,7 +268,7 @@ private ApiResponse<UploadFileData> uploadFile(final FileUploadParameterBuilder

private String buildFileListParams(final FileListSearchParams fileListSearchParams)
{
final List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
final List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair(LOCALE, fileListSearchParams.getLocale()));
nameValuePairs.add(new BasicNameValuePair(URI_MASK, fileListSearchParams.getUriMask()));
nameValuePairs.add(new BasicNameValuePair(LAST_UPLOADED_AFTER, DateFormatter.format(fileListSearchParams.getLastUploadedAfter())));
Expand Down
23 changes: 23 additions & 0 deletions api-sdk/src/main/java/com/smartling/api/sdk/JsonReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.smartling.api.sdk;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.Data;
import com.smartling.api.sdk.util.DateTypeAdapter;

import java.util.Date;

public class JsonReader {
public static <T extends Data> ApiResponse<T> getApiResponse(String response, TypeToken<ApiResponseWrapper<T>> responseType) {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

final Gson gson = builder.create();
final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

return responseWrapper.getResponse();
}
}
6 changes: 6 additions & 0 deletions api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.smartling.api.sdk.dto;

public enum ApiCode
{
SUCCESS, VALIDATION_ERROR, AUTHENTICATION_ERROR, AUTHORIZATION_ERROR, RESOURCE_LOCKED, MAX_OPERATIONS_LIMIT_EXCEEDED, GENERAL_ERROR, MAINTENANCE_MODE_ERROR, NETWORK_ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package com.smartling.api.sdk.dto;

import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.List;

// TODO(AShesterov): refactor API-SDK: rename ApiResponse to SmartlingApiResponse

// TODO(AShesterov): refactor API-SDK: do NOT use generics for response, but OOP! e.g. UploadFileDataApiResponse extends ApiResponse
Expand All @@ -31,7 +32,7 @@
public class ApiResponse<T extends Data>
{
private T data;
private String code;
private ApiCode code;
private List<String> messages;

/**
Expand All @@ -47,15 +48,17 @@ public T getData()

/**
* The response code returned from the Smartling Translation API.
*
* @return response code
*/
public String getCode()
public ApiCode getCode()
{
return code;
}

/**
* The messages returned form the Smartling Translation API.
*
* @return list of messages.
*/
public List<String> getMessages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@
package com.smartling.api.sdk.exceptions;

// TODO(AShesterov): refactor API-SDK: rename ApiException to SmartlingApiException

import com.smartling.api.sdk.dto.ApiCode;
import org.apache.commons.lang3.StringUtils;

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

/**
* Thrown when an exception has occurred when using the {@link com.smartling.api.sdk.FileApiClientAdapter}.
*/
public class ApiException extends Exception
{
private static final long serialVersionUID = -397098626101615761L;

public ApiException(final String message)
private ApiCode apiCode;
private int httpCode;
private List<String> messages = new ArrayList<>();

ApiException(List<String> messages, final ApiCode apiCode, int httpCode)
{
super(message);
super(StringUtils.join(messages, " ,"));
this.messages = messages;
this.apiCode = apiCode;
this.httpCode = httpCode;
}

public ApiException(final Exception e)
ApiException(final Exception e, ApiCode apiCode)
{
super(e);
messages.add(e.getMessage());
this.apiCode = apiCode;
}

public ApiCode getApiCode()
{
return apiCode;
}

public int getHttpCode()
{
return httpCode;
}

public List<String> getMessages()
{
return messages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smartling.api.sdk.exceptions;

import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.JsonReader;
import com.smartling.api.sdk.dto.ApiCode;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.EmptyResponse;

import java.io.IOException;
import java.util.List;

public class ApiExceptionBuilder
{
public ApiException newException(String contents, int httpCode)
{
ApiResponse<EmptyResponse> apiResponse = JsonReader.getApiResponse(contents, new TypeToken<ApiResponseWrapper<EmptyResponse>>()
{
}
);
ApiCode apiCode = apiResponse.getCode();
List<String> messages = apiResponse.getMessages();
return new ApiException(messages, apiCode, httpCode);
}

public ApiException newException(IOException e) {
return new ApiException(e, ApiCode.NETWORK_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public class FileApiParams
public static final String LOCALES_TO_APPROVE = "localesToApprove";
public static final String OVERWRITE_APPROVED_LOCALES = "overwriteApprovedLocales";
public static final String INCLUDE_ORIGINAL_STRINGS = "includeOriginalStrings";
public static final String CLIENT_LIB_ID = "smartling.client_lib_id";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class FileUploadParameterBuilder implements ParameterBuilder
private String fileUri;
private Boolean approveContent;
private String callbackUrl;
private String clientUid;
private List<String> localesToApprove;
private Boolean overwriteApprovedLocales;
private Map<String, String> directives;
Expand Down Expand Up @@ -110,6 +111,19 @@ public FileUploadParameterBuilder fileType(final FileType fileType)
return this;
}

/**
* library UID that uses sdk for sending files
* if UID is not provided default UID that is equal to sdk library UID is set
* @param name
* @param version
* @return
*/
public FileUploadParameterBuilder clientUid(final String name, final String version)
{
this.clientUid = ProjectPropertiesHolder.clientUid(name, version);
return this;
}

public FileType getFileType()
{
return fileType;
Expand All @@ -128,6 +142,7 @@ public List<NameValuePair> getNameValueList()
paramsList.addAll(convertLocalesBasedApproveParams(FileApiParams.LOCALES_TO_APPROVE, localesToApprove));
if (overwriteApprovedLocales != null)
paramsList.add(new BasicNameValuePair(FileApiParams.OVERWRITE_APPROVED_LOCALES, overwriteApprovedLocales.toString()));
paramsList.add(new BasicNameValuePair(FileApiParams.CLIENT_LIB_ID, clientUid != null ? clientUid : ProjectPropertiesHolder.defaultClientUid()));

paramsList.addAll(convertMapParams(directives));

Expand Down
Loading