Last active
August 29, 2015 14:12
-
-
Save brunkb/2afd5af40b8c0bb03ec0 to your computer and use it in GitHub Desktop.
The meat of the example
This file contains 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
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