Login user against Steademy API
Groovy & Grails March 20th, 2009
Steademy exposes a public API that allows any third party tool to directly connect to steademy.
My first blog outlines the groovy & grails way to login against the steademy platform and to receive a valid sessionKey.
The steademy sessionKey uniquely identifies a user at our platform. You have to deliver it with every consequent request.
We accept simple POST based requests and return JSON strings.
Setup:
You basically need to setup a grails project (grails create-app std), and create your service (grails create-service steademy)
Furthermore the xfire plugin needs to be nstalled into your grails project.
I downloaded release 0.8.1 from here
Adjust config
Getting rid of static properties in our class is our first goal
We therefore add a couple of new lines into grails-conf/Config.groovy
Grails provides a very elegant way to read properties from the config file.
grails-app/conf/Config.groovy std{ msapi{ url = "https://steademy.com login.path="/msapi/Login" } }
SteademyService
Our groovy driven service only needs a couple of code-lines for proper execution.
grails-service/SteademyService.groovy ... class SteademyService implements InitializingBean{ boolean transactional = true def grailsApplication def msapi def crs void afterPropertiesSet(){ msapi = grailsApplication.config.std.msapi crs = grailsApplication.config.std.crs } ... String loginUser(String login, String password){ log.debug "remote login to steademy using [login:${login}] [password:${password}]" def builder = new HTTPBuilder(msapi.url+msapi.login.path) builder.request(POST, JSON){ req -> send URLENC,[login:login, password:password, appId:"12345678", appversion:"1.0"] response.success = {resp,json -> if(json.error){ log.debug "ERROR: ${json.error}" return null } return json.steademy.sessionKey } response.failure = { log.debug "url ${msapi.url}/${msapi.login.path} not valid" return null } } } }
- The service implements the Initalizing Bean. Thus the method afterPropertiesSet is called once all properties have been initialized by spring.
- We directly reference the properties set in the grails-app/conf/Config.groovy file by defining the grailsApplication object. Grails/Spring in the backend takes care for its proper binding. The command grailsApplication.config.std.crs finally reads the corresponding property from with the config file.
The actual login method is quite simple. Due to grails perfect support for JSON and thanks to the http-builder project, some simple lines of code connect to the steademy api and process the user login.
SteademyServiceTest
Lets step forward to our test. If you created the service in the grails way, there should exist a groovy class called. test/integration/SteademyServiceTest.
SteademyServicetest ... def steademyService void testSteademyLogin(){ steademyService.loginUser("foo", "bar") }
Thats it basically.
The next blog will outline a couple of more API calls that allow us to send messages through steademy.
Thorsten Maus
About
Great site this blog.steademy.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor