Skip to content

Commit

Permalink
Add example 3 and improve docs (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox authored Aug 2, 2023
1 parent 2fc7cde commit 495cf24
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ FlightDuration: 3h55m0s}
```
go run ./examples/example1/main.go
go run ./examples/example2/main.go
go run ./examples/example3/main.go
```

## Bug / Feature / Suggestion
Expand Down
13 changes: 9 additions & 4 deletions examples/example1/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This example gets best offers in the provided date range and print the cheapest one.
package main

import (
Expand All @@ -10,10 +11,13 @@ import (
"golang.org/x/text/language"
)

// Get best offers in the provided date range and print the cheapest one
func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCity, dstCity string, lang language.Tag) {
func getCheapesOffer(
rangeStartDate, rangeEndDate time.Time,
tripLength int,
srcCity, dstCity string,
lang language.Tag,
) {
session := flights.New()
var bestOffer flights.Offer

args := flights.Args{
Adults: 1,
Expand All @@ -38,6 +42,7 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
log.Fatal(err)
}

var bestOffer flights.Offer
for _, o := range offers {
if bestOffer.Price == 0 || o.Price < bestOffer.Price {
bestOffer = o
Expand All @@ -62,7 +67,7 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
}

func main() {
getBestOffer(
getCheapesOffer(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
2,
Expand Down
29 changes: 18 additions & 11 deletions examples/example2/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
// This example gets PriceGraph offers and prints only those whose
// price is cheaper than the low price of the offer
package main

import (
"fmt"
"log"
"os"
"time"

"github.com/krisukox/google-flights-api/flights"
"golang.org/x/text/currency"
"golang.org/x/text/language"
)

// Get offers in the provided date range and print only those which price is cheaper than low price of the offer
func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities []string, lang language.Tag) {
func getCheapOffers(
rangeStartDate, rangeEndDate time.Time,
tripLength int,
srcCities, dstCities []string,
lang language.Tag,
) {
logger := log.New(os.Stdout, "", 0)
session := flights.New()

args := flights.Args{
Expand All @@ -34,7 +42,7 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
},
)
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}

for _, priceGraphOffer := range priceGraphOffers {
Expand All @@ -48,11 +56,10 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
},
)
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}

var bestOffer flights.FullOffer

for _, o := range offers {
if bestOffer.Price == 0 || o.Price < bestOffer.Price {
bestOffer = o
Expand All @@ -69,10 +76,10 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
},
)
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
if priceRange == nil {
log.Fatal("missing priceRange")
logger.Fatal("missing priceRange")
}

if bestOffer.Price < priceRange.Low {
Expand All @@ -86,11 +93,11 @@ func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcC
},
)
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
fmt.Println(url)
logger.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
logger.Printf("price %d\n", int(bestOffer.Price))
logger.Println(url)
}
}
}
Expand Down
127 changes: 127 additions & 0 deletions examples/example3/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// This example iterates over PriceGraph offers concurrently and prints only
// those whose price is cheaper than the low price of the offer.
// This example is the same as Example 2, but it sends requests concurrently.
package main

import (
"fmt"
"log"
"os"
"sync"
"time"

"github.com/krisukox/google-flights-api/flights"
"golang.org/x/text/currency"
"golang.org/x/text/language"
)

