REST API for Lookup Manager
Introduction
Now 3rd party apps can get and edit lookup tables with the newly added REST endpoints
Resources
Table
Get information for all the tables (GET)
METHOD | URL | Data |
---|---|---|
GET | /rest/lookuprestresource/1.0/lookup/table | Return an array of table information Example
CODE
|
Sample code
//Get information for all the tables
def data = sendGetRequest("/rest/lookuprestresource/1.0/lookup/table/")
data.each{val ->
//TODO
}
Add a new table (POST)
METHOD | URL | Data |
---|---|---|
POST | /rest/lookuprestresource/1.0/lookup/table | The data should be a valid JSON string containing the following information:
Example
CODE
|
Sample code
def bodyData = 'data={"name":"New_Table","columns":["Column_1","Column_2"]}'
def responseData = sendPostRequest("/rest/lookuprestresource/1.0/lookup/table",bodyData)
//{columns=[{id=22, name=Column_1}, {id=23, name=Column_2}], name=New_Table, id=11}
Update an existing table (PUT)
METHOD | URL | Data |
---|---|---|
PUT | /rest/lookuprestresource/1.0/lookup/table/{TABLE_NAME} | The data should be a valid JSON string containing the following information:
Example
CODE
|
Sample code
def bodyData = '{"name":"New_Table","columns":["Column_1","Column_2"],"mode":"overwrite","result":[["Column_1","Column_2"],["admin","This is a message"]]}';
def responseData = sendPutRequest("/rest/lookuprestresource/1.0/lookup/table/New_Table",bodyData)
Delete an existing table (DELETE)
METHOD | URL | Data |
---|---|---|
DELETE | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID} | - |
Sample code
//Delete the data for table id 2
sendDeleteRequest("/rest/lookuprestresource/1.0/lookup/table/2")
Table row
Get rows information for a table (GET)
METHOD | URL | Data |
---|---|---|
GET | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/entries | Return the an array of rows information for a table Example
CODE
|
Sample code
//Get the rows information for table id 2
def data = sendGetRequest("/rest/lookuprestresource/1.0/lookup/table/2/entries")
data.each{val ->
//TODO
}
Add a new row in a table (POST)
METHOD | URL | Data | Sample Code |
---|---|---|---|
POST | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/entries | The row data should be a valid JSON string containing the following information:
Example
CODE
|
CODE
|
Sample code
def bodyData = 'data={"4":"Jira 8","5":"admin"}'
def responseData = sendPostRequest("/rest/lookuprestresource/1.0/lookup/table/2/entries", bodyData)
//Sample responseData: {4=Jira, 5=admin, id=20, tableid=2}
Update an existing row in a table (PUT)
METHOD | URL | Data |
---|---|---|
PUT | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/entries/{ROW_ID} | The data should be a valid JSON string containing the following information:
Example
CODE
|
Sample code
//Update the data for row id 20
def bodyData = 'data={"4":"Jira","5":"admin1"}'
def responseData = sendPutRequest("/rest/lookuprestresource/1.0/lookup/table/2/entries/20",bodyData)
//Sample responseData: {4=Jira, 5=admin1, id=20, tableid=2}
Delete an existing row in a table (DELETE)
METHOD | URL | Data |
---|---|---|
DELETE | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/entries/{ROW_ID} |
Sample code
//Delete the data for row id 20
sendDeleteRequest("/rest/lookuprestresource/1.0/lookup/table/2/entries/20")
Table Column
Get columns information for a table (GET)
METHOD | URL | Data |
---|---|---|
GET | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/columns | Return the columns information for a table Example
CODE
|
Sample code
//Get the columns information for table id 2
def data = sendGetRequest("/rest/lookuprestresource/1.0/lookup/table/2/columns")
data.each{val ->
//TODO
}
Add a new column in a table (POST)
METHOD | URL | Data |
---|---|---|
POST | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/column | The column name Example:
CODE
|
Sample code
def bodyData = 'columnName=COLUMN_NAME'
def responseData = sendPostRequest("/rest/lookuprestresource/1.0/lookup/table/2/column",bodyData)
Update an existing column in a table (PUT)
METHOD | URL | Data |
---|---|---|
PUT | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/column/{COLUMN_ID} | The new column name Example:
CODE
|
Sample code
def bodyData = 'newColumnName=NEW_COLUMN_NAME'
def responseData = sendPutRequest("/rest/lookuprestresource/1.0/lookup/table/2/column/6",bodyData)
Delete an existing column in a table (DELETE)
METHOD | URL | Data |
---|---|---|
DELETE | /rest/lookuprestresource/1.0/lookup/table/{TABLE_ID}/column/{COLUMN_ID} | - |
Sample code
def responseData = sendDeleteRequest("/rest/lookuprestresource/1.0/lookup/table/2/column/6")
Useful Methods
Import packages
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
Send GET request
def sendGetRequest(url) {
def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)
//Step 1: Set base URL, E.g. http://localhost:2990/jira
def baseUrl = "http://localhost:2990/jira";
//Step 2: Set username and password
def userName = "admin"
def password = "admin"
def http = new HTTPBuilder(baseUrl + url)
http.request(GET) {
headers.
'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
headers.
'X-Atlassian-Token' = 'no-check';
requestContentType = URLENC
response.success = {resp, data ->
log.debug("SUCCESS: " + resp.status)
log.debug(data)
}
response.failure = { resp,data ->
log.error("ERROR: " + resp.status)
log.error(data)
}
}
}
Send POST request with data
def sendPostRequest(url, bodyData) {
def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)
def json = new groovy.json.JsonBuilder()
//Step 1: Set base URL, E.g. http://localhost:2990/jira
def baseUrl = "http://localhost:2990/jira";
//Step 2: Set username and password
def userName = "admin"
def password = "admin"
def http = new HTTPBuilder(baseUrl + url)
http.request(POST) {
headers.
'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
headers.
'X-Atlassian-Token' = 'no-check';
requestContentType = URLENC
body = bodyData
response.success = {resp, data ->
log.debug("SUCCESS: " + resp.status)
log.debug(data)
return data
}
response.failure = { resp,data ->
log.error("ERROR: " + resp.status)
log.error(data)
return null
}
}
}
Send PUT request with data
def sendPutRequest(url, bodyData) {
def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)
def json = new groovy.json.JsonBuilder()
//Step 1: Set base URL, E.g. http://localhost:2990/jira
def baseUrl = "http://localhost:2990/jira";
//Step 2: Set username and password
def userName = "admin"
def password = "admin"
def http = new HTTPBuilder(baseUrl + url)
http.request(PUT) {
headers.
'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
headers.
'X-Atlassian-Token' = 'no-check';
requestContentType = URLENC
body = bodyData
response.success = {resp, data ->
log.debug("SUCCESS: " + resp.status)
log.debug(data)
return data
}
response.failure = { resp,data ->
log.error("ERROR: " + resp.status)
log.error(data)
return null
}
}
}
Send DELETE request
def sendDeleteRequest(url) {
def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)
def json = new groovy.json.JsonBuilder()
//Step 1: Set base URL, E.g. http://localhost:2990/jira
def baseUrl = "http://localhost:2990/jira";
//Step 2: Set username and password
def userName = "admin"
def password = "admin"
def http = new HTTPBuilder(baseUrl + url)
http.request(DELETE) {
headers.
'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
headers.
'X-Atlassian-Token' = 'no-check';
requestContentType = URLENC
response.success = {resp, data ->
log.debug("SUCCESS: " + resp.status)
log.debug(data)
return data
}
response.failure = { resp,data ->
log.error("ERROR: " + resp.status)
log.error(data)
return null
}
}
}
Convert map to json object
def convertMapToJson(data) {
return JsonOutput.prettyPrint(JsonOutput.toJson(data))
}