object IO {
def getHtmlFromWebsiteViaHttp(link: String, apiKey: String = ""): String = {
Http(link)
.param("access_token", apiKey)
.asString
.body
}
}
class SongService {
private def retrieveSongId(songName: String): Option[JsValue] = {
val formattedSongName = songName.replace(" ", "%20")
val searchLink = "https://api.genius.com/search?q=" + formattedSongName
//impure call
val geniusStringResponse = IO.getHtmlFromWebsiteViaHttp(searchLink, apiKey)
//Extra processing on geniusStringResponse
}
}
My current design is I would have a service class which is responsible for getting some information via an external API. Now I understand that it's impossible to have 100% pure functions.
My question: What is the best way to handle situations where you need to connect to an external API in Scala/FP?. The aim is to have the most adequate 'functional programming style' by minimising impure functions
Currently, I am encapsulating all API calls in IO object. Is this suitable enough? I see examples of monads for situations. Should I incorporate a monad style in this case?