Skip to content

Commit ce8b134

Browse files
feat: onebusaway-sdk-kotlin examples
1 parent 1a37662 commit ce8b134

23 files changed

+876
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
group = "org.onebusaway.example"
6+
version = "0.0.1-alpha.0"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
implementation(project(":onebusaway-sdk-kotlin"))
14+
testImplementation(platform("org.junit:junit-bom:5.9.1"))
15+
testImplementation("org.junit.jupiter:junit-jupiter")
16+
}
17+
18+
tasks.test {
19+
useJUnitPlatform()
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object AgenciesWithCoverage {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
// Define the required Params
22+
val params = AgenciesWithCoverageListParams.builder().build()
23+
24+
// Get the agencies with coverage
25+
val agencies: AgenciesWithCoverageListResponse = client.agenciesWithCoverage().list(params)
26+
27+
for (agency in agencies.data().list()) {
28+
println(agency)
29+
}
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object Agency {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
22+
println(args)
23+
// Define the agency ID
24+
val agencyId = "1"
25+
26+
// Define the parameters for the agency retrieval request
27+
val params = AgencyRetrieveParams.builder().agencyId(agencyId).build()
28+
29+
// Retrieve the agency information
30+
val agency: AgencyRetrieveResponse = client.agency().retrieve(params)
31+
32+
println(agency)
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object ArrivalAndDepartureForStop {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
// Define the stop ID and trip ID
22+
val stopId = "1_75403"
23+
val tripId = "1_604670535"
24+
25+
// Define the service date directly as a Unix timestamp (in seconds)
26+
val serviceDate: Long = 1810918000 // Example timestamp
27+
28+
// Create parameters for arrival and departure request
29+
val params = ArrivalAndDepartureRetrieveParams.builder()
30+
.stopId(stopId)
31+
.tripId(tripId)
32+
.serviceDate(serviceDate) // Use the Unix timestamp directly
33+
.build()
34+
35+
// Retrieve arrival and departure information
36+
val arrivalAndDepartureForStop: ArrivalAndDepartureRetrieveResponse = client.arrivalAndDeparture().retrieve(params)
37+
println(arrivalAndDepartureForStop)
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object ArrivalsAndDeparturesForStop {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
// Define the stop ID
22+
val stopId = "1_75403"
23+
val minutesBefore: Long = 5
24+
val minutesAfter: Long = 35
25+
26+
27+
// Define the parameters for the arrival and departure list request
28+
val params = ArrivalAndDepartureListParams.builder()
29+
.stopId(stopId)
30+
.minutesBefore(minutesBefore)
31+
.minutesAfter(minutesAfter)
32+
.build()
33+
34+
// Retrieve arrival and departure information
35+
val arrivalsAndDeparturesForStop: ArrivalAndDepartureListResponse = client.arrivalAndDeparture().list(params)
36+
37+
for (arrivalAndDeparture in arrivalsAndDeparturesForStop.data().entry().arrivalsAndDepartures()) {
38+
println(arrivalAndDeparture)
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.errors.OnebusawaySdkServiceException
6+
import org.onebusaway.models.*
7+
8+
object Block {
9+
10+
// Retrieve constants from environment variables or fallback to default values
11+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
12+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
13+
14+
// Initialize the Onebusaway SDK client
15+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
16+
.apiKey(API_KEY)
17+
.baseUrl(BASE_URL)
18+
.build()
19+
20+
@JvmStatic
21+
fun main(args: Array<String>) {
22+
// Define the block ID
23+
val blockId = "1_7331695"
24+
25+
try {
26+
// Define the parameters for the block retrieval request
27+
val params = BlockRetrieveParams.builder().blockId(blockId).build()
28+
29+
// Retrieve the block information
30+
val block: BlockRetrieveResponse = client.block().retrieve(params)
31+
32+
println(block)
33+
34+
} catch (e: OnebusawaySdkServiceException) {
35+
// Handle the SDK-specific service exception
36+
println("Error occurred: ${e.message}")
37+
println("Status Code: ${e.statusCode()}")
38+
} catch (e: Exception) {
39+
e.printStackTrace()
40+
}
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object CurrentTime {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
// Define the parameters for the current time retrieval request
22+
val params = CurrentTimeRetrieveParams.builder().build()
23+
24+
// Retrieve the current time information
25+
val currentTime: CurrentTimeRetrieveResponse = client.currentTime().retrieve(params)
26+
27+
println(currentTime)
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.errors.OnebusawaySdkServiceException
6+
import org.onebusaway.models.*
7+
8+
object Route {
9+
10+
// Retrieve constants from environment variables or fallback to default values
11+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
12+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
13+
14+
// Initialize the Onebusaway SDK client
15+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
16+
.apiKey(API_KEY)
17+
.baseUrl(BASE_URL)
18+
.build()
19+
20+
@JvmStatic
21+
fun main(args: Array<String>) {
22+
// Define the route ID
23+
val routeId = "1_100224"
24+
25+
try {
26+
val params = RouteRetrieveParams.builder().routeId(routeId).build()
27+
28+
val route: RouteRetrieveResponse = client.route().retrieve(params)
29+
30+
println(route)
31+
32+
} catch (e: OnebusawaySdkServiceException) {
33+
// Handle the SDK-specific service exception
34+
System.err.println("Error occurred: ${e.message}")
35+
System.err.println("Status Code: ${e.statusCode()}")
36+
}
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.errors.OnebusawaySdkServiceException
6+
import org.onebusaway.models.*
7+
8+
object RouteForAgency {
9+
10+
// Retrieve constants from environment variables or fallback to default values
11+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
12+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
13+
14+
// Initialize the Onebusaway SDK client
15+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
16+
.apiKey(API_KEY)
17+
.baseUrl(BASE_URL)
18+
.build()
19+
20+
@JvmStatic
21+
fun main(args: Array<String>) {
22+
try {
23+
// Define the agency ID
24+
val agencyId = "1"
25+
26+
// Create the parameters for the routes for agency list request
27+
val params = RoutesForAgencyListParams.builder()
28+
.agencyId(agencyId)
29+
.build()
30+
31+
// Retrieve the routes for the agency
32+
val routesForAgency: RoutesForAgencyListResponse = client.routesForAgency().list(params)
33+
34+
for (route in routesForAgency.data().list()) {
35+
println(route)
36+
}
37+
} catch (e: OnebusawaySdkServiceException) {
38+
// Handle the SDK-specific service exception
39+
System.err.println("Error occurred: ${e.message}")
40+
System.err.println("Status Code: ${e.statusCode()}")
41+
} catch (e: Exception) {
42+
System.err.println("Error occurred: ${e.message}")
43+
}
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.onebusaway.example
2+
3+
import org.onebusaway.client.OnebusawaySdkClient
4+
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
5+
import org.onebusaway.models.*
6+
7+
object RoutesForLocation {
8+
9+
// Retrieve constants from environment variables or fallback to default values
10+
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
11+
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"
12+
13+
// Initialize the Onebusaway SDK client
14+
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
15+
.apiKey(API_KEY)
16+
.baseUrl(BASE_URL)
17+
.build()
18+
19+
@JvmStatic
20+
fun main(args: Array<String>) {
21+
try {
22+
// Define the location parameters
23+
val lat = 47.653435
24+
val lon = -122.305641
25+
val radius = 1000.0
26+
27+
// Create the parameters for the routes for location request
28+
val params = RoutesForLocationListParams.builder()
29+
.lat(lat)
30+
.lon(lon)
31+
.radius(radius)
32+
.build()
33+
34+
// Retrieve the routes for location
35+
val routesForLocation: RoutesForLocationListResponse = client.routesForLocation().list(params)
36+
37+
for (route in routesForLocation.data().list()) {
38+
println(route)
39+
}
40+
} catch (e: Exception) {
41+
System.err.println("Error occurred: ${e.message}")
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)