0

Might be the wrong place to post this but I have been messing around with async http builders trying to get basic cypher queries to work. It works with Http Builders but can't get it to work with the async version.

   @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' )
    @Grab(group='net.sf.json-lib', module='json-lib', version='2.4', classifier='jdk15' )

    import groovyx.net.http.*
    import static groovyx.net.http.ContentType.*
    import static groovyx.net.http.Method.*

    def query(statement, params,success, error) {
    def http = new HTTPBuilder( 'http://localhost:7474' )
    http.request( POST, JSON ) {
    uri.path = '/db/data/cypher/'
    headers.'X-Stream' = 'true'
    requestContentType = JSON
    body =  [ query : statement , params : params ?: [:] ]

     // uri.query = [ param : 'value' ]

     response.success = { resp, json ->
       if (success) success(json)
       else {
        println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
       }
     }

     response.failure = { resp, message ->
           def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
           result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
           result.message = message
       if (error) {
                 error(result)
       } else {
        println "Status: ${result.status} : ${result.statusText} "
        println 'Headers: ${result.headers}'
        println 'Message: ${result.message}'
       }
     }
    }
   }

query("MATCH n RETURN n;",[],{ println "Success: ${it}" },{ println "Error: ${it}" })

However I have tried this with the AsyncHttpBuilder. Couldn't get it to work. Now I am trying a simple thing and have been unable to get it to give anytype of useful result.

@Test
public void testQueue()
{
    def http = new AsyncHTTPBuilder( poolSize : 1 ,
            uri : 'http://localhost:7474/db/data/cypher' )
    def responses = []
    responses << http.post(query : [q:  "MATCH n RETURN n;"]) {return it}


    if (!responses.every{it.done})
    {
        println 'waiting...'
        Thread.sleep(2000)
    }
    responses.each {
        println(it)
    }
    http.shutdown()
} 

Any thoughts? Thanks!

4
  • 1
    In last code piece, maybe you should do while(!responces.every{it.done})? Moreover, AsyncBuilder works same as Sync one, it reports completion on responses callbacks.
    – Seagull
    Dec 24, 2013 at 14:13
  • So it will just tell me whether the request went through?
    – Badmiral
    Dec 24, 2013 at 14:39
  • what happens when you run this test? Dec 24, 2013 at 21:58
  • Look at the example here: groovy.codehaus.org/modules/http-builder/doc/async.html, it shows how to wait for async request to complete
    – akhikhl
    Dec 24, 2013 at 23:07

1 Answer 1

1

for reference: I've answered this at https://groups.google.com/forum/?fromgroups#!topic/neo4j/5Cle5vBsMXQ

you need to pass in the cypher query in the request's body and not as query param. See https://gist.github.com/sarmbruster/8114445 for a working example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.