artifactory-client-java
artifactory-client-java copied to clipboard
Ability to add custom queries in REST api call
I wanted to use this REST api call so I could find the last modifed item easily: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-ItemLastModified I couldn't figure out how to add the custom query (?lastModified) in the request so I decided to just use the GAVC search. That's not too hard in this particular case, but it would be nice to be able to specify custom queries so people can use REST API calls like that.
EDIT: I found that I could do this by doing addQueryParam with lastModifed as the key, with null value, but that's somewhat counterintuitive.
The API provides a way to send any type of query to the REST API.
Create a class like this
import org.jfrog.artifactory.client.ArtifactoryRequest
class ArtifactoryRequestImpl implements ArtifactoryRequest {
private def method
private def apiUrl
private def queryParams
private def headers
private def body
private def responseType
private def requestType
ArtifactoryRequestImpl(){
requestType = ArtifactoryRequest.ContentType.ANY
responseType = ArtifactoryRequest.ContentType.ANY
queryParams = new HashMap<String, String>()
headers = new HashMap<String, String>()
}
ArtifactoryRequestImpl apiUrl(String url){
this.apiUrl = url
this
}
ArtifactoryRequestImpl method(ArtifactoryRequest.Method method) {
this.method = method
this
}
ArtifactoryRequestImpl setQueryParams(Map<String, String> queryParams) {
this.queryParams = queryParams
this
}
ArtifactoryRequestImpl setHeaders(Map<String, String> headers) {
this.headers = headers
this
}
ArtifactoryRequestImpl addHeader(String key, String value){
headers.put(key, value)
this
}
ArtifactoryRequestImpl addQueryParam(String key, String value){
queryParams.put(key, value)
this
}
def <T> ArtifactoryRequestImpl requestBody(T body) {
this.body = body
this
}
ArtifactoryRequestImpl responseType(ArtifactoryRequest.ContentType responseType){
this.responseType = responseType
this
}
ArtifactoryRequestImpl requestType(ArtifactoryRequest.ContentType requestType){
this.requestType = requestType
this
}
ArtifactoryRequest.Method getMethod() {
this.method
}
Map<String, String> getQueryParams() {
this.queryParams
}
Map<String, String> getHeaders() {
this.headers
}
def <T> T getBody() {
this.body
}
ArtifactoryRequest.ContentType getResponseType() {
responseType
}
String getApiUrl() {
apiUrl
}
ArtifactoryRequest.ContentType getRequestType() {
requestType
}
}
Then use it this way. This is an example to invoke the "List Files" service.
import org.jfrog.artifactory.client.Artifactory
import org.jfrog.artifactory.client.ArtifactoryRequest
def request = new ArtifactoryRequestImpl()
.apiUrl("api/storage/${repository}")
.addQueryParam('list', null)
.addQueryParam('deep', deep ? '1' : '0')
.addQueryParam('depth', "${depth}")
.addQueryParam('listFolders', listFolders ? '1' : '0')
.method(ArtifactoryRequest.Method.GET)
.requestType(ArtifactoryRequest.ContentType.JSON)
.responseType(ArtifactoryRequest.ContentType.JSON)
def json = artifactory.restCall(request)
// See structure of the JSON response here: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-FileList
json['files'].collect { new ArtifactoryItem(it) }
This should help you to get started.