/
PluginRequestHelper.java
44 lines (38 loc) · 2.5 KB
/
PluginRequestHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.thoughtworks.go.plugin.access;
import com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import com.thoughtworks.go.plugin.infra.PluginManager;
import java.util.List;
import static java.lang.String.format;
public class PluginRequestHelper {
protected PluginManager pluginManager;
private List<String> goSupportedVersions;
private String extensionName;
public PluginRequestHelper(PluginManager pluginManager, List<String> goSupportedVersions, String extensionName) {
this.pluginManager = pluginManager;
this.goSupportedVersions = goSupportedVersions;
this.extensionName = extensionName;
}
public <T> T submitRequest(String pluginId, String requestName, PluginInteractionCallback<T> pluginInteractionCallback) {
if (!pluginManager.isPluginOfType(extensionName, pluginId)) {
throw new RuntimeException(format("Did not find '%s' plugin with id '%s'. Looks like plugin is missing", extensionName, pluginId));
}
try {
String resolvedExtensionVersion = pluginManager.resolveExtensionVersion(pluginId, goSupportedVersions);
DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(extensionName, resolvedExtensionVersion, requestName);
apiRequest.setRequestBody(pluginInteractionCallback.requestBody(resolvedExtensionVersion));
apiRequest.setRequestParams(pluginInteractionCallback.requestParams(resolvedExtensionVersion));
GoPluginApiResponse response = pluginManager.submitTo(pluginId, apiRequest);
if(response == null){
throw new RuntimeException("The plugin sent a null response");
}
if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == response.responseCode()) {
return pluginInteractionCallback.onSuccess(response.responseBody(), resolvedExtensionVersion);
}
throw new RuntimeException(format("The plugin sent a response that could not be understood by Go. Plugin returned with code '%s' and the following response: '%s'", response.responseCode(), response.responseBody()));
} catch (Exception e) {
throw new RuntimeException(format("Interaction with plugin with id '%s' implementing '%s' extension failed while requesting for '%s'. Reason: [%s]", pluginId, extensionName, requestName, e.getMessage()), e);
}
}
}