String addCategory(String categoryUrl, String name) throws IOException { log.info(format("Adding category %s to %s", name, categoryUrl)); Post request = new Post(rootUrl + CATEGORY_URI, credentials); request.setAccept(APPLICATION_JSON); request.addString("parent", categoryUrl); request.addString("name", name); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to add category " + name, categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to add category " + name, categoryUrl); if (request.isBadRequest()) throw new NotFoundException("Category not found", categoryUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Category " + name + " already exists", categoryUrl); if (!request.isSuccessful()) throw new IOException("POST on " + (rootUrl + CATEGORY_URI) + " with payload " + name + " not successful: " + result); return request.getLocation(); }
public String addUser(String userName, String password, String firstName, String lastName, String email) throws IOException { log.info("Adding user " + userName + "," + firstName + "," + lastName + "," + email); Post request = new Post(apiUrl + USER_URI); request.setAccept(APPLICATION_JSON); request.addString("username", userName); request.addString("password", password); request.addString("first_name", firstName); request.addString("last_name", lastName); request.addString("email", email); String result = request.executeAsString(); if (request.isBadRequest()) throw new ForbiddenException("Cannot add user: " + result, apiUrl + USER_URI); if (request.isForbidden()) throw new ForbiddenException("Cannot add user: " + result, apiUrl + USER_URI); if (!request.isSuccessful()) throw new IOException("POST on " + (apiUrl + USER_URI) + " with payload " + userName + "," + firstName + "," + lastName + "," + email + " not successful: " + result); return request.getLocation(); }
String addRoute(String categoryUrl, String description, String localFile, String remoteUrl) throws IOException { log.info(format("Adding route %s to category %s with remote url %s", description, categoryUrl, remoteUrl)); Post request = new Post(rootUrl + ROUTE_URI, credentials); request.setAccept(APPLICATION_JSON); request.addString("category", categoryUrl); request.addString("description", description); if (localFile != null) request.addString("localFile", localFile); if (remoteUrl != null) request.addString("remoteUrl", remoteUrl); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to add route " + description, categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to add route " + description, categoryUrl); if (request.isBadRequest()) throw new NotFoundException("Category not found", categoryUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Route " + description + " already exists", categoryUrl); if (!request.isSuccessful()) throw new IOException("POST on " + (rootUrl + ROUTE_URI) + " with route " + description + "," + categoryUrl + "," + remoteUrl + " not successful: " + result); return request.getLocation(); }