ScritpUnit and JSON data

12 Mar '13, 06:32 PM
18,079 Views
No Forum Badges

Hi, I'm trying to write in a ScriptUnit an HTTP get with JSON.

I have this example from http://groovy.codehaus.org/modules/http-builder/doc/json.html

How can I pass response out of ScriptUnit?

This is the code:

import groovyx.net.http.*
@Grab(group='org.codehaus.groovy.modules.http-builder',
    module='http-builder', version='0.5.2' )

def http = new HTTPBuilder( 'http://twitter.com/statuses/' )

http.get( path: 'user_timeline.json',
        query: [id:'httpbuilder', count:5] ) { resp, json ->

    println resp.status

    json.each {  // iterate over JSON 'status' object in the response:
        println it.created_at
        println '  ' + it.text
    }
}

 
x 0
Follow
Answer Answer at this question and get points!
No Forum Badges

The most basic way of returning outputs from a Scirpt Unit is to just use the return statement.

For example, you may extract data from your JSON structure and turn it into a list of Groovy maps, and then return the entire list.

def result = []
def http = new HTTPBuilder('http://twitter.com/statuses/')
http.get(path: 'user_timeline.json', query: [id:'httpbuilder', count:5] ) { resp, son ->
    def entry = [""created"": it.created_at, ""text"": it.text]
    result.add(entry)
}
return result

Another possibility, if your data is ""flat"", is returning a series of related lists, each one including a piece of data.

#output String[] creationTimes
#output String[] texts
def creationTimes = []
def texts = []
def http = new HTTPBuilder('http://twitter.com/statuses/')
http.get(path: 'user_timeline.json', query: [id:'httpbuilder', count:5] ) { resp, son ->
    creationTimes.add(it.created_at)
    texts.add(it.text)
}
return [""creationTimes"": creationTimes, ""texts"", texts]

This will cause your Script Unit to output two parameters that can be conveniently coupled to two attributes, for example of a Create Unit for storing data on database. For more information about the #output syntax, please see the wiki article introducing the Script Unit (section Declaring typed inputs and outputs).

   
x 2
No Forum Badges

I'm very sad, there is a regression update.

Since this summer, Twitter API were updatet to version 1.1 and my code doesn't work anymore.

I need to add the autentication before to call the search. I created my own authentication into Twitter portal and found some code examples for application authentication, but this not work.

....
OAuthConsumer consumer = new DefaultOAuthConsumer(
"<my Key>",
"<my Secret>");

OAuthProvider provider = new DefaultOAuthProvider(
            "https://api.twitter.com/oauth/request_token",
            "https://api.twitter.com/oauth/access_token",
            "https://api.twitter.com/oauth/authorize");

String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
String pinCode = "<my PIN>";
provider.retrieveAccessToken(consumer, pinCode);

consumer.setTokenWithSecret(consumer.getToken(), consumer.getTokenSecret());

URL url = new URL("http://twitter.com/statuses/mentions.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
request.connect();
int statusCode = request.getResponseCode();

http.request('https://api.twitter.com' , GET, JSON ) {
    uri.path = '/1.1/search/tweets.json'
    uri.query = [q: i_query ]
....

Any idea?

Tks, Alessio.

[ WebRatio Personal Version: 7.0.5 Build id: 201305081004 ]

 
x 0
Answer at this question and get points!