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 10, 2024
1 parent 5282d54 commit 69cdca6
Show file tree
Hide file tree
Showing 36 changed files with 429 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.box.l10n.mojito.cli.command.CommandException;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class AssetWsApiHelper {
@Autowired AssetWsApi assetClient;
public class AssetClient extends AssetWsApi {

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

public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long repositoryId)
throws CommandException {
Expand All @@ -18,7 +18,7 @@ public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long reposit

List<AssetAssetSummary> assets;
try {
assets = this.assetClient.getAssets(repositoryId, path, null, null, null);
assets = this.getAssets(repositoryId, path, null, null, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@

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();
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
Expand Up @@ -7,12 +7,12 @@
import java.time.OffsetDateTime;
import java.util.List;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RepositoryWsApiHelper {
@Autowired RepositoryWsApi repositoryClient;
public class RepositoryClient extends RepositoryWsApi {

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

public List<BranchBranchSummary> getBranchesOfRepository(
Long repositoryId,
Expand All @@ -26,7 +26,7 @@ public List<BranchBranchSummary> getBranchesOfRepository(
List<BranchBranchSummary> branches;
try {
branches =
this.repositoryClient.getBranchesOfRepository(
this.getBranchesOfRepository(
repositoryId, branchName, deleted, translated, createdBefore);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
Expand All @@ -51,7 +51,7 @@ public List<BranchBranchSummary> getBranchesOfRepository(
public RepositoryRepository findRepositoryByName(String repositoryName) throws CommandException {
try {
Preconditions.checkNotNull(repositoryName, "Repository name can't be null");
List<RepositoryRepository> repositories = repositoryClient.getRepositories(repositoryName);
List<RepositoryRepository> repositories = this.getRepositories(repositoryName);
if (repositories.size() != 1) {
throw new CommandException("Repository with name [" + repositoryName + "] is not found");
}
Expand Down

This file was deleted.

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

import static java.util.Optional.ofNullable;

import com.box.l10n.mojito.cli.model.Branch;
import com.box.l10n.mojito.cli.model.BranchGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.GitBlame;
import com.box.l10n.mojito.cli.model.GitBlameGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.GitBlameWithUsage;
import com.box.l10n.mojito.cli.model.GitBlameWithUsageGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.Repository;
import com.box.l10n.mojito.cli.model.RepositoryGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.Screenshot;
import com.box.l10n.mojito.cli.model.ScreenshotGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.ScreenshotTextUnit;
import com.box.l10n.mojito.cli.model.ScreenshotTextUnitGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.TMTextUnit;
import com.box.l10n.mojito.cli.model.TMTextUnitGitBlameWithUsage;
import com.box.l10n.mojito.cli.model.User;
import com.box.l10n.mojito.cli.model.UserGitBlameWithUsage;

public class GitBlameWithUsageMapper {
public static GitBlame mapToGitBlame(GitBlameGitBlameWithUsage gitBlameWithUsage) {
GitBlame gitBlame = new GitBlame();
gitBlame.setId(gitBlameWithUsage.getId());
gitBlame.setCreatedDate(gitBlameWithUsage.getCreatedDate());
gitBlame.setAuthorEmail(gitBlameWithUsage.getAuthorEmail());
gitBlame.setAuthorName(gitBlameWithUsage.getAuthorName());
gitBlame.setCommitTime(gitBlameWithUsage.getCommitTime());
gitBlame.setCommitName(gitBlameWithUsage.getCommitName());
return gitBlame;
}

public static Repository mapToRepository(
RepositoryGitBlameWithUsage repositoryGitBlameWithUsage) {
Repository repository = new Repository();
repository.setId(repositoryGitBlameWithUsage.getId());
repository.setCreatedDate(repositoryGitBlameWithUsage.getCreatedDate());
repository.setName(repositoryGitBlameWithUsage.getName());
return repository;
}

public static User mapToUser(UserGitBlameWithUsage userGitBlameWithUsage) {
User user = new User();
user.setId(userGitBlameWithUsage.getId());
user.setCreatedDate(userGitBlameWithUsage.getCreatedDate());
user.setUsername(userGitBlameWithUsage.getUsername());
user.setCommonName(userGitBlameWithUsage.getCommonName());
return user;
}

public static TMTextUnit mapToTmTextUnit(
TMTextUnitGitBlameWithUsage tmTextUnitGitBlameWithUsage) {
TMTextUnit tmTextUnit = new TMTextUnit();
tmTextUnit.setId(tmTextUnitGitBlameWithUsage.getId());
tmTextUnit.setCreatedDate(tmTextUnitGitBlameWithUsage.getCreatedDate());
tmTextUnit.setName(tmTextUnitGitBlameWithUsage.getName());
return tmTextUnit;
}

public static ScreenshotTextUnit mapToScreenshotTextUnit(
ScreenshotTextUnitGitBlameWithUsage screenshotTextUnitGitBlameWithUsage) {
ScreenshotTextUnit screenshotTextUnit = new ScreenshotTextUnit();
screenshotTextUnit.setId(screenshotTextUnitGitBlameWithUsage.getId());
screenshotTextUnit.setTmTextUnit(
ofNullable(screenshotTextUnitGitBlameWithUsage.getTmTextUnit())
.map(GitBlameWithUsageMapper::mapToTmTextUnit)
.orElse(null));
return screenshotTextUnit;
}

public static Screenshot mapToScreenshot(
ScreenshotGitBlameWithUsage screenshotGitBlameWithUsage) {
Screenshot screenshot = new Screenshot();
screenshot.setId(screenshotGitBlameWithUsage.getId());
screenshot.setCreatedDate(screenshotGitBlameWithUsage.getCreatedDate());
screenshot.setSrc(screenshotGitBlameWithUsage.getSrc());
screenshot.setTextUnits(
ofNullable(screenshotGitBlameWithUsage.getTextUnits())
.map(
textUnits ->
textUnits.stream()
.map(GitBlameWithUsageMapper::mapToScreenshotTextUnit)
.toList())
.orElse(null));
return screenshot;
}

public static Branch mapToBranch(BranchGitBlameWithUsage branchGitBlameWithUsage) {
Branch branch = new Branch();
branch.setId(branchGitBlameWithUsage.getId());
branch.setCreatedDate(branchGitBlameWithUsage.getCreatedDate());
branch.setRepository(
ofNullable(branchGitBlameWithUsage.getRepository())
.map(GitBlameWithUsageMapper::mapToRepository)
.orElse(null));
branch.setName(branchGitBlameWithUsage.getName());
branch.setCreatedByUser(
ofNullable(branchGitBlameWithUsage.getCreatedByUser())
.map(GitBlameWithUsageMapper::mapToUser)
.orElse(null));
branch.setDeleted(branchGitBlameWithUsage.isDeleted());
branch.setScreenshots(
ofNullable(branchGitBlameWithUsage.getScreenshots())
.map(
screenshots ->
screenshots.stream().map(GitBlameWithUsageMapper::mapToScreenshot).toList())
.orElse(null));
return branch;
}

public static GitBlameWithUsage mapToGitBlameWithUsage(
GitBlameWithUsageGitBlameWithUsage gitBlameWithUsage) {
GitBlameWithUsage newGitBlameWithUsage = new GitBlameWithUsage();
newGitBlameWithUsage.setUsages(gitBlameWithUsage.getUsages());
newGitBlameWithUsage.setTextUnitName(gitBlameWithUsage.getTextUnitName());
newGitBlameWithUsage.setPluralForm(gitBlameWithUsage.getPluralForm());
newGitBlameWithUsage.setTmTextUnitId(gitBlameWithUsage.getTmTextUnitId());
newGitBlameWithUsage.setAssetId(gitBlameWithUsage.getAssetId());
newGitBlameWithUsage.setAssetTextUnitId(gitBlameWithUsage.getAssetTextUnitId());
newGitBlameWithUsage.setThirdPartyTextUnitId(gitBlameWithUsage.getThirdPartyTextUnitId());
newGitBlameWithUsage.setContent(gitBlameWithUsage.getContent());
newGitBlameWithUsage.setComment(gitBlameWithUsage.getComment());
newGitBlameWithUsage.setGitBlame(
ofNullable(gitBlameWithUsage.getGitBlame())
.map(GitBlameWithUsageMapper::mapToGitBlame)
.orElse(null));
newGitBlameWithUsage.setBranch(
ofNullable(gitBlameWithUsage.getBranch())
.map(GitBlameWithUsageMapper::mapToBranch)
.orElse(null));
newGitBlameWithUsage.setScreenshots(
ofNullable(gitBlameWithUsage.getScreenshots())
.map(
screenshots ->
screenshots.stream().map(GitBlameWithUsageMapper::mapToScreenshot).toList())
.orElse(null));
newGitBlameWithUsage.setIntroducedBy(gitBlameWithUsage.getIntroducedBy());
newGitBlameWithUsage.setIsVirtual(gitBlameWithUsage.isIsVirtual());
return newGitBlameWithUsage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.AiPromptWsApi;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.model.AITranslationLocalePromptOverridesRequest;
import org.slf4j.Logger;
Expand All @@ -22,8 +23,6 @@ public class AIRepositoryLocaleOverrideCommand extends Command {

static Logger logger = LoggerFactory.getLogger(CreateAIPromptCommand.class);

@Autowired AiPromptWsApi aiServiceClient;

@Parameter(
names = {"--repository-name", "-r"},
required = true,
Expand Down Expand Up @@ -55,6 +54,8 @@ public class AIRepositoryLocaleOverrideCommand extends Command {
description = "Delete the AI prompt overrides for the given locales")
boolean isDelete = false;

@Autowired private ApiClient apiClient;

@Override
protected void execute() throws CommandException {
AITranslationLocalePromptOverridesRequest aiTranslationLocalePromptOverridesRequest =
Expand All @@ -64,6 +65,7 @@ protected void execute() throws CommandException {
StringUtils.commaDelimitedListToSet(locales).stream().toList());
aiTranslationLocalePromptOverridesRequest.setAiPromptId(aiPromptId);
aiTranslationLocalePromptOverridesRequest.setDisabled(disabled);
AiPromptWsApi aiServiceClient = new AiPromptWsApi(this.apiClient);
try {
if (isDelete) {
aiServiceClient.deleteRepositoryLocalePromptOverrides(
Expand Down
Loading

0 comments on commit 69cdca6

Please sign in to comment.