private void cleanupMethods (Resource resource, Map<ActionType, Action> actions) { final HashSet<String> methods = new HashSet<>(); for (ActionType action : actions.keySet()) { methods.add(action.toString()); } for (Method m : resource.getResourceMethods().values()) { String httpMethod = m.getHttpMethod().toUpperCase(); if (!methods.contains(httpMethod)) { LOG.info(format("Removing deleted method %s for resource %s", httpMethod, resource.getId())); m.deleteMethod(); } } }
private void createIntegration(Resource resource, Method method, JSONObject config) { if (config == null) { return; } try { final JSONObject integ = config.getJSONObject(resource.getPath()) .getJSONObject(method.getHttpMethod().toLowerCase()) .getJSONObject("integration"); IntegrationType type = IntegrationType.valueOf(integ.getString("type").toUpperCase()); LOG.info("Creating integration with type " + type); PutIntegrationInput input = new PutIntegrationInput() .withType(type) .withUri(integ.getString("uri")) .withCredentials(integ.optString("credentials")) .withHttpMethod(integ.optString("httpMethod")) .withRequestParameters(jsonObjectToHashMapString(integ.optJSONObject("requestParameters"))) .withRequestTemplates(jsonObjectToHashMapString(integ.optJSONObject("requestTemplates"))) .withCacheNamespace(integ.optString("cacheNamespace")) .withCacheKeyParameters(jsonObjectToListString(integ.optJSONArray("cacheKeyParameters"))); Integration integration = method.putIntegration(input); createIntegrationResponses(integration, integ.optJSONObject("responses")); } catch (JSONException e) { LOG.info(format("Skipping integration for method %s of %s: %s", method.getHttpMethod(), resource.getPath(), e)); } }
private void cleanupMethodModels(Method method, Map<String, MimeType> body) { if (method.getRequestModels() != null) { for (Map.Entry<String, String> entry : method.getRequestModels().entrySet()) { if (!body.containsKey(entry.getKey()) || body.get(entry.getKey()).getSchema() == null) { LOG.info(format("Removing model %s from method %s", entry.getKey(), method.getHttpMethod())); method.updateMethod(createPatchDocument(createRemoveOperation("/requestModels/" + escapeOperationString(entry.getKey())))); } } } }
private void cleanupMethod(Method method, String type, Set<String> parameterSet) { if (method.getRequestParameters() != null) { method.getRequestParameters().keySet().forEach(key -> { final String[] parts = key.split("\\."); final String paramType = parts[2]; final String paramName = parts[3]; if (paramType.equals(type) && !parameterSet.contains(paramName)) { method.updateMethod(createPatchDocument(createRemoveOperation("/requestParameters/" + key))); } }); } }
private void createMethodResponses(RestApi api, Method method, Map<String, Response> responses, boolean update) { for (Map.Entry<String, Response> entry : responses.entrySet()) { createMethodResponse(api, method, entry.getKey(), entry.getValue(), update); } if (update) { cleanupMethodResponses(method, responses); } }
private void cleanupMethodResponses(Method method, Map<String, Response> responses) { method.getMethodResponses().entrySet().forEach(entry -> { if (!responses.containsKey(entry.getKey())) { entry.getValue().deleteMethodResponse(); } }); }
private void updateMethod(RestApi api, Method method, String type, String name, boolean required) { String expression = getExpression("method", "request", type, name); Map<String, Boolean> requestParameters = method.getRequestParameters(); Boolean requestParameter = requestParameters == null ? null : requestParameters.get(expression); if (requestParameter != null && requestParameter.equals(required)) { return; } LOG.info(format("Creating method parameter for api %s and method %s with name %s", api.getId(), method.getHttpMethod(), expression)); method.updateMethod(createPatchDocument(createAddOperation("/requestParameters/" + expression, getStringValue(required)))); }
private static String getBasePath(RestApi restApi) { String[] basePath = null; for (Resources resources = restApi.getResources(); resources != null; resources = safeGetNext(resources)) { for (Resource resource : resources.getItem()) { Map<String, Method> resourceMethods = resource.getResourceMethods(); if (resourceMethods.isEmpty()) { continue; } String[] resourcePath = resource.getPath().split("/"); if (basePath == null) { basePath = resourcePath; } else { int i=0; for (; i<basePath.length && i<resourcePath.length; i++) { if (!basePath[i].equals(resourcePath[i])) { break; } } basePath = Arrays.copyOf(basePath, i); } } } StringBuilder sb = new StringBuilder(); for (String s : basePath) { if (!s.isEmpty()) { sb.append("/"); sb.append(s); } } return sb.toString(); }
@Before public void setUp() throws Exception { BasicConfigurator.configure(); Injector injector = Guice.createInjector(new SwaggerApiImporterTestModule()); client = injector.getInstance(ApiGateway.class); importer = injector.getInstance(SwaggerApiFileImporter.class); RestApis mockRestApis = mock(RestApis.class); Integration mockIntegration = Mockito.mock(Integration.class); Method mockMethod = Mockito.mock(Method.class); when(mockMethod.getHttpMethod()).thenReturn("GET"); when(mockMethod.putIntegration(any())).thenReturn(mockIntegration); mockChildResource = Mockito.mock(Resource.class); when(mockChildResource.getPath()).thenReturn("/child"); when(mockChildResource.putMethod(any(), any())).thenReturn(mockMethod); mockResource = Mockito.mock(Resource.class); when(mockResource.getPath()).thenReturn("/"); when(mockResource.createResource(any())).thenReturn(mockChildResource); when(mockResource.putMethod(any(), any())).thenReturn(mockMethod); Resources mockResources = mock(Resources.class); when(mockResources.getItem()).thenReturn(Arrays.asList(mockResource)); Model mockModel = Mockito.mock(Model.class); when(mockModel.getName()).thenReturn("test model"); Models mockModels = mock(Models.class); when(mockModels.getItem()).thenReturn(Arrays.asList(mockModel)); mockRestApi = mock(RestApi.class); when(mockRestApi.getResources()).thenReturn(mockResources); when(mockRestApi.getModels()).thenReturn(mockModels); when(mockRestApi.getResourceById(any())).thenReturn(mockResource); when(client.getRestApis()).thenReturn(mockRestApis); when(client.createRestApi(any())).thenReturn(mockRestApi); importer.importApi(getResourcePath(API_GATEWAY)); }