Restful Client update
So I have just finished the first class of my rest client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import groovy.xml.StreamingMarkupBuilder import java.net.HttpURLConnection; import apphub.util.RESTClient /** * Created by IntelliJ IDEA. * User: Chauncey-JL * Date: Jan 13, 2010 * Time: 11:59:39 AM * To change this template use File | Settings | File Templates. */ class AccessLevel { String appCode String accessTypeCode String accessLevelCode String displayName def static list(appId, accessTypeCode) { def accessLevels = [] def client = new RESTClient(url:"http://localhost:8080/AppHub/accessLevel/${appId}/${accessTypeCode}").makeRemoteCall() if(client.status == HttpURLConnection.HTTP_OK) { client.xml.children().each { list -> def accessLevel = new AccessLevel() list.children().each { child -> accessLevel.setProperty(child.name(), child.text()) } accessLevels.add(accessLevel) } } return accessLevels } def static get(appId, accessTypeCode, accessLevelCode) { def client = new RESTClient(url:"http://localhost:8080/AppHub/accessLevel/${appId}/${accessTypeCode}/${accessLevelCode}").makeRemoteCall() if(client.status == HttpURLConnection.HTTP_OK) { def accessLevel = new AccessLevel() client.xml.children().each { child -> accessLevel.setProperty(child.name(), child.text()) } return accessLevel } } def save() { def client = new RESTClient(url:"http://localhost:8080/AppHub/accessLevel", method:"POST", body:this.toXML().toString()).makeRemoteCall() if(client.status == HttpURLConnection.HTTP_CREATED) { return true } else { return false } } def update() { def client = new RESTClient(url:"http://localhost:8080/AppHub/accessLevel/${this.appCode}/${this.accessTypeCode}/${this.accessLevelCode}", method:"PUT", body:this.toXML().toString()).makeRemoteCall() if(client.status == HttpURLConnection.HTTP_OK) { return true } else { return false } } def delete() { def client = new RESTClient(url:"http://localhost:8080/AppHub/accessLevel/${this.appCode}/${this.accessTypeCode}/${this.accessLevelCode}", method:"DELETE", body:this.toXML().toString()).makeRemoteCall() if(client.status == HttpURLConnection.HTTP_OK) { return true } else { return false } } private toXML() { StreamingMarkupBuilder builder = new StreamingMarkupBuilder(); def accessLevel = { mkp.xmlDeclaration() accessLevel { appCode(this.appCode) accessTypeCode(this.accessTypeCode) accessLevelCode(this.accessLevelCode) displayName(this.displayName) } } builder.bind(accessLevel) } def String toString() { this.toXML() } } |
And the test class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package apphub.client //def accessLevel = new AccessLevel(appCode:'LEGALDOC', accessTypeCode:'ACCESS_LVL', accessLevelCode:'TEST', displayName:'TEST') //accessLevel.save() AccessLevel.list('LEGALDOC', 'ACCESS_LVL').each { println "APPCODE:${it.appCode}" } println "AccessLevel.get().displayName:"+AccessLevel.get('LEGALDOC', 'ACCESS_LVL', 'USER').displayName def lvl = new AccessLevel(appCode:'LEGALDOC', accessTypeCode:'ACCESS_LVL', accessLevelCode:'TEST', displayName:'TEST') println "AccessLevel.save():${lvl.save()}" lvl.displayName = 'Updated Display Name' println "AccessLevel.update():${lvl.update()} --- Display Name:${lvl.displayName}" |
Leave a comment
You must be logged in to post a comment.

