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, "") password = config.getProperty(CONFIG_PWD, "")
timelineId = config.getProperty(CONFIG_TIMELINE, "") 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) 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 = [:] entryHashes = [:]
} }
@ -53,9 +64,15 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
authenticate() authenticate()
// load the timeline information // load the timeline information
timelineJSON = http.get( http.request(GET, JSON) {
path: "/ts_api/timelines/${username}/${timelineId}", uri.path = "timelines/${username}/${timelineId}"
contentType: JSON) { resp, json -> json }
response.'404' = { resp ->
throw new IOException("No timeline '${timelineId}' for user " +
"'${username}'") }
response.success = { resp, json -> timelineJSON = json }
}
timeline = new Timeline() timeline = new Timeline()
@ -68,7 +85,7 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
// load the timeline entries // load the timeline entries
entryListJSON = http.get( entryListJSON = http.get(
path: "/ts_api/entries/${username}/${timelineId}", path: "entries/${username}/${timelineId}",
contentType: JSON, contentType: JSON,
query: [ query: [
byDate: true, byDate: true,
@ -115,11 +132,11 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
// delete all entries that used to be present but are not any longer // delete all entries that used to be present but are not any longer
deletedEntries.each { hash, entryId -> deletedEntries.each { hash, entryId ->
http.request(DELETE) { 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 response.'404' = { resp -> entryHashes.remove(hash) }
entryHashes.remove(hash) response.success = { resp -> entryHashes.remove(hash) }
}
} }
// add all new entries // add all new entries
@ -131,32 +148,30 @@ public class JDBLabsWebTimelineSource extends TimelineSource {
timestamp: isoDateFormat.format(entry.timestamp) timestamp: isoDateFormat.format(entry.timestamp)
] ]
// TODO: error handling http.request(POST, JSON) {
http.post( uri.path = "entries/${username}/${timelineId}"
path: "/ts_api/entries/${username}/${timelineId}", requestContentType = JSON
contentType: JSON, body = entryBody
requestContentType: JSON,
body: entryBody) { resp, json -> response.success = { entryHashes.put(fullHash(entry), entry.id) }
entryHashes.put(fullHash(entry), entry)
json
} }
} }
} }
public boolean isAuthenticated() { public boolean isAuthenticated() { return false; }
// TODO
}
public void authenticate() { public void authenticate() throws AuthenticationException {
// TODO: error detection http.request(POST, JSON) { req ->
http.post( uri.path = '/ts_api/login'
path: '/ts_api/login', requestContentType = JSON
contentType: JSON, body = [ username: this.username,
requestContentType: JSON, password: this.password ]
body: [ username: this.username,
password: this.password ]) { resp, json -> json }
response.'401' = { resp ->
throw new AuthenticationException(
"Unable to connect to ${baseUri}: " +
"Invalid username/password combination.") }
}
} }
protected int fullHash(TimelineMarker tm) { protected int fullHash(TimelineMarker tm) {