au.org.consumerdatastandards.client.cli.ApiCliBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client-cli Show documentation
Show all versions of client-cli Show documentation
CDS client side command line tool
/*
* Consumer Data Standards
* Sample CLI tool to Demonstrate the Consumer Data Right APIs
*
* NOTE: This class is auto generated by the codegen artefact
* https://github.com/ConsumerDataStandardsAustralia/java-artefacts/codegen
*/
package au.org.consumerdatastandards.client.cli;
import au.org.consumerdatastandards.client.ApiResponse;
import au.org.consumerdatastandards.client.ConformanceError;
import au.org.consumerdatastandards.client.Header;
import au.org.consumerdatastandards.client.cli.support.ClientCLIFactory;
import au.org.consumerdatastandards.client.cli.support.ClientCLIOptions;
import au.org.consumerdatastandards.client.model.banking.BaseResponse;
import au.org.consumerdatastandards.client.model.Links;
import au.org.consumerdatastandards.client.model.LinksPaginated;
import au.org.consumerdatastandards.client.model.MetaPaginated;
import au.org.consumerdatastandards.client.model.PaginatedResponse;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApiCliBase {
protected ClientCLIFactory clientFactory;
@Autowired
private void setApiClientOptions(ClientCLIOptions apiClientOptions) {
clientFactory = new ClientCLIFactory(apiClientOptions);
}
protected void throwConformanceErrors(List conformanceErrors) throws Exception {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("Received " + conformanceErrors.size() + " errors");
for (ConformanceError conformanceError : conformanceErrors) {
pw.println("Conformance Error: " + conformanceError.getDescription());
}
pw.println("Found a total of " + conformanceErrors.size() + " conformance errors");
throw new Exception(sw.toString());
}
protected int getEndpointVersion(ApiResponse response) {
Map headers = response.getHeaders();
List values = (List) headers.get(Header.VERSION.getKey());
if (values != null && !values.isEmpty()) {
String version = values.get(0);
if(StringUtils.isNumeric(version)) {
return Integer.parseInt(version);
}
}
return 1;
}
public List validateMetadata(String requestUrl, ApiResponse response) {
Integer page = 1, pageSize = 25;
Map params = extractParameters(requestUrl);
List errors = new ArrayList<>();
try {
Integer pageParam = getPageParameter(params);
if (pageParam != null) page = pageParam;
} catch (NumberFormatException e) {
errors.add(new ConformanceError()
.errorType(ConformanceError.Type.DATA_NOT_MATCHING_CRITERIA)
.errorMessage("Invalid 'page' parameter value " + getParameter(params, "page"))
);
}
try {
Integer pageSizeParam = getPageSizeParameter(params);
if (pageSizeParam != null) pageSize = pageSizeParam;
} catch (NumberFormatException e) {
errors.add(new ConformanceError()
.errorType(ConformanceError.Type.DATA_NOT_MATCHING_CRITERIA)
.errorMessage("Invalid 'page-size' parameter value " + getParameter(params, "page-size"))
);
}
Integer totalPages = null;
T body = response.getBody();
if (body instanceof PaginatedResponse) {
MetaPaginated meta = ((PaginatedResponse)body).getMeta();
if (meta != null) {
Integer totalRecords = meta.getTotalRecords();
totalPages = meta.getTotalPages();
if (totalRecords != null && totalPages != null
&& (totalRecords / pageSize + (totalRecords % pageSize > 0 ? 1 : 0)) != totalPages) {
errors.add(new ConformanceError().errorType(ConformanceError.Type.DATA_NOT_MATCHING_CRITERIA)
.errorMessage(String.format(
"totalPages %d does not match totalRecords / page-size + remainder of (totalRecords / page-size), "
+ "where page-size is " + pageSize + " . See below:\n%s", totalPages, meta.toString()))
);
}
}
}
Links links = (body instanceof PaginatedResponse ? ((PaginatedResponse)body).getLinks() : ((BaseResponse)body).getLinks());
if (links != null) {
String self = links.getSelf();
String linksJson = toJson(links);
checkSelfLink(requestUrl, errors, linksJson, self);
if (links instanceof LinksPaginated) {
String first = ((LinksPaginated) links).getFirst();
String prev = ((LinksPaginated) links).getPrev();
String next = ((LinksPaginated) links).getNext();
String last = ((LinksPaginated) links).getLast();
checkFirstLink(page, pageSize, errors, totalPages, linksJson, first);
checkLastLink(page, pageSize, errors, totalPages, linksJson, last);
checkPrevLink(page, errors, linksJson, prev);
checkNextLink(page, errors, totalPages, linksJson, next);
}
}
return errors;
}
private Map extractParameters(String url) {
String[] parts = url.split("\\?");
if (parts.length < 2) return null;
String queryString = parts[1];
Map params = new HashMap<>();
String[] queryParams = queryString.split("&");
for (String queryParam : queryParams) {
String[] keyValue = queryParam.split("=");
String key = keyValue[0];
Object value = keyValue.length > 1 ? keyValue[1] : Boolean.TRUE;
if (params.containsKey(key)) {
if (params.get(key) instanceof List) {
((List) params.get(key)).add(value);
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy