Chapter 2. Retrieving Seasons using Scala.
In our previous blog, where we made use of C# extensively, we retrieved league details and went further with a few more exercises. In this blog, we will take another direction by retrieving seasons’ data from the Football API.
Create a new file in the ‘SCALA’ folder of your project and name Seasons.
Select ‘Object’ on file type as shown below.
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.{HttpURLConnection, URL}
import scala.util.{Try, Using}
object Seasons extends App {
def sendGetRequest(apiUrl: String): Try[String] = {
val url = new URL(apiUrl)
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestMethod("GET")
connection.setRequestProperty("Accept", "application/json")
// Execute the request and handle the response
val result = Try {
val responseCode = connection.getResponseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
// Read the response
Using(new BufferedReader(new InputStreamReader(connection.getInputStream))) { reader =>
Iterator.continually(reader.readLine()).takeWhile(_ != null).mkString
}.getOrElse("")
} else {
s"Error: HTTP response code $responseCode"
}
}
// Ensure the connection is closed
connection.disconnect()
result
}
// Define the API endpoint
val apiUrl = "https://api.sportmonks.com/v3/football/seasons?api_token=API_Token" //replace with your API Token
// Send the request and print the response
sendGetRequest(apiUrl) match {
case scala.util.Success(data) => println(s"Response data: $data")
case scala.util.Failure(exception) => println(s"Error: ${exception.getMessage}")
}
}
{"data":[{"id":1,"sport_id":1,"league_id":66,"tie_breaker_rule_id":169,"name":"2013\/2014","finished":true,"pending":false,"is_current":false,"starting_at":"2013-08-17","ending_at":"2014-04-26","standings_recalculated_at":"2023-03-29 09:00:56","games_in_current_week":false},{"id":2,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2010\/2011","finished":true,"pending":false,"is_current":false,"starting_at":"2010-08-14","ending_at":"2011-05-22","standings_recalculated_at":"2023-05-24 08:28:07","games_in_current_week":false},{"id":3,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2013\/2014","finished":true,"pending":false,"is_current":false,"starting_at":"2013-08-17","ending_at":"2014-05-11","standings_recalculated_at":"2023-05-24 08:28:09","games_in_current_week":false},{"id":6,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2008\/2009","finished":true,"pending":false,"is_current":false,"starting_at":"2008-08-16","ending_at":"2009-05-24","standings_recalculated_at":"2023-11-30 13:38:48","games_in_current_week":false},{"id":7,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2012\/2013","finished":true,"pending":false,"is_current":false,"starting_at":"2012-08-18","ending_at":"2013-05-19","standings_recalculated_at":"2023-05-24 08:28:13","games_in_current_week":false},{"id":8,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2006\/2007","finished":true,"pending":false,"is_current":false,"starting_at":"2006-08-19","ending_at":"2007-05-13","standings_recalculated_at":"2023-05-24 08:28:14","games_in_current_week":false},{"id":9,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2011\/2012","finished":true,"pending":false,"is_current":false,"starting_at":"2011-08-13","ending_at":"2012-05-13","standings_recalculated_at":"2023-05-24 08:28:16","games_in_current_week":false},{"id":10,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2015\/2016","finished":true,"pending":false,"is_current":false,"starting_at":"2015-08-08","ending_at":"2016-05-17","standings_recalculated_at":"2023-05-24 08:28:17","games_in_current_week":false},{"id":11,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2009\/2010","finished":true,"pending":false,"is_current":false,"starting_at":"2009-08-15","ending_at":"2010-05-09","standings_recalculated_at":"2023-05-24 08:28:19","games_in_current_week":false},{"id":12,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2014\/2015","finished":true,"pending":false,"is_current":false,"starting_at":"2014-08-16","ending_at":"2015-05-24","standings_recalculated_at":"2023-05-24 08:28:20","games_in_current_week":false},{"id":13,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2016\/2017","finished":true,"pending":false,"is_current":false,"starting_at":"2016-08-13","ending_at":"2017-05-21","standings_recalculated_at":"2023-05-24 08:28:21","games_in_current_week":false},{"id":14,"sport_id":1,"league_id":8,"tie_breaker_rule_id":1526,"name":"2007\/2008","finished":true,"pending":false,"is_current":false,"starting_at":"2007-08-11","ending_at":"2008-05-11","standings_recalculated_at":"2023-05-24 08:28:22","games_in_current_week":false},{"id":15,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2005\/2006","finished":true,"pending":false,"is_current":false,"starting_at":"2005-08-05","ending_at":"2006-05-20","standings_recalculated_at":"2023-05-24 08:28:24","games_in_current_week":false},{"id":16,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2006\/2007","finished":true,"pending":false,"is_current":false,"starting_at":"2006-08-05","ending_at":"2007-05-28","standings_recalculated_at":"2023-05-24 08:28:26","games_in_current_week":false},{"id":17,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2007\/2008","finished":true,"pending":false,"is_current":false,"starting_at":"2007-08-11","ending_at":"2008-05-24","standings_recalculated_at":"2023-05-24 08:28:28","games_in_current_week":false},{"id":18,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2008\/2009","finished":true,"pending":false,"is_current":false,"starting_at":"2008-08-09","ending_at":"2009-05-25","standings_recalculated_at":"2024-03-04 14:58:46","games_in_current_week":false},{"id":19,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2009\/2010","finished":true,"pending":false,"is_current":false,"starting_at":"2009-08-07","ending_at":"2010-05-22","standings_recalculated_at":"2023-05-24 08:28:30","games_in_current_week":false},{"id":20,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2010\/2011","finished":true,"pending":false,"is_current":false,"starting_at":"2010-08-06","ending_at":"2011-05-30","standings_recalculated_at":"2023-05-24 08:28:32","games_in_current_week":false},{"id":21,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2011\/2012","finished":true,"pending":false,"is_current":false,"starting_at":"2011-08-05","ending_at":"2012-05-19","standings_recalculated_at":"2023-05-24 08:28:34","games_in_current_week":false},{"id":22,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2012\/2013","finished":true,"pending":false,"is_current":false,"starting_at":"2012-08-17","ending_at":"2013-05-27","standings_recalculated_at":"2023-05-24 08:29:22","games_in_current_week":false},{"id":23,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2013\/2014","finished":true,"pending":false,"is_current":false,"starting_at":"2013-08-03","ending_at":"2014-05-24","standings_recalculated_at":"2023-05-24 08:29:23","games_in_current_week":false},{"id":24,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2014\/2015","finished":true,"pending":false,"is_current":false,"starting_at":"2014-08-08","ending_at":"2015-05-25","standings_recalculated_at":"2023-05-24 08:29:25","games_in_current_week":false},{"id":25,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2015\/2016","finished":true,"pending":false,"is_current":false,"starting_at":"2015-08-07","ending_at":"2016-05-28","standings_recalculated_at":"2023-11-30 13:39:47","games_in_current_week":false},{"id":26,"sport_id":1,"league_id":9,"tie_breaker_rule_id":169,"name":"2016\/2017","finished":true,"pending":false,"is_current":false,"starting_at":"2016-08-05","ending_at":"2017-05-29","standings_recalculated_at":"2023-05-24 08:29:28","games_in_current_week":false},{"id":27,"sport_id":1,"league_id":12,"tie_breaker_rule_id":169,"name":"2005\/2006","finished":true,"pending":false,"is_current":false,"starting_at":"2005-08-05","ending_at":"2006-05-26","standings_recalculated_at":"2023-05-24 08:29:29","games_in_current_week":false}],"pagination":{"count":25,"per_page":25,"current_page":1,"next_page":"https:\/\/api.sportmonks.com\/v3\/football\/seasons?page=2","has_more":true},"subscription":[{"meta":{"trial_ends_at":"2024-10-16 14:50:45","ends_at":null,"current_timestamp":1730995538},"plans":[{"plan":"Enterprise plan (loyal)","sport":"Football","category":"Advanced"},{"plan":"Enterprise Plan","sport":"Cricket","category":"Standard"},{"plan":"Formula One","sport":"Formula One","category":"Standard"}],"add_ons":[{"add_on":"All-in News API","sport":"Football","category":"News"},{"add_on":"pressure index add-on","sport":"Football","category":"Default"},{"add_on":"Enterprise Plan Predictions","sport":"Football","category":"Predictions"},{"add_on":"xG Advanced","sport":"Football","category":"Expected"}],"widgets":[{"widget":"Sportmonks Widgets","sport":"Football"}]}],"rate_limit":{"resets_in_seconds":3600,"remaining":2999,"requested_entity":"Season"},"timezone":"UTC"}
Paste the piece of code above and run the Scala file as shown in the image below.
Explanation.
The URL and HttpURLConnection: Creates a connection to the specified API URL.
Setting request properties: Sets the request method to GET and adds a header to accept JSON responses.
Reading the response: Reads the response line by line and combines it into a single string.
Taking a look at the result, which displays a JSON response, you will find seasons with the data structure, which includes id, league_id, name, and starting_at among many others.