Version 1.2: Refactored JDBLabsWebTimeline, cleaned up libs.

* Removed superfluous ``signpost`` library used by ``HTTPBuilder`` for OAuth
  support.
* Refactored JDBLabsWebTimeline to throw appropriate exceptions when problems
  occur while communicating with the server.
This commit is contained in:
Jonathan Bernard 2011-06-28 18:14:43 -05:00
parent 3ca6909b95
commit b56a708a09
6 changed files with 53 additions and 29 deletions

9
doc/issues/0000tn5.rst Normal file
View File

@ -0,0 +1,9 @@
Add unit tests.
===============
----
========= ==========
Created: 2011-06-28
Resolved: YYYY-MM-DD
========= ==========

Binary file not shown.

Binary file not shown.

View File

@ -37,9 +37,20 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
password = config.getProperty(CONFIG_PWD, "")
timelineId = config.getProperty(CONFIG_TIMELINE, "")
baseUri = new URI(uri.scheme + "://" + uri.authority)
baseUri = new URI(uri.scheme + "://" + uri.authority + "/ts_api/")
http = new HTTPBuilder(baseUri)
// set some default error handlers
http.handler.'500' = { resp, json ->
throw new IOException("The web timeline reported an internal " +
"error: ${json.error ?: 'no details available'}") }
http.handler.failure = {resp, json ->
throw new IOException("Unable to complete the operation: error " +
"communicating to the web timeline.\n" +
"${resp.statusLine}: ${json}") }
// init our hash of known entries
entryHashes = [:]
}
@ -53,9 +64,15 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
authenticate()
// load the timeline information
timelineJSON = http.get(
path: "/ts_api/timelines/${username}/${timelineId}",
contentType: JSON) { resp, json -> json }
http.request(GET, JSON) {
uri.path = "timelines/${username}/${timelineId}"
response.'404' = { resp ->
throw new IOException("No timeline '${timelineId}' for user " +
"'${username}'") }
response.success = { resp, json -> timelineJSON = json }
}
timeline = new Timeline()
@ -68,7 +85,7 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
// load the timeline entries
entryListJSON = http.get(
path: "/ts_api/entries/${username}/${timelineId}",
path: "entries/${username}/${timelineId}",
contentType: JSON,
query: [
byDate: true,
@ -115,11 +132,11 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
// delete all entries that used to be present but are not any longer
deletedEntries.each { hash, entryId ->
http.request(DELETE) {
uri.path = "/ts_api/entries/${username}/${timelineId}/${entryId}"
}
uri.path = "entries/${username}/${timelineId}/${entryId}"
// TODO: error handling, make sure this only happens on success
entryHashes.remove(hash)
response.'404' = { resp -> entryHashes.remove(hash) }
response.success = { resp -> entryHashes.remove(hash) }
}
}
// add all new entries
@ -131,32 +148,30 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
timestamp: isoDateFormat.format(entry.timestamp)
]
// TODO: error handling
http.post(
path: "/ts_api/entries/${username}/${timelineId}",
contentType: JSON,
requestContentType: JSON,
body: entryBody) { resp, json ->
entryHashes.put(fullHash(entry), entry)
json
http.request(POST, JSON) {
uri.path = "entries/${username}/${timelineId}"
requestContentType = JSON
body = entryBody
response.success = { entryHashes.put(fullHash(entry), entry.id) }
}
}
}
public boolean isAuthenticated() {
// TODO
}
public boolean isAuthenticated() { return false; }
public void authenticate() {
// TODO: error detection
http.post(
path: '/ts_api/login',
contentType: JSON,
requestContentType: JSON,
body: [ username: this.username,
password: this.password ]) { resp, json -> json }
public void authenticate() throws AuthenticationException {
http.request(POST, JSON) { req ->
uri.path = '/ts_api/login'
requestContentType = JSON
body = [ username: this.username,
password: this.password ]
response.'401' = { resp ->
throw new AuthenticationException(
"Unable to connect to ${baseUri}: " +
"Invalid username/password combination.") }
}
}
protected int fullHash(TimelineMarker tm) {