Last active
July 13, 2016 17:08
-
-
Save nullset2/a581a29a81c81b71038f055ff2b7df9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.net.http.* | |
//if you import these classes statically, you'll be able to use the classes you import with this without qualified access | |
//notice how we're using just POST in some calls below, for example, instead of having to do Method.POST | |
import static groovyx.net.http.ContentType.* | |
import static groovyx.net.http.Method.* | |
class LoginService{ | |
def authenticateURL = //url | |
def decryptURL = //url | |
//there's definitely some better way to send the password as digest or an encrypted string but that is left to the student to explore | |
def authenticate(String username, String password){ | |
def params = //whatever params that you need go here, along with request body, represented as maps | |
def httpClient = new HTTPBuilder(authenticateURL) //this is where the magic happens | |
try { | |
httpClient.request(POST) { | |
uri.path = "/login" //path for your authentication endpoint | |
//whatever params that you need go here, along with request body, for example uri.params = someMap | |
response.success = { resp, reader -> | |
println "Authentication request for $username successful - ${resp.statusLine}" | |
//do whatever you need with the response | |
} | |
} | |
} catch (groovyx.net.http.HttpResponseException ex) { | |
ex.printStackTrace() | |
return null | |
} catch (java.net.ConnectException ex) { | |
ex.printStackTrace() | |
return null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment