0

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?

1 Answer 1

0

So I asked the question and am posting the solution(s) I found. Hopefully folks will add better solutions and maybe even explain what I missed in my initial attempts.

Here is my preferred solution (all three solutions below are from googling but I lost the links, feel free to poke me and I will dig them up--kudos to original posters):

String furl = "${FUSION_API_BASE}${path}" //http://localhost:8764/api/apollo/introspect
RESTClient rc = new RESTClient(furl)
rc.headers['Authorization'] = 'Basic ' + "$user:$pass".bytes.encodeBase64()
//rc.headers['Authorization'] = 'Basic ' + "$user:$pass".getBytes('iso-8859-1').encodeBase64()
def foo = rc.get([:])
log.info "Foo: $foo"

And another working solution:

RESTClient rest = new RESTClient( 'http://localhost:8764/' )
HttpClient client = rest.client
client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'sean:mypass'.bytes.encodeBase64().toString())
    }
})
def resp = rest.get( path : path)
assert resp.status == 200  // HTTP response code; 404 means not found, etc.
println resp.getData()

And for those keeping score at home, a python solution for comparison:

import requests
from requests.auth import HTTPBasicAuth
rsp = requests.get('http://corp:8764/api/apollo/introspect', auth=HTTPBasicAuth('sean', 'lucid4pass'))
print "Response ok/status code: %s/%s", rsp.ok, rsp.status_code
print "Response content: %s", rsp.content

HTH,

Sean

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.