Last active
December 20, 2015 21:18
-
-
Save davejsmith/6196661 to your computer and use it in GitHub Desktop.
SSRE rest client example
This file contains hidden or 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
/** | |
* Simple groovy script for retrieving sample records from SSRE REST service. | |
*/ | |
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' ) | |
import groovyx.net.http.* | |
import static groovyx.net.http.ContentType.* | |
import groovy.xml.Namespace | |
def serviceUrl = 'http://devel-13.ssdt-ohio.org' | |
def contextPath = '/ssre-webapp-live' | |
if (args) { | |
serviceUrl = args[0] | |
} | |
def ns2 = new Namespace("http://www.ssdt-ohio.org/ssre/model/2012") | |
def http = new HTTPBuilder(serviceUrl) | |
http.auth.basic 'ode', '******' | |
def done = false | |
def index = 0 | |
def max = 100 | |
def count = 0 | |
def batchCount = 0 | |
def since = '2011-03-27T11:00:00' | |
def start = System.currentTimeMillis() | |
http.handler.'404' = { | |
println "404 response (EOD)" | |
done = true | |
} | |
while (!done) { | |
http.get( path : "${contextPath}/rest/student", | |
contentType : TEXT, | |
query : [max: max, pretty: true, index: index, since: since], | |
headers: [Accept: 'application/xml', "Accept-Encoding": 'gzip'] ) { resp, text -> | |
def xml = new XmlParser().parse(text) | |
count += xml[ns2.StudentRecord].size() | |
batchCount++ | |
xml[ns2.StudentRecord].each { s -> | |
def values =new LinkedHashMap().withDefault { "" } | |
values.id = s[ns2.stateStudentIdentifier].text() | |
values.name = s[ns2.lastName].text() | |
values.dob = s[ns2.birthDate].text() | |
values.diploma = s[ns2.studentAttributeNoDate][ns2.diplomaDate].text() | |
values.expectedGrad = s[ns2.studentAttributeNoDate][ns2.expectedGraduationDate].text() | |
values.each { k,v -> print "$k: $v " } | |
println "" | |
} | |
index += max | |
} | |
} | |
def elapsed = (System.currentTimeMillis() - start) / 1000 | |
println "$count records in $batchCount batches in $elapsed seconds (${count / elapsed} records/second)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment