Search code examples
grailsgroovyrx-java

Groovy HTTPBuilder + RxJava returns an empty object/observable


def login(String email, String password, String apiKey) throws HttpResponseException {

    def postBody = [
            email   : email,
            password: password
    ]

    def http = new HTTPBuilder('https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=' + apiKey)

    return Observable.just({ Observer observer ->

        def thread = new Thread({

            http.post(body: postBody, requestContentType: ContentType.JSON) { resp, json  ->
                println 'status code ' + resp.statusLine.statusCode
                if (resp.statusLine.statusCode == 200 || resp.statusLine.statusCode == 201) {
                    observer.onNext(json)
                } else {
                    observer.onError(new Throwable('broken'))
                }

                observer.onCompleted()

            }

            http.handler.failure = { resp ->
                observer.onError(new Throwable('failure'))
                observer.onCompleted()

            }

            http.handler.'400' = { resp ->
                observer.onError(new Throwable('bad request'))
                observer.onCompleted()

            }

        } as Runnable)

        thread.start()

        return Subscriptions.empty()
    })
}

So this is the method that I wrote in the service class to call login for Firebase auth REST API. But the thing right now is that it didnt return me any data.

I was debugging this in Intellij and what I found out is that when I do a login.subscribe I was expecting it to return json to me but it didn't.

So what did I do wrong?


Solution

  • I'm no Groovy developer but this line seems wrong:

    return Observable.just({ Observer observer ->
    

    You are creating a lambda object to be returned by just but nothing inside really gets executed. You are probably looking for

    return Observable.create({ Emitter emitter -> 
    

    Edit:

    JavaDoc