mirror of
https://github.com/Qortal/qortal.git
synced 2025-02-14 11:15:49 +00:00
Adapted GET /arbitrary/resources endpoint to allow filtering by identifier
- If an identifier parameter is missing or empty, it will return an unfiltered list of all possible identifiers. - If an identifier is specified, only resources with a matching identifier will be returned. - If default is set to true, only resources without identifiers will be returned.
This commit is contained in:
parent
bc38184ebf
commit
e90ecd2085
@ -84,7 +84,6 @@ public class AdminResource {
|
|||||||
@Parameter(in = ParameterIn.QUERY, name = "limit", description = "Maximum number of entries to return, 0 means unlimited", schema = @Schema(type = "integer", defaultValue = "20"))
|
@Parameter(in = ParameterIn.QUERY, name = "limit", description = "Maximum number of entries to return, 0 means unlimited", schema = @Schema(type = "integer", defaultValue = "20"))
|
||||||
@Parameter(in = ParameterIn.QUERY, name = "offset", description = "Starting entry in results, 0 is first entry", schema = @Schema(type = "integer"))
|
@Parameter(in = ParameterIn.QUERY, name = "offset", description = "Starting entry in results, 0 is first entry", schema = @Schema(type = "integer"))
|
||||||
@Parameter(in = ParameterIn.QUERY, name = "reverse", description = "Reverse results", schema = @Schema(type = "boolean"))
|
@Parameter(in = ParameterIn.QUERY, name = "reverse", description = "Reverse results", schema = @Schema(type = "boolean"))
|
||||||
@Parameter(in = ParameterIn.QUERY, name = "includestatus", description = "Include status", schema = @Schema(type = "boolean"))
|
|
||||||
public String globalParameters() {
|
public String globalParameters() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,10 @@ public class ArbitraryResource {
|
|||||||
@GET
|
@GET
|
||||||
@Path("/resources")
|
@Path("/resources")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "List arbitrary resources available on chain, optionally filtered by service",
|
summary = "List arbitrary resources available on chain, optionally filtered by service and identifier",
|
||||||
|
description = "- If an identifier parameter is missing or empty, it will return an unfiltered list of all possible identifiers.\n" +
|
||||||
|
"- If an identifier is specified, only resources with a matching identifier will be returned.\n" +
|
||||||
|
"- If default is set to true, only resources without identifiers will be returned.",
|
||||||
responses = {
|
responses = {
|
||||||
@ApiResponse(
|
@ApiResponse(
|
||||||
content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ArbitraryResourceInfo.class))
|
content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ArbitraryResourceInfo.class))
|
||||||
@ -76,19 +79,29 @@ public class ArbitraryResource {
|
|||||||
)
|
)
|
||||||
@ApiErrors({ApiError.REPOSITORY_ISSUE})
|
@ApiErrors({ApiError.REPOSITORY_ISSUE})
|
||||||
public List<ArbitraryResourceInfo> getResources(
|
public List<ArbitraryResourceInfo> getResources(
|
||||||
@QueryParam("service") Service service, @Parameter(
|
@QueryParam("service") Service service,
|
||||||
ref = "limit"
|
@QueryParam("identifier") String identifier,
|
||||||
) @QueryParam("limit") Integer limit, @Parameter(
|
@Parameter(description = "Default resources (without identifiers) only") @QueryParam("default") Boolean defaultResource,
|
||||||
ref = "offset"
|
@Parameter(ref = "limit") @QueryParam("limit") Integer limit,
|
||||||
) @QueryParam("offset") Integer offset, @Parameter(
|
@Parameter(ref = "offset") @QueryParam("offset") Integer offset,
|
||||||
ref = "reverse"
|
@Parameter(ref = "reverse") @QueryParam("reverse") Boolean reverse,
|
||||||
) @QueryParam("reverse") Boolean reverse, @Parameter(
|
@Parameter(description = "Include status") @QueryParam("includestatus") Boolean includeStatus) {
|
||||||
ref = "includestatus"
|
|
||||||
) @QueryParam("includestatus") Boolean includeStatus) {
|
|
||||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
|
|
||||||
|
// Treat empty identifier as null
|
||||||
|
if (identifier != null && identifier.isEmpty()) {
|
||||||
|
identifier = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that "default" and "identifier" parameters cannot coexist
|
||||||
|
boolean defaultRes = Boolean.TRUE.equals(defaultResource);
|
||||||
|
if (defaultRes == true && identifier != null) {
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.INVALID_CRITERIA, "identifier cannot be specified when requesting a default resource");
|
||||||
|
}
|
||||||
|
|
||||||
List<ArbitraryResourceInfo> resources = repository.getArbitraryRepository()
|
List<ArbitraryResourceInfo> resources = repository.getArbitraryRepository()
|
||||||
.getArbitraryResources(service, limit, offset, reverse);
|
.getArbitraryResources(service, identifier, defaultRes, limit, offset, reverse);
|
||||||
|
|
||||||
if (resources == null) {
|
if (resources == null) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
|
@ -23,7 +23,7 @@ public interface ArbitraryRepository {
|
|||||||
public ArbitraryTransactionData getLatestTransaction(String name, Service service, Method method, String identifier) throws DataException;
|
public ArbitraryTransactionData getLatestTransaction(String name, Service service, Method method, String identifier) throws DataException;
|
||||||
|
|
||||||
|
|
||||||
public List<ArbitraryResourceInfo> getArbitraryResources(Service service, Integer limit, Integer offset, Boolean reverse) throws DataException;
|
public List<ArbitraryResourceInfo> getArbitraryResources(Service service, String identifier, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException;
|
||||||
|
|
||||||
|
|
||||||
public List<ArbitraryPeerData> getArbitraryPeerDataForSignature(byte[] signature) throws DataException;
|
public List<ArbitraryPeerData> getArbitraryPeerDataForSignature(byte[] signature) throws DataException;
|
||||||
|
@ -295,16 +295,29 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArbitraryResourceInfo> getArbitraryResources(Service service, Integer limit, Integer offset, Boolean reverse) throws DataException {
|
public List<ArbitraryResourceInfo> getArbitraryResources(Service service, String identifier,
|
||||||
|
boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException {
|
||||||
StringBuilder sql = new StringBuilder(512);
|
StringBuilder sql = new StringBuilder(512);
|
||||||
|
|
||||||
sql.append("SELECT name, service, identifier FROM ArbitraryTransactions");
|
sql.append("SELECT name, service, identifier FROM ArbitraryTransactions WHERE 1=1");
|
||||||
|
|
||||||
if (service != null) {
|
if (service != null) {
|
||||||
sql.append(" WHERE service = ");
|
sql.append(" AND service = ");
|
||||||
sql.append(service.value);
|
sql.append(service.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (defaultResource) {
|
||||||
|
// Default resource requested - use NULL identifier
|
||||||
|
// The AND ? IS NULL AND ? IS NULL is a hack to make use of the identifier params in checkedExecute()
|
||||||
|
identifier = null;
|
||||||
|
sql.append(" AND (identifier IS NULL AND ? IS NULL AND ? IS NULL)");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Non-default resource requested
|
||||||
|
// Use an exact match identifier, or list all if supplied identifier is null
|
||||||
|
sql.append(" AND (identifier = ? OR (? IS NULL))");
|
||||||
|
}
|
||||||
|
|
||||||
sql.append(" GROUP BY name, service, identifier ORDER BY name");
|
sql.append(" GROUP BY name, service, identifier ORDER BY name");
|
||||||
|
|
||||||
if (reverse != null && reverse) {
|
if (reverse != null && reverse) {
|
||||||
@ -315,14 +328,14 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
|||||||
|
|
||||||
List<ArbitraryResourceInfo> arbitraryResources = new ArrayList<>();
|
List<ArbitraryResourceInfo> arbitraryResources = new ArrayList<>();
|
||||||
|
|
||||||
try (ResultSet resultSet = this.repository.checkedExecute(sql.toString())) {
|
try (ResultSet resultSet = this.repository.checkedExecute(sql.toString(), identifier, identifier)) {
|
||||||
if (resultSet == null)
|
if (resultSet == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
String name = resultSet.getString(1);
|
String name = resultSet.getString(1);
|
||||||
Service serviceResult = Service.valueOf(resultSet.getInt(2));
|
Service serviceResult = Service.valueOf(resultSet.getInt(2));
|
||||||
String identifier = resultSet.getString(3);
|
String identifierResult = resultSet.getString(3);
|
||||||
|
|
||||||
// We should filter out resources without names
|
// We should filter out resources without names
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
@ -332,7 +345,7 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository {
|
|||||||
ArbitraryResourceInfo arbitraryResourceInfo = new ArbitraryResourceInfo();
|
ArbitraryResourceInfo arbitraryResourceInfo = new ArbitraryResourceInfo();
|
||||||
arbitraryResourceInfo.name = name;
|
arbitraryResourceInfo.name = name;
|
||||||
arbitraryResourceInfo.service = serviceResult;
|
arbitraryResourceInfo.service = serviceResult;
|
||||||
arbitraryResourceInfo.identifier = identifier;
|
arbitraryResourceInfo.identifier = identifierResult;
|
||||||
|
|
||||||
arbitraryResources.add(arbitraryResourceInfo);
|
arbitraryResources.add(arbitraryResourceInfo);
|
||||||
} while (resultSet.next());
|
} while (resultSet.next());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user