I have started working with Lucidworks Fusion (2.1.2) and am most comfortable hacking with Groovy. Side note: python 'requests' handled this seamlessly, but I am stubborn and don't want to use python...
Fusion has promising API which I look forward to working with in Groovy.
How do I best connect to the Fusion authenticated API in Fusion using Groovy (in a groovy-ish manner)?
I have tried several approaches (and finally found a few than worked). I welcome feedback on why the basic RESTClient doesn't work for me, along with other 'simple' solutions.
Here is what I tried:
groovyx.net.http.HTTPBuilder hb = new HTTPBuilder(FUSION_API_BASE)
hb.auth.basic(user, pass)
That fails with a 401 unauthorized (because of encoding I believe). HTTPBuilder came from gradle:
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
I also tried:
HttpPost httpPost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "sean"));
nvps.add(new BasicNameValuePair("password", "mypass"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
and got:
{"code":"unauthorized"}
Also tried:
String path = '/api/apollo/introspect'
URL url = new URL('http', 'corp', 8764, path)
try {
def foo = url.getContent()
log.info "Foo: $foo"
} catch (IOException ioe){
log.warn "IO ERR: $ioe"
}
which threw (a now expected) IOError: 401. If anyone wants more info on my failures let me know and I can likely bore you with voluminous technical details.
I am shamelessly answering my own question (below) but hope that some groovy sensei out there can enlighten me a bit.
So to recap: is there a better/groovier solution than what I found below?