Last active
August 29, 2015 14:26
-
-
Save kailuowang/c73a70c9194e662163e8 to your computer and use it in GitHub Desktop.
Functional Asyn 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
def createOrUpdateCard( profileId: ProfileId, contextName: ContextName): FutureEither[CardId] = { | |
lazy val nowF = getAndValidateGoogleNowCredential(profileId) | |
lazy val contentF = getContentFromStationService(profileId, contextName) | |
lazy val contextF = ensureContextExistsOnGoogle(contextName) | |
def createCard = { | |
for { | |
now <- nowF | |
content <- contentF | |
context <- contextF | |
toCreate = new Card().setContent(content) | |
created <- BlockingIO.futureEither(createCardOnGoogle(toCreate, now)) | |
} yield created.getCardId | |
} | |
def updateCard = { | |
val existingCard = | |
for { | |
now <- nowF | |
context <- contextF | |
card <- findCardOnGoogle(profileId, context))) | |
} yield card | |
for { | |
now <- nowF | |
card <- existingCard | |
content <- contentF | |
toUpdate = card.setContent(content) | |
updated <- BlockingIO.futureEither(updateCard(toUpdate, now)) | |
updatedId <- updated.getCardId | |
} yield updatedId | |
} | |
updateCard orElse createCard | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment