Skip to content

Commit

Permalink
feat: onebusaway-sdk-kotlin examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedhossamdev committed Oct 7, 2024
1 parent 1a37662 commit ce8b134
Show file tree
Hide file tree
Showing 23 changed files with 876 additions and 0 deletions.
20 changes: 20 additions & 0 deletions onebusaway-sdk-kotlin-example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
kotlin("jvm")
}

group = "org.onebusaway.example"
version = "0.0.1-alpha.0"

repositories {
mavenCentral()
}

dependencies {
implementation(project(":onebusaway-sdk-kotlin"))
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object AgenciesWithCoverage {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the required Params
val params = AgenciesWithCoverageListParams.builder().build()

// Get the agencies with coverage
val agencies: AgenciesWithCoverageListResponse = client.agenciesWithCoverage().list(params)

for (agency in agencies.data().list()) {
println(agency)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object Agency {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {

println(args)
// Define the agency ID
val agencyId = "1"

// Define the parameters for the agency retrieval request
val params = AgencyRetrieveParams.builder().agencyId(agencyId).build()

// Retrieve the agency information
val agency: AgencyRetrieveResponse = client.agency().retrieve(params)

println(agency)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object ArrivalAndDepartureForStop {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the stop ID and trip ID
val stopId = "1_75403"
val tripId = "1_604670535"

// Define the service date directly as a Unix timestamp (in seconds)
val serviceDate: Long = 1810918000 // Example timestamp

// Create parameters for arrival and departure request
val params = ArrivalAndDepartureRetrieveParams.builder()
.stopId(stopId)
.tripId(tripId)
.serviceDate(serviceDate) // Use the Unix timestamp directly
.build()

// Retrieve arrival and departure information
val arrivalAndDepartureForStop: ArrivalAndDepartureRetrieveResponse = client.arrivalAndDeparture().retrieve(params)
println(arrivalAndDepartureForStop)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object ArrivalsAndDeparturesForStop {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the stop ID
val stopId = "1_75403"
val minutesBefore: Long = 5
val minutesAfter: Long = 35


// Define the parameters for the arrival and departure list request
val params = ArrivalAndDepartureListParams.builder()
.stopId(stopId)
.minutesBefore(minutesBefore)
.minutesAfter(minutesAfter)
.build()

// Retrieve arrival and departure information
val arrivalsAndDeparturesForStop: ArrivalAndDepartureListResponse = client.arrivalAndDeparture().list(params)

for (arrivalAndDeparture in arrivalsAndDeparturesForStop.data().entry().arrivalsAndDepartures()) {
println(arrivalAndDeparture)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.errors.OnebusawaySdkServiceException
import org.onebusaway.models.*

object Block {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the block ID
val blockId = "1_7331695"

try {
// Define the parameters for the block retrieval request
val params = BlockRetrieveParams.builder().blockId(blockId).build()

// Retrieve the block information
val block: BlockRetrieveResponse = client.block().retrieve(params)

println(block)

} catch (e: OnebusawaySdkServiceException) {
// Handle the SDK-specific service exception
println("Error occurred: ${e.message}")
println("Status Code: ${e.statusCode()}")
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object CurrentTime {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the parameters for the current time retrieval request
val params = CurrentTimeRetrieveParams.builder().build()

// Retrieve the current time information
val currentTime: CurrentTimeRetrieveResponse = client.currentTime().retrieve(params)

println(currentTime)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.errors.OnebusawaySdkServiceException
import org.onebusaway.models.*

object Route {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
// Define the route ID
val routeId = "1_100224"

try {
val params = RouteRetrieveParams.builder().routeId(routeId).build()

val route: RouteRetrieveResponse = client.route().retrieve(params)

println(route)

} catch (e: OnebusawaySdkServiceException) {
// Handle the SDK-specific service exception
System.err.println("Error occurred: ${e.message}")
System.err.println("Status Code: ${e.statusCode()}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.errors.OnebusawaySdkServiceException
import org.onebusaway.models.*

object RouteForAgency {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
try {
// Define the agency ID
val agencyId = "1"

// Create the parameters for the routes for agency list request
val params = RoutesForAgencyListParams.builder()
.agencyId(agencyId)
.build()

// Retrieve the routes for the agency
val routesForAgency: RoutesForAgencyListResponse = client.routesForAgency().list(params)

for (route in routesForAgency.data().list()) {
println(route)
}
} catch (e: OnebusawaySdkServiceException) {
// Handle the SDK-specific service exception
System.err.println("Error occurred: ${e.message}")
System.err.println("Status Code: ${e.statusCode()}")
} catch (e: Exception) {
System.err.println("Error occurred: ${e.message}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.onebusaway.example

import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient
import org.onebusaway.models.*

object RoutesForLocation {

// Retrieve constants from environment variables or fallback to default values
private val API_KEY: String = System.getenv("ONEBUSAWAY_API_KEY") ?: "TEST"
private val BASE_URL: String = System.getenv("ONEBUSAWAY_BASE_URL") ?: "https://api.pugetsound.onebusaway.org"

// Initialize the Onebusaway SDK client
private val client: OnebusawaySdkClient = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build()

@JvmStatic
fun main(args: Array<String>) {
try {
// Define the location parameters
val lat = 47.653435
val lon = -122.305641
val radius = 1000.0

// Create the parameters for the routes for location request
val params = RoutesForLocationListParams.builder()
.lat(lat)
.lon(lon)
.radius(radius)
.build()

// Retrieve the routes for location
val routesForLocation: RoutesForLocationListResponse = client.routesForLocation().list(params)

for (route in routesForLocation.data().list()) {
println(route)
}
} catch (e: Exception) {
System.err.println("Error occurred: ${e.message}")
}
}
}
Loading

0 comments on commit ce8b134

Please sign in to comment.