forked from box/mojito
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I18N-1323 Update Mojito CLI to use OpenAPI spec for rest calls
Refactor other commands
- Loading branch information
Showing
93 changed files
with
2,460 additions
and
7,841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,4 @@ release.properties | |
/tmp/ | ||
/local/ | ||
.vscode/ | ||
/webapp/src/main/resources/openapi.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 2 additions & 19 deletions
21
cli/src/main/java/com/box/l10n/mojito/cli/apiclient/AuthenticatedApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
cli/src/main/java/com/box/l10n/mojito/cli/apiclient/LocaleWsApiProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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(); | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
cli/src/main/java/com/box/l10n/mojito/cli/apiclient/PollableTaskWsApiProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.box.l10n.mojito.cli.apiclient; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import com.box.l10n.mojito.cli.apiclient.exceptions.PollableTaskException; | ||
import com.box.l10n.mojito.cli.apiclient.exceptions.PollableTaskExecutionException; | ||
import com.box.l10n.mojito.cli.apiclient.exceptions.PollableTaskTimeoutException; | ||
import com.box.l10n.mojito.cli.command.CommandException; | ||
import com.box.l10n.mojito.cli.model.PollableTask; | ||
import com.box.l10n.mojito.cli.models.ErrorMessage; | ||
import com.box.l10n.mojito.json.ObjectMapper; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PollableTaskWsApiProxy extends PollableTaskWsApi { | ||
/** logger */ | ||
static Logger logger = getLogger(PollableTaskWsApiProxy.class); | ||
|
||
public static final Long NO_TIMEOUT = -1L; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public PollableTaskWsApiProxy(ApiClient apiClient, ObjectMapper objectMapper) { | ||
super(apiClient); | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
/** | ||
* Waits for {@link PollableTask} to be all finished (see {@link PollableTask#isAllFinished() }). | ||
* Infinite timeout. | ||
* | ||
* @param pollableId the {@link PollableTask#getId()} | ||
* @throws PollableTaskException | ||
*/ | ||
public void waitForPollableTask(Long pollableId) throws PollableTaskException { | ||
waitForPollableTask(pollableId, NO_TIMEOUT); | ||
} | ||
|
||
public void waitForPollableTask(Long pollableTaskId, long timeout) throws PollableTaskException { | ||
waitForPollableTask(pollableTaskId, timeout, null); | ||
} | ||
|
||
/** | ||
* Waits for {@link PollableTask} to be all finished (see {@link PollableTask#isAllFinished() }). | ||
* | ||
* @param pollableId the {@link com.box.l10n.mojito.cli.model.PollableTask#getId()} | ||
* @param timeout timeout in milliseconds. | ||
* @param waitForPollableTaskListener listener to be called during polling | ||
* @throws PollableTaskException | ||
*/ | ||
public void waitForPollableTask( | ||
Long pollableId, long timeout, WaitForPollableTaskListener waitForPollableTaskListener) | ||
throws PollableTaskException { | ||
|
||
long timeoutTime = System.currentTimeMillis() + timeout; | ||
long waitTime = 0; | ||
|
||
PollableTask pollableTask = null; | ||
|
||
while (pollableTask == null || !pollableTask.isAllFinished()) { | ||
|
||
logger.debug("Waiting for PollableTask: {} to finish", pollableId); | ||
|
||
try { | ||
pollableTask = this.getPollableTaskById(pollableId); | ||
} catch (ApiException e) { | ||
throw new CommandException(e.getMessage(), e); | ||
} | ||
|
||
if (waitForPollableTaskListener != null) { | ||
waitForPollableTaskListener.afterPoll(pollableTask); | ||
} | ||
|
||
List<PollableTask> pollableTaskWithErrors = getAllPollableTasksWithError(pollableTask); | ||
|
||
if (!pollableTaskWithErrors.isEmpty()) { | ||
|
||
for (PollableTask pollableTaskWithError : pollableTaskWithErrors) { | ||
ErrorMessage errorMessage = | ||
this.objectMapper.convertValue( | ||
pollableTaskWithError.getErrorMessage(), ErrorMessage.class); | ||
logger.debug( | ||
"Error happened in PollableTask {}: {}", | ||
pollableTaskWithError.getId(), | ||
errorMessage.getMessage()); | ||
} | ||
|
||
// Last task is the root task if it has an error or any of the sub task | ||
// TODO(P1) we might want to show all errors | ||
PollableTask lastTaskInError = | ||
pollableTaskWithErrors.get(pollableTaskWithErrors.size() - 1); | ||
|
||
ErrorMessage errorMessage = | ||
this.objectMapper.convertValue(lastTaskInError.getErrorMessage(), ErrorMessage.class); | ||
throw new PollableTaskExecutionException(errorMessage.getMessage()); | ||
} | ||
|
||
if (!pollableTask.isAllFinished()) { | ||
|
||
if (timeout != NO_TIMEOUT && System.currentTimeMillis() > timeoutTime) { | ||
logger.debug( | ||
"Timed out waiting for PollableTask: {} to finish. \n{}", | ||
pollableId, | ||
ReflectionToStringBuilder.toString(pollableTask)); | ||
throw new PollableTaskTimeoutException( | ||
"Timed out waiting for PollableTask: " + pollableId); | ||
} | ||
|
||
try { | ||
Thread.sleep(waitTime); | ||
waitTime = getNextWaitTime(waitTime); | ||
} catch (InterruptedException ie) { | ||
throw new RuntimeException(ie); | ||
} | ||
} else { | ||
logger.debug("PollableTask: {} finished", pollableId); | ||
} | ||
} | ||
} | ||
|
||
long getNextWaitTime(long lastWaitTime) { | ||
int maxTime = 500; | ||
long nextWaitTime = lastWaitTime + 25; | ||
nextWaitTime = Math.max(maxTime, nextWaitTime); | ||
return nextWaitTime; | ||
} | ||
|
||
/** | ||
* Get all the PollableTasks with error (traverses all the PollableTask's subtasks) | ||
* | ||
* @param pollableTask | ||
* @return | ||
*/ | ||
public List<PollableTask> getAllPollableTasksWithError(PollableTask pollableTask) { | ||
List<PollableTask> result = new ArrayList<>(); | ||
recursivelyGetAllPollableTaskWithError(pollableTask, result); | ||
return result; | ||
} | ||
|
||
/** | ||
* Recursively traverses all subtasks of {@code pollableTask} and return all the {@link | ||
* PollableTask} which had an error message | ||
* | ||
* @param pollableTask | ||
* @param pollableTasksWithError | ||
*/ | ||
private void recursivelyGetAllPollableTaskWithError( | ||
PollableTask pollableTask, List<PollableTask> pollableTasksWithError) { | ||
|
||
for (PollableTask subTask : pollableTask.getSubTasks()) { | ||
recursivelyGetAllPollableTaskWithError(subTask, pollableTasksWithError); | ||
} | ||
|
||
if (pollableTask.getErrorMessage() != null) { | ||
pollableTasksWithError.add(pollableTask); | ||
} | ||
} | ||
} |
63 changes: 0 additions & 63 deletions
63
cli/src/main/java/com/box/l10n/mojito/cli/apiclient/RepositoryWsApiHelper.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.