Skip to content

Commit

Permalink
fix RESTClient examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jgritman committed Feb 4, 2014
1 parent 436f19b commit 8d9004a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/site/apt/doc/rest.apt
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ RESTClient
JSON POST requests.) For this reason, a <<<requestContentType>>> parameter must
be specified in order to identify how the request body should be serialized.

Also note that we're requesting <<<update.xml>>>, not <<<update.json>>>. Since
we never set a default content-type on the RESTClient instance or pass a
Since we never set a default content-type on the RESTClient instance or pass a
<<<contentType>>> argument in this request, RESTClient will put <<<Accept: */*>>>
in the request header, and parse the response based on whatever is given in
the response content-type header. So because Twitter correctly identifies its
response as <<<application/xml>>>, it will automatically be parsed by the
default {{{../apidocs/groovyx/net/http/ParserRegistry.html#parseXML(org.apache.http.HttpResponse)}XML parser}}.
response as <<<application/json>>>, it will automatically be parsed by the
default {{{../apidocs/groovyx/net/http/ParserRegistry.html#parseJSON(org.apache.http.HttpResponse)}JSON parser}}.


** Now <<<DELETE>>> that post
Expand Down
50 changes: 26 additions & 24 deletions src/site/examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,18 @@ def resp = weather.get( path: 'weather', query:[q: 'London', mode: 'xml'])
END SNIPPET: xml4

START SNIPPET: json1
import groovyx.net.http.*
@Grab(group='org.codehaus.groovy.modules.http-builder',
module='http-builder', version='0.5.2' )
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

def http = new HTTPBuilder( 'http://twitter.com/statuses/' )
import groovyx.net.http.HTTPBuilder

http.get( path: 'user_timeline.json',
query: [id:'httpbuilder', count:5] ) { resp, json ->
def http = new HTTPBuilder( 'http://api.openweathermap.org/data/2.5/' )

http.get( path: 'weather', query: [q: 'London'] ) { resp, json ->

println resp.status

json.each { // iterate over JSON 'status' object in the response:
println it.created_at
println ' ' + it.text
}
println "It is currently ${json.weather.main[0]} in London."
println "The temperature is ${json.main.temp} degrees Kelvin"
}
END SNIPPET: json1

Expand Down Expand Up @@ -292,11 +289,15 @@ START SNIPPET: json3
END SNIPPET: json3

START SNIPPET: rest1
@GrabResolver(name='codehaus-snapshots', root=' http://snapshots.repository.codehaus.org//')
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7-SNAPSHOT')
@Grab(group='oauth.signpost', module='signpost-core', version='1.2.1.2')
@Grab(group='oauth.signpost', module='signpost-commonshttp4', version='1.2.1.2')

import groovyx.net.http.RESTClient
import groovy.util.slurpersupport.GPathResult
import static groovyx.net.http.ContentType.URLENC
import static groovyx.net.http.ContentType.*

twitter = new RESTClient( 'https://twitter.com/statuses/' )
def twitter = new RESTClient( 'https://api.twitter.com/1.1/statuses/' )
// twitter auth omitted

try { // expect an exception from a 404 response:
Expand All @@ -306,29 +307,30 @@ try { // expect an exception from a 404 response:
// The exception is used for flow control but has access to the response as well:
catch( ex ) { assert ex.response.status == 404 }

assert twitter.head( path : 'public_timeline.json' ).status == 200
assert twitter.head( path : 'home_timeline.json' ).status == 200
END SNIPPET: rest1

START SNIPPET: rest2
def resp = twitter.get( path : 'friends_timeline.json' )
def resp = twitter.get( path : 'home_timeline.json' )
assert resp.status == 200
assert resp.contentType == JSON.toString()
assert ( resp.data instanceof net.sf.json.JSON )
assert ( resp.data instanceof List )
assert resp.data.status.size() > 0
END SNIPPET: rest2

START SNIPPET: rest3
def msg = "I'm using HTTPBuilder's RESTClient on ${new Date()}"

resp = twitter.post( path : 'update.xml',
body : [ status:msg, source:'httpbuilder' ],
requestContentType : URLENC )
def resp = twitter.post(
path : 'update.json',
body : [ status:msg, source:'httpbuilder' ],
requestContentType : URLENC )

assert resp.status == 200
assert ( resp.data instanceof GPathResult ) // parsed using XmlSlurper
assert resp.headers.Status
assert resp.data.text == msg
assert resp.data.user.screen_name == userName
def postID = resp.data.id.toInteger()

def postID = resp.data.id
END SNIPPET: rest3

START SNIPPET: rest4
Expand Down Expand Up @@ -630,7 +632,7 @@ START SNIPPET: download1
<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>0.5.2</version>
<version>0.7</version>
</dependency>
END SNIPPET: download1

Expand All @@ -647,7 +649,7 @@ START SNIPPET: download2
END SNIPPET: download2

START SNIPPET: download3
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
// ....
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/groovyx/net/http/RESTClientTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class RESTClientTest {
// test the exception class:
catch( ex ) { assert ex.response.status == 404 }

// assert twitter.head( path : 'public_timeline.json' ).status == 200
assert twitter.head( path : 'home_timeline.json' ).status == 200
}

@Test public void testGet() {
Expand Down

0 comments on commit 8d9004a

Please sign in to comment.