Skip to content

Instantly share code, notes, and snippets.

@brunkb
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunkb/2afd5af40b8c0bb03ec0 to your computer and use it in GitHub Desktop.
Save brunkb/2afd5af40b8c0bb03ec0 to your computer and use it in GitHub Desktop.
The meat of the example
class InventoryApiConnectionFactory {
// these properties would ordinarily be injected
def authenticationProvider = new AuthenticationProvider()
String username = "username"
String password = "password"
def logError(String msg) { println msg } // simulated logger
def withInventoryConnection(Closure c) {
def authenticatedUserToken = authenticationProvider.authenticateUser(username, password)
def connection = new InventorySystem()
def isOpen = connection.open(authenticatedUserToken)
try {
if(isOpen) {
c.call(connection)
}
} catch (any) {
logError("Something went wrong: ${any.message}".toString())
throw any
} finally {
connection.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment