Skip to content

Commit

Permalink
feat: Add go sdk examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedhossamdev committed Sep 11, 2024
1 parent 30741c6 commit 231fd0d
Show file tree
Hide file tree
Showing 26 changed files with 854 additions and 25 deletions.
25 changes: 0 additions & 25 deletions examples/agency.go

This file was deleted.

1 change: 1 addition & 0 deletions examples/agency/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
// Initialize the OneBusAway client with the API key
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()
Expand Down
37 changes: 37 additions & 0 deletions examples/arrival-and-departure-for-stop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"fmt"
"log"
"time"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {
// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

// Define the query parameters
stopID := "1_75403"
tripID := "1_604670535"
serviceDate := time.Unix(1810918000, 0) // Convert Unix timestamp to time.Time

// Retrieve arrival and departure information
response, err := client.ArrivalAndDeparture.Get(ctx, stopID, onebusaway.ArrivalAndDepartureGetParams{
TripID: onebusaway.F(tripID),
ServiceDate: onebusaway.F(serviceDate.Unix()),
})
if err != nil {
log.Fatalf("Error retrieving arrival and departure: %v", err)
}

fmt.Printf("%+v\n", response)
}
45 changes: 45 additions & 0 deletions examples/arrivals-and-departures-for-stop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"fmt"
"log"
"time"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {
// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

// Define the query parameters
stopID := "1_75403" // Replace with actual stop ID

minutesBefore := int64(5) // include vehicles having arrived or departed in the previous n minutes (default=5)
minutesAfter := int64(35) // include vehicles arriving or departing in the next n minutes (default=35)

// Retrieve arrival and departure information
response, err := client.ArrivalAndDeparture.List(ctx, stopID, onebusaway.ArrivalAndDepartureListParams{
MinutesAfter: onebusaway.F(minutesAfter),
MinutesBefore: onebusaway.F(minutesBefore),
})
if err != nil {
log.Fatalf("Error retrieving arrival and departure: %v", err)
}

for _, ad := range response.Data.Entry.ArrivalsAndDepartures {
fmt.Printf("Route: %s\n", ad.RouteShortName)
fmt.Printf("Trip Headsign: %s\n", ad.TripHeadsign)
fmt.Printf("Predicted Arrival Time: %s\n", time.Unix(ad.PredictedArrivalTime/1000, 0))
fmt.Printf("Scheduled Arrival Time: %s\n", time.Unix(ad.ScheduledArrivalTime/1000, 0))
fmt.Printf("Vehicle ID: %s\n", ad.VehicleID)
fmt.Println()
}
}
31 changes: 31 additions & 0 deletions examples/block/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Initialize the OneBusAway client with the API key
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

blockID := "1_7331695" // Replace with actual block ID

block, err := client.Block.Get(ctx, blockID)

if err != nil {
log.Fatalf("Error fetching block: %v", err)
}

fmt.Print(block.Data.JSON.RawJSON())
}
29 changes: 29 additions & 0 deletions examples/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

config, err := client.Config.Get(ctx)

if err != nil {
log.Fatalf("Error getting config: %v", err)
}

fmt.Println(config)
}
29 changes: 29 additions & 0 deletions examples/current-time/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

currentTime, err := client.CurrentTime.Get(ctx)

if err != nil {
log.Fatalf("Error fetching current time: %v", err)
}

fmt.Println(currentTime.Data.JSON.RawJSON())
}
35 changes: 35 additions & 0 deletions examples/report-problem-with-stop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

stopID := "1_100000"

reportProblemWithStop, err := client.ReportProblemWithStop.Get(ctx, stopID, onebusaway.ReportProblemWithStopGetParams{
UserComment: onebusaway.F("This is a test report"),
UserLat: onebusaway.F(47.6062),
UserLon: onebusaway.F(-122.3321),
})

if err != nil {
log.Fatalf("Error reporting problem with stop: %v", err)
}

fmt.Println(reportProblemWithStop)
}
42 changes: 42 additions & 0 deletions examples/report-problem-with-trip/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"fmt"
"log"
"time"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

// Define the trip ID
tripID := "1_100000"

reportProblemWithTrip, err := client.ReportProblemWithTrip.Get(ctx, tripID, onebusaway.ReportProblemWithTripGetParams{
UserComment: onebusaway.F("This is a test report"),
ServiceDate: onebusaway.F(time.Now().Unix()),
VehicleID: onebusaway.F("1000"),
UserLat: onebusaway.F(47.6062),
UserLon: onebusaway.F(-122.3321),
UserOnVehicle: onebusaway.F(true),
UserVehicleNumber: onebusaway.F("1234"),
StopID: onebusaway.F("1_100000"),
})

if err != nil {
log.Fatalf("Error reporting problem with trip: %v", err)
}

fmt.Println(reportProblemWithTrip)
}
32 changes: 32 additions & 0 deletions examples/route/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

routeID := "1_100224"

route, err := client.Route.Get(ctx, routeID)

if err != nil {
log.Fatalf("Error fetching route: %v", err)
}

fmt.Print(route)

}
33 changes: 33 additions & 0 deletions examples/routes-for-agency/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

// Define the agency ID
agencyID := "1"

// Fetch the routes for the agency
routes, err := client.RoutesForAgency.List(ctx, agencyID)

if err != nil {
log.Fatalf("Error fetching routes: %v", err)
}

fmt.Print(routes.Data.JSON.RawJSON())
}
36 changes: 36 additions & 0 deletions examples/routes-for-location/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"fmt"
"log"

onebusaway "github.com/stainless-sdks/open-transit-go"
"github.com/stainless-sdks/open-transit-go/option"
)

func main() {

// Create a new instance of the OneBusAway SDK with the settings
client := onebusaway.NewClient(
option.WithAPIKey("TEST"),
option.WithBaseURL("https://api.pugetsound.onebusaway.org/"),
)

ctx := context.Background()

location := onebusaway.RoutesForLocationListParams{
Lat: onebusaway.F(47.6097),
Lon: onebusaway.F(-122.3331),
}

// Fetch the routes for the location
routes, err := client.RoutesForLocation.List(ctx, location)

if err != nil {
log.Fatalf("Error fetching routes: %v", err)
}

fmt.Print(routes.Data.JSON.RawJSON())

}
Loading

0 comments on commit 231fd0d

Please sign in to comment.