From 748df6894a6537eb0a2914340f5341b197cfa253 Mon Sep 17 00:00:00 2001 From: Oleksii Burov Date: Thu, 1 Oct 2015 16:53:17 +0300 Subject: [PATCH] #14 Extracted common method --- .../api/sdk/BaseApiClientAdapter.java | 2 +- .../api/sdk/FileApiClientAdapterImpl.java | 66 +++++++++---------- .../api/sdk/ProjectApiClientAdapterImpl.java | 2 +- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/BaseApiClientAdapter.java b/api-sdk/src/main/java/com/smartling/api/sdk/BaseApiClientAdapter.java index c1e64bb..4b452b9 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/BaseApiClientAdapter.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/BaseApiClientAdapter.java @@ -198,7 +198,7 @@ protected String getApiResponseMessages(final ApiResponse apiResponse) return responseMessages; } - protected ApiResponse getApiResponse(final String response, final TypeToken> responseType) + protected ApiResponse parseApiResponse(final String response, final TypeToken> responseType) { return JsonReader.parseApiResponse(response, responseType); } diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/FileApiClientAdapterImpl.java b/api-sdk/src/main/java/com/smartling/api/sdk/FileApiClientAdapterImpl.java index 4452197..594ebe2 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/FileApiClientAdapterImpl.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/FileApiClientAdapterImpl.java @@ -18,6 +18,7 @@ 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.dto.EmptyResponse; import com.smartling.api.sdk.dto.file.FileLastModified; import com.smartling.api.sdk.dto.file.FileList; @@ -149,8 +150,7 @@ public ApiResponse getFilesList(final FileListSearchParams fileListSea final String params = buildFileListParams(fileListSearchParams); final HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_LIST_API_URL, params)); - final StringResponse response = getStringResponse(getRequest); - final ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + final ApiResponse apiResponse = getResponse(getRequest, new TypeToken>() {}); logger.debug(String.format("Get files list: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; @@ -159,23 +159,17 @@ public ApiResponse getFilesList(final FileListSearchParams fileListSea @Override public ApiResponse getFileStatus(final String fileUri, final String locale) throws ApiException { - logger.debug(String.format("Get file satatus: fileUri = %s, projectId = %s, apiKey = %s, locale = %s", fileUri, this.projectId, maskApiKey(this.apiKey), locale)); + logger.debug(String.format("Get file status: fileUri = %s, projectId = %s, apiKey = %s, locale = %s", fileUri, this.projectId, maskApiKey(this.apiKey), locale)); final String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(LOCALE, locale)); final HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_STATUS_API_URL, params)); - final ApiResponse apiResponse = getApiResponse(getRequest); + final ApiResponse apiResponse = getResponse(getRequest, new TypeToken>() {}); logger.debug(String.format("Get file status: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; } - private ApiResponse getApiResponse(final HttpGet getRequest) throws ApiException - { - final StringResponse response = getStringResponse(getRequest); - return getApiResponse(response.getContents(), new TypeToken>() {}); - } - @Override public ApiResponse uploadFile(final File fileToUpload, final String charsetName, final FileUploadParameterBuilder fileUploadParameterBuilder) throws ApiException @@ -201,8 +195,7 @@ public ApiResponse deleteFile(final String fileUri) throws ApiExc final String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri)); final HttpDelete httpDeleteFileRequest = new HttpDelete(buildUrl(DELETE_FILE_URL, params)); - final StringResponse response = getStringResponse(httpDeleteFileRequest); - final ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + final ApiResponse apiResponse = getResponse(httpDeleteFileRequest, new TypeToken>() {}); logger.debug(String.format("Delete file: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; @@ -217,18 +210,12 @@ public ApiResponse renameFile(final String fileUri, final String final String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(NEW_FILE_URI, newFileUri)); final HttpPost httpPostRequest = new HttpPost(buildUrl(RENAME_FILE_URL, params)); - final StringResponse response = getStringResponse(httpPostRequest); - final ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + final ApiResponse apiResponse = getResponse(httpPostRequest, new TypeToken>() {}); logger.debug(String.format("Rename file: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; } - private StringResponse getStringResponse(final HttpRequestBase httpRequest) throws ApiException - { - return getHttpUtils().executeHttpCall(httpRequest, proxyConfiguration); - } - @Override public ApiResponse getLastModified(final String fileUri, final Date lastModifiedAfter, final String locale) throws ApiException { @@ -242,41 +229,50 @@ public ApiResponse getLastModified(final String fileUri, final ); final HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_LAST_MODIFIED, params)); - final StringResponse response = getStringResponse(getRequest); - final ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + final ApiResponse apiResponse = getResponse(getRequest, new TypeToken>() {}); logger.debug(String.format("Get last modified: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; } - private HttpPost createFileUploadHttpPostRequest(final String apiParameters, final ContentBody contentBody) - { - final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create() - .addPart(FileApiParams.FILE, contentBody); - - final HttpPost httpPost = new HttpPost(String.format(UPLOAD_FILE_API_URL, baseApiUrl) + apiParameters); - httpPost.setEntity(multipartEntityBuilder.build()); - - return httpPost; - } - private ApiResponse uploadFile(final FileUploadParameterBuilder fileUploadParameterBuilder, final ContentBody contentBody) throws ApiException { logger.debug(String.format("Upload file: fileUri = %s, projectId = %s, apiKey = %s, localesToApprove = %s", - fileUploadParameterBuilder.getFileUri(), this.projectId, maskApiKey(this.apiKey), StringUtils.join(fileUploadParameterBuilder.getLocalesToApprove(), ", "))); + fileUploadParameterBuilder.getFileUri(), this.projectId, maskApiKey(this.apiKey), StringUtils.join(fileUploadParameterBuilder.getLocalesToApprove(), ", "))); final List paramsList = fileUploadParameterBuilder.getNameValueList(); final String params = buildParamsQuery(paramsList.toArray(new NameValuePair[paramsList.size()])); final HttpPost httpPostFile = createFileUploadHttpPostRequest(params, contentBody); - final StringResponse response = getStringResponse(httpPostFile); - final ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + final ApiResponse apiResponse = getResponse(httpPostFile, new TypeToken>() {}); logger.debug(String.format("Upload file: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse; } + private ApiResponse getResponse(final HttpRequestBase httpRequest, final TypeToken> typeToken) throws ApiException + { + final StringResponse response = getStringResponse(httpRequest); + return parseApiResponse(response.getContents(), typeToken); + } + + private StringResponse getStringResponse(final HttpRequestBase httpRequest) throws ApiException + { + return getHttpUtils().executeHttpCall(httpRequest, proxyConfiguration); + } + + private HttpPost createFileUploadHttpPostRequest(final String apiParameters, final ContentBody contentBody) + { + final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create() + .addPart(FileApiParams.FILE, contentBody); + + final HttpPost httpPost = new HttpPost(String.format(UPLOAD_FILE_API_URL, baseApiUrl) + apiParameters); + httpPost.setEntity(multipartEntityBuilder.build()); + + return httpPost; + } + private String buildFileListParams(final FileListSearchParams fileListSearchParams) { final List nameValuePairs = new ArrayList<>(); diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/ProjectApiClientAdapterImpl.java b/api-sdk/src/main/java/com/smartling/api/sdk/ProjectApiClientAdapterImpl.java index 15fcc23..8f51308 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/ProjectApiClientAdapterImpl.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/ProjectApiClientAdapterImpl.java @@ -76,7 +76,7 @@ this.projectId, maskApiKey(this.apiKey) HttpGet getRequest = new HttpGet(buildUrl(GET_PROJECT_LOCALES_API_URL, params)); StringResponse response = getHttpUtils().executeHttpCall(getRequest, proxyConfiguration); - ApiResponse apiResponse = getApiResponse(response.getContents(), new TypeToken>() {}); + ApiResponse apiResponse = parseApiResponse(response.getContents(), new TypeToken>() {}); logger.debug(String.format("Get last modified: %s. %s", apiResponse.getCode(), getApiResponseMessages(apiResponse))); return apiResponse;