Skip to content

Commit

Permalink
I18N-1323 Update Mojito CLI to use OpenAPI spec for rest calls
Browse files Browse the repository at this point in the history
Refactor other commands
  • Loading branch information
DarKhaos committed Dec 18, 2024
1 parent c44ef6d commit 46b17d4
Show file tree
Hide file tree
Showing 105 changed files with 2,945 additions and 7,932 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ release.properties
/tmp/
/local/
.vscode/
/webapp/src/main/resources/openapi.*
11 changes: 5 additions & 6 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@
</dependency>

<dependency>
<groupId>com.box.l10n.mojito</groupId>
<artifactId>mojito-webapp</artifactId>
<version>0.111-SNAPSHOT</version>
<scope>test</scope>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>com.box.l10n.mojito</groupId>
<artifactId>mojito-common</artifactId>
<artifactId>mojito-webapp</artifactId>
<version>0.111-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.box.l10n.mojito</groupId>
<artifactId>mojito-restclient</artifactId>
<artifactId>mojito-common</artifactId>
<version>0.111-SNAPSHOT</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.model.AICheckRequest;
import com.box.l10n.mojito.cli.model.AICheckResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AiChecksWsApiProxy extends AiChecksWsApi {
/** logger */
static Logger logger = LoggerFactory.getLogger(AiChecksWsApiProxy.class);

public AiChecksWsApiProxy(ApiClient apiClient) {
super(apiClient);
}

@Override
public AICheckResponse executeAIChecks(AICheckRequest body) throws ApiException {
logger.debug("Received request to execute AI checks");
return super.executeAIChecks(body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.model.AIPromptContextMessageCreateRequest;
import com.box.l10n.mojito.cli.model.AIPromptCreateRequest;
import com.box.l10n.mojito.cli.model.AITranslationLocalePromptOverridesRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AiPromptWsApiProxy extends AiPromptWsApi {
/** logger */
static Logger logger = LoggerFactory.getLogger(AssetWsApiProxy.class);

public AiPromptWsApiProxy(ApiClient apiClient) {
super(apiClient);
}

@Override
public String deleteRepositoryLocalePromptOverrides(
AITranslationLocalePromptOverridesRequest body) throws ApiException {
logger.debug("Received request to delete repository locale prompt overrides");
return super.deleteRepositoryLocalePromptOverrides(body);
}

@Override
public String createOrUpdateRepositoryLocalePromptOverrides(
AITranslationLocalePromptOverridesRequest body) throws ApiException {
logger.debug("Received request to create or update repository locale prompt overrides");
return super.createOrUpdateRepositoryLocalePromptOverrides(body);
}

@Override
public void addPromptToRepository(Long promptId, String repositoryName, String promptType)
throws ApiException {
logger.debug("Received request to add prompt id {} to {} repository", promptId, repositoryName);
super.addPromptToRepository(promptId, repositoryName, promptType);
}

@Override
public Long createPrompt(AIPromptCreateRequest body) throws ApiException {
logger.debug("Received request to create prompt");
return super.createPrompt(body);
}

@Override
public Long createPromptMessage(AIPromptContextMessageCreateRequest body) throws ApiException {
logger.debug("Received request to create prompt context message");
return super.createPromptMessage(body);
}

@Override
public void deletePrompt(Long promptId) throws ApiException {
logger.debug("Received request to delete prompt id {}", promptId);
super.deletePrompt(promptId);
}

@Override
public void deletePromptMessage(Long contextMessageId) throws ApiException {
logger.debug("Received request to delete prompt message id {}", contextMessageId);
super.deletePromptMessage(contextMessageId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.command.CommandException;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import com.box.l10n.mojito.cli.model.ImportLocalizedAssetBody;
import com.box.l10n.mojito.cli.model.LocalizedAssetBody;
import com.box.l10n.mojito.cli.model.MultiLocalizedAssetBody;
import com.box.l10n.mojito.cli.model.PollableTask;
import com.box.l10n.mojito.cli.model.XliffExportBody;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class AssetWsApiProxy extends AssetWsApi {

/** logger */
static Logger logger = LoggerFactory.getLogger(AssetWsApiProxy.class);

public static final String OUTPUT_BCP47_TAG = "en-x-pseudo";

public AssetWsApiProxy(ApiClient apiClient) {
super(apiClient);
}

public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long repositoryId)
throws CommandException, ApiException {
Assert.notNull(path, "path must not be null");
Assert.notNull(repositoryId, "repository must not be null");

List<AssetAssetSummary> assets = this.getAssets(repositoryId, path, null, null, null);
if (!assets.isEmpty()) {
return assets.getFirst();
} else {
throw new CommandException(
"Could not find asset with path = [" + path + "] at repo id [" + repositoryId + "]");
}
}

@Override
public LocalizedAssetBody getLocalizedAssetForContent(
LocalizedAssetBody body, Long assetId, Long localeId) throws ApiException {
logger.debug(
"Getting localized asset with asset id = {}, locale id = {}, outputBcp47tag: {}",
assetId,
localeId,
body.getOutputBcp47tag());
return super.getLocalizedAssetForContent(body, assetId, localeId);
}

@Override
public ImportLocalizedAssetBody importLocalizedAsset(
ImportLocalizedAssetBody body, Long assetId, Long localeId) throws ApiException {
logger.debug("Import localized asset with asset id = {}, locale id = {}", assetId, localeId);
return super.importLocalizedAsset(body, assetId, localeId);
}

@Override
public PollableTask getLocalizedAssetForContentParallel(
MultiLocalizedAssetBody body, Long assetId) throws ApiException {
logger.debug("Getting localized assets with asset id = {}", assetId);
return super.getLocalizedAssetForContentParallel(body, assetId);
}

@Override
public PollableTask getLocalizedAssetForContentAsync(LocalizedAssetBody body, Long assetId)
throws ApiException {
logger.debug(
"Getting localized asset with asset id = {}, locale id = {}, outputBcp47tag: {}",
assetId,
body.getLocaleId(),
body.getOutputBcp47tag());
return super.getLocalizedAssetForContentAsync(body, assetId);
}

@Override
public List<Long> getAssetIds(Long repositoryId, Boolean deleted, Boolean virtual, Long branchId)
throws ApiException {
Assert.notNull(repositoryId, "The repositoryId must not be null");
return super.getAssetIds(repositoryId, deleted, virtual, branchId);
}

@Override
public PollableTask deleteAssetsOfBranches(List<Long> body, Long branchId) throws ApiException {
logger.debug("Deleting assets by asset ids = {} or branch id: {}", body.toString(), branchId);
return super.deleteAssetsOfBranches(body, branchId);
}

@Override
public List<AssetAssetSummary> getAssets(
Long repositoryId, String path, Boolean deleted, Boolean virtual, Long branchId)
throws ApiException {
logger.debug("Get assets by path = {} repo id = {} deleted = {}", path, repositoryId, deleted);
return super.getAssets(repositoryId, path, deleted, virtual, branchId);
}

@Override
public XliffExportBody xliffExportAsync(XliffExportBody body, String bcp47tag, Long assetId)
throws ApiException {
logger.debug("Export asset id: {} for locale: {}", assetId, bcp47tag);
return super.xliffExportAsync(body, bcp47tag, assetId);
}

@Override
public XliffExportBody xliffExport(Long assetId, Long tmXliffId) throws ApiException {
logger.debug("Get exported xliff for asset id: {} for tm xliff id: {}", assetId, tmXliffId);
return super.xliffExport(assetId, tmXliffId);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
package com.box.l10n.mojito.cli.apiclient;

import com.squareup.okhttp.OkHttpClient;
import jakarta.annotation.PostConstruct;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import org.springframework.stereotype.Component;

@Component
public class AuthenticatedApiClient extends ApiClient {

private CookieManager cookieManager;

@PostConstruct
public void init() {
this.cookieManager = this.getCookieManager();
public AuthenticatedApiClient() {
this.setHttpClient(this.getOkHttpClient());
}

private CookieManager getCookieManager() {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
return cookieManager;
}

private OkHttpClient getOkHttpClient() {
OkHttpClient httpClient = new OkHttpClient();
httpClient
.interceptors()
.add(new AuthenticatedApiInterceptor(this.cookieManager, this.getBasePath()));
httpClient.interceptors().add(new AuthenticatedApiInterceptor(this.getBasePath()));
return httpClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.util.Optional;
Expand All @@ -37,11 +38,18 @@ public class AuthenticatedApiInterceptor implements Interceptor {

private final String basePath;

public AuthenticatedApiInterceptor(CookieManager cookieManager, String basePath) {
this.cookieManager = cookieManager;
public AuthenticatedApiInterceptor(String basePath) {
this.cookieManager = getCookieManager();
this.basePath = basePath;
}

private CookieManager getCookieManager() {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
return cookieManager;
}

private Optional<HttpCookie> getCookie() {
CookieStore cookieStore = this.cookieManager.getCookieStore();
return cookieStore.getCookies().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.box.l10n.mojito.cli.apiclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageWsApiProxy extends ImageWsApi {
/** logger */
static Logger logger = LoggerFactory.getLogger(ImageWsApiProxy.class);

public ImageWsApiProxy(ApiClient apiClient) {
super(apiClient);
}

@Override
public void uploadImage(byte[] body, String imageName) throws ApiException {
logger.debug("Upload image with name = {}", imageName);
super.uploadImage(body, imageName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.command.CommandException;
import com.box.l10n.mojito.cli.model.Locale;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LocaleWsApiProxy extends LocaleWsApi {
/** logger */
static Logger logger = LoggerFactory.getLogger(LocaleWsApiProxy.class);

public LocaleWsApiProxy(ApiClient apiClient) {
super(apiClient);
}

public Locale getLocaleByBcp47Tag(String bcp47Tag) throws ApiException {
logger.debug("Getting locale for BCP47 tag: {}", bcp47Tag);

List<Locale> locales = this.getLocales(bcp47Tag);

if (locales.size() != 1) {
throw new CommandException("Could not find locale with BCP47 tag: " + bcp47Tag);
}

return locales.getFirst();
}

@Override
public List<Locale> getLocales(String bcp47Tag) throws ApiException {
logger.debug("Getting all locales");
return super.getLocales(bcp47Tag);
}
}
Loading

0 comments on commit 46b17d4

Please sign in to comment.