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 19, 2024
1 parent c44ef6d commit e36219a
Show file tree
Hide file tree
Showing 110 changed files with 3,088 additions and 7,927 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,29 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.credentialprovider.CredentialProvider;
import com.squareup.okhttp.OkHttpClient;
import jakarta.annotation.PostConstruct;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AuthenticatedApiClient extends ApiClient {

private CookieManager cookieManager;
@Autowired CredentialProvider credentialProvider;

@Autowired ConsoleWriter consoleWriter;

@PostConstruct
public void init() {
this.cookieManager = this.getCookieManager();
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()));
.add(new AuthenticatedApiInterceptor(this.getBasePath(), this.credentialProvider, this.consoleWriter));
return httpClient;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.credentialprovider.CredentialProvider;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
Expand All @@ -11,6 +13,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,9 +40,22 @@ public class AuthenticatedApiInterceptor implements Interceptor {

private final String basePath;

public AuthenticatedApiInterceptor(CookieManager cookieManager, String basePath) {
this.cookieManager = cookieManager;
private final CredentialProvider credentialProvider;

private ConsoleWriter consoleWriter;

public AuthenticatedApiInterceptor(String basePath, CredentialProvider credentialProvider, ConsoleWriter consoleWriter) {
this.cookieManager = getCookieManager();
this.basePath = basePath;
this.credentialProvider = credentialProvider;
this.consoleWriter = consoleWriter;
}

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

private Optional<HttpCookie> getCookie() {
Expand Down Expand Up @@ -71,8 +87,12 @@ private void updateCsrfToken() throws IOException {
httpClient.setCookieHandler(CookieHandler.getDefault());
Request request = new Request.Builder().url(url).build();
this.latestCsrfToken = this.getCsrfToken(httpClient, request);
consoleWriter.newLine().a(credentialProvider.getUsername() + " : " + credentialProvider.getPassword()).println();
RequestBody requestBody =
new FormEncodingBuilder().add("username", "admin").add("password", "ChangeMe").build();
new FormEncodingBuilder()
.add("username", credentialProvider.getUsername())
.add("password", credentialProvider.getPassword())
.build();
request =
new Request.Builder()
.url(url)
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);
}
}
Loading

0 comments on commit e36219a

Please sign in to comment.