-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example 3 and improve docs (#16)
- Loading branch information
Showing
13 changed files
with
297 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.