func getCheapOffersConcurrent(
rangeStartDate, rangeEndDate time.Time,
tripLength int,
srcCities, dstCities []string,
lang language.Tag,
) {
logger := log.New(os.Stdout, "", 0)
session := flights.New()

args := flights.Args{
Adults: 1,
Currency: currency.USD,
Stops: flights.AnyStops,
Class: flights.Economy,
TripType: flights.RoundTrip,
Lang: lang,
}

priceGraphOffers, err := session.GetPriceGraph(
flights.PriceGraphArgs{
RangeStartDate: rangeStartDate,
RangeEndDate: rangeEndDate,
TripLength: tripLength,
SrcCities: srcCities,
DstCities: dstCities,
Args: args,
},
)
if err != nil {
logger.Fatal(err)
}

var wg sync.WaitGroup
wg.Add(len(priceGraphOffers))

for _, priceGraphOffer := range priceGraphOffers {
go func(offer flights.Offer) {
defer wg.Done()
offers, _, err := session.GetOffers(
flights.OffersArgs{
Date: offer.StartDate,
ReturnDate: offer.ReturnDate,
SrcCities: srcCities,
DstCities: dstCities,
Args: args,
},
)
if err != nil {
logger.Fatal(err)
}

var bestOffer flights.FullOffer
for _, o := range offers {
if bestOffer.Price == 0 || o.Price < bestOffer.Price {
bestOffer = o
}
}

_, priceRange, err := session.GetOffers(
flights.OffersArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcAirports: []string{bestOffer.SrcAirportCode},
DstAirports: []string{bestOffer.DstAirportCode},
Args: args,
},
)
if err != nil {
logger.Fatal(err)
}
if priceRange == nil {
logger.Fatal("missing priceRange")
}

if bestOffer.Price < priceRange.Low {
url, err := session.SerializeURL(
flights.URLArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcAirports: []string{bestOffer.SrcAirportCode},
DstAirports: []string{bestOffer.DstAirportCode},
Args: args,
},
)
if err != nil {
logger.Fatal(err)
}
logger.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
logger.Printf("price %d\n", int(bestOffer.Price))
logger.Println(url)
}
}(priceGraphOffer)
}
wg.Wait()
}

func main() {
t := time.Now()

getCheapOffersConcurrent(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
7,
[]string{"Mia", "Orlando"},
[]string{"New York", "Philadelphia", "Washington"},
language.English,
)

fmt.Println(time.Since(t))
}
6 changes: 4 additions & 2 deletions flights/city.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func abbrCitySchema(city, abbrCity *string) *[][][][]interface{} {
return &[][][][]interface{}{{{{nil, nil, city, nil, abbrCity}}}}
}

// AbbrCity serializes the city name by requesting it from the Google Flight API. The city name should be provided
// in the language described by [language.Tag].
// AbbrCity serializes the city name by requesting it from the Google Flight API. The city name should
// be provided in the language described by [language.Tag].
//
// AbbrCity returns an error if the city name is misspelled.
func (s *Session) AbbrCity(city string, lang language.Tag) (string, error) {
if abbrCity, ok := s.Cities.Load(city); ok {
return abbrCity, nil
Expand Down
89 changes: 89 additions & 0 deletions flights/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package flights_test

import (
"fmt"
"log"
"time"

"github.com/krisukox/google-flights-api/flights"
"golang.org/x/text/currency"
"golang.org/x/text/language"
)

func ExampleSession_GetPriceGraph() {
session := flights.New()

offers, err := session.GetPriceGraph(
flights.PriceGraphArgs{
RangeStartDate: time.Now().AddDate(0, 0, 30),
RangeEndDate: time.Now().AddDate(0, 0, 60),
TripLength: 7,
SrcCities: []string{"San Francisco"},
DstCities: []string{"New York"},
Args: flights.ArgsDefault(),
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(offers)
}

func ExampleSession_SerializeURL() {
session := flights.New()

url, err := session.SerializeURL(
flights.URLArgs{
Date: time.Now().AddDate(0, 0, 30),
ReturnDate: time.Now().AddDate(0, 0, 37),
SrcCities: []string{"San Diego"},
SrcAirports: []string{"LAX"},
DstCities: []string{"New York", "Philadelphia"},
Args: flights.ArgsDefault(),
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}

func ExampleSession_GetOffers() {
session := flights.New()

offers, priceRange, err := session.GetOffers(
flights.OffersArgs{
Date: time.Now().AddDate(0, 0, 30),
ReturnDate: time.Now().AddDate(0, 0, 37),
SrcCities: []string{"Madrid"},
DstCities: []string{"Estocolmo"},
Args: flights.Args{
Adults: 2,
Currency: currency.EUR,
Stops: flights.Stop1,
Class: flights.Economy,
TripType: flights.RoundTrip,
Lang: language.Spanish,
},
},
)
if err != nil {
log.Fatal(err)
}

if priceRange != nil {
fmt.Printf("High price %d\n", int(priceRange.High))
fmt.Printf("Low price %d\n", int(priceRange.Low))
}
fmt.Println(offers)
}

func ExampleSession_AbbrCity() {
session := flights.New()

city, err := session.AbbrCity("New York", language.English)
if err != nil {
log.Fatal(err)
}
fmt.Println(city)
}
Loading

0 comments on commit 495cf24

Please sign in to comment.