import groovyx.net.http.HttpResponseDecorator;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.entity.StringEntity;
import groovy.json.JsonSlurper;
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
@Grapes([
@Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.5.1'),
@GrabExclude('org.codehaus.groovy:groovy')
])
//Your Jira api url
def jiraApiUrl = 'localhost:8989/rest/api/2/'
//Your user and password
def basic = 'Basic ' +
'user:password'.bytes.encodeBase64().toString()
//New issue to add
def msg= '''
{
"fields": {
"project": {
"id": "10000"
},
"summary": "something's test www",
"issuetype": {
"id": "10000"
},
"assignee": {
"name": "luigi"
},
"labels": [
"bugfix",
"blitz_test"
],
"timetracking": {
"originalEstimate": "10",
"remainingEstimate": "5"
},
"environment": "environment",
"description": "description",
"duedate": "2011-03-11",
"customfield_10007": "Epic Name is required."
}
}
'''
def bodyMap= new JsonSlurper().parseText(msg)
println bodyMap
//Url and path
def myClient = new groovyx.net.http.HTTPBuilder('http://'+jiraApiUrl+'issue/')
//Proxy to use Fiddler
myClient.setProxy('localhost', 8888, 'http')
results = myClient.request(POST, JSON) { req ->
req.addHeader('Authorization', basic)
req.addHeader('Content-Type' ,'application/json')
req.addHeader("accept", "application/json");
requestContentType = JSON
body = bodyMap
println req.getAllHeaders()
response.success = { resp, reader ->
println "SUCCESS! ${resp.statusLine}"
}
response.failure = { resp ->
println "FAILURE! ${resp.properties}"
}
}
fredag 14 mars 2014
Groovy HTTPBuilder POST: JIRA API
How to create a Rest client to
Groovy RestClient GET; JIRA API
How to create a Rest client to
import groovyx.net.http.RESTClient;
import groovyx.net.http.HttpResponseDecorator;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;
import groovy.json.JsonSlurper;
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
@Grapes([
@Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.5.1'),
@GrabExclude('org.codehaus.groovy:groovy')
])
def jiraApiUrl = 'http://localhost:8989/rest/api/2/'
def jiraClient = new RESTClient(jiraApiUrl)
def basic = 'Basic ' +
'user:password'.bytes.encodeBase64().toString()
jiraClient.client.addRequestInterceptor(
new HttpRequestInterceptor() {
void process(HttpRequest httpRequest,
HttpContext httpContext) {
httpRequest.addHeader('Authorization', basic)
}
})
def serverInfo = jiraClient.get(path: 'issue/SPACE-4')
println serverInfo.data
def slurp =
new JsonSlurper().parseText(serverInfo.data.toString())
println slurp.fields.description
måndag 3 mars 2014
Modular Groovy Scripting
First we create an abstract class like this:
package test
public abstract class IncludeScript extends Script {
def includeScript(scriptClass) {
GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(new File(scriptClass));
def scriptInstance = clazz.newInstance()
scriptInstance.metaClass = this.metaClass
scriptInstance.binding = new ConfigBinding(this.getBinding().callable)
scriptInstance.&run.call()
}
}
Compile the IncludeScript.groovy with groovyc and add the class-file to your classpath.
Second we create something to test on like:
service{
endpoint ="http://mysite.se/Service"
port=4322
user="admin"
password="YmlsbDR5b3U="
}
//----------------------------------------------------------------------------------------
//Copy the code above to a file and name it to ServiceConfig.groovy
//----------------------------------------------------------------------------------------
import test.IncludeScript
class SecurityConfig extends IncludeScript {
def run() { // normal contents of a config file go in here
security {
includeScript( "c:\\tmp\\ServiceConfig.groovy" )
active = true
}
}
}
//----------------------------------------------------------------------------------------
//Copy the code above to a file and name it to what you want, I named my to prop.groovy
//----------------------------------------------------------------------------------------
Now we test our modularity...
def conf = new ConfigSlurper().parse( new File("c:\\tmp\\prop.groovy").toURL() )
println conf.security.service.password
println conf.security.active
Prenumerera på:
Inlägg (Atom)