Skip to content

Commit

Permalink
feat: Java examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedhossamdev committed Oct 4, 2024
1 parent d13daae commit e60980e
Show file tree
Hide file tree
Showing 25 changed files with 855 additions and 0 deletions.
Empty file added For
Empty file.
Empty file added Get
Empty file.
Empty file added Run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.example;

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

public class AgenciesWithCoverage {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();

public static void main(String[] args) {

// Define the required Params
AgenciesWithCoverageListParams params = AgenciesWithCoverageListParams.builder().build();

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

for (AgenciesWithCoverageListResponse.Data.List agency : agencies.data().list()) {
System.out.println(agency);
}
}
}
33 changes: 33 additions & 0 deletions onebusaway-sdk-java-example/src/main/java/org/example/Agency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.example;

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

public class Agency {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

// Define the agency ID
String agencyId = "1";

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

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

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

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

public class ArrivalAndDepartureForStop {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();

public static void main(String[] args) {

// Define the stop ID and trip ID
String stopId = "1_75403";
String tripId = "1_604670535";

// Define the service date directly as a Unix timestamp (in seconds)
long serviceDate = 1810918000; // Example timestamp

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

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

import org.onebusaway.client.OnebusawaySdkClient;
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
import org.onebusaway.models.*;
public class ArrivalsAndDeparturesForStop {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

// Define the stop ID
String stopId = "1_75403";
int minutesBefore = 5;
int minutesAfter = 35;

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

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

for (ArrivalAndDepartureListResponse.Data.Entry.ArrivalsAndDeparture arrivalAndDeparture : arrivalsAndDeparturesForStop.data().entry().arrivalsAndDepartures()) {
System.out.println(arrivalAndDeparture);
}
}

}
43 changes: 43 additions & 0 deletions onebusaway-sdk-java-example/src/main/java/org/example/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.example;

import org.onebusaway.client.OnebusawaySdkClient;
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
import org.onebusaway.errors.OnebusawaySdkServiceException;
import org.onebusaway.models.*;
public class Block {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

// Define the block ID
String blockId = "1_7331695";

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

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

System.out.println(block);

} catch (OnebusawaySdkServiceException e) {

// Handle the SDK-specific service exception
System.err.println("Error occurred: " + e.getMessage());
System.err.println("Status Code: " + e.statusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.example;

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

public class CurrentTime {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

// Define the parameters for the current time retrieval request
CurrentTimeRetrieveParams params = CurrentTimeRetrieveParams.builder().build();

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

System.out.println(currentTime);
}


}
39 changes: 39 additions & 0 deletions onebusaway-sdk-java-example/src/main/java/org/example/Route.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.example;

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


public class Route {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

// Define the route ID
String routeId = "1_100224";

try {

RouteRetrieveParams params = org.onebusaway.models.RouteRetrieveParams.builder().routeId(routeId).build();

RouteRetrieveResponse route = client.route().retrieve(params);

System.out.println(route);

} catch (org.onebusaway.errors.OnebusawaySdkServiceException e) {
System.err.println("Error occurred: " + e.getMessage());
System.err.println("Status Code: " + e.statusCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.example;

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

public class RouteForAgency {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

try {

// Define the agency ID
String agencyId = "1";

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

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

for (RoutesForAgencyListResponse.Data.List route : routesForAgency.data().list()) {
System.out.println(route);
}
}
catch (OnebusawaySdkServiceException e) {
System.err.println("Error occurred: " + e.getMessage());
System.err.println("Status Code: " + e.statusCode());
}
catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.example;

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

public class RoutesForLocation {

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

// Initialize the Onebusaway SDK client
static final OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
.apiKey(API_KEY)
.baseUrl(BASE_URL)
.build();


public static void main(String[] args) {

try {
// Define the location parameters
double lat = 47.653435;
double lon = -122.305641;
double radius = 1000.0;

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

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

for (RoutesForLocationListResponse.Data.List route : routesForLocation.data().list()) {
System.out.println(route);
}
}
catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
}
}

}
Loading

0 comments on commit e60980e

Please sign in to comment.