Skip to content

Commit

Permalink
Group api arguments into structs
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox committed Jul 29, 2023
1 parent 8e7c426 commit b056a14
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 499 deletions.
131 changes: 80 additions & 51 deletions examples/example1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,98 @@ import (
func getCheapOffers(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCities, dstCities []string, lang language.Tag) {
session := flights.New()

for _, s := range srcCities {
for _, d := range dstCities {
offers, err := session.GetPriceGraph(
rangeStartDate, rangeEndDate,
[]string{s}, []string{}, []string{d}, []string{},
1, currency.PLN, flights.AnyStops, flights.Economy, flights.RoundTrip,
lang, tripLength)
if err != nil {
log.Fatal(err)
args := flights.Args{
Adults: 1,
Curr: currency.PLN,
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 {
log.Fatal(err)
}

for _, priceGraphOffer := range priceGraphOffers {
offers, _, err := session.GetOffers(
flights.OffersArgs{
Date: priceGraphOffer.StartDate,
ReturnDate: priceGraphOffer.ReturnDate,
SrcCities: srcCities,
DstCities: dstCities,
Args: args,
},
)
if err != nil {
log.Fatal(err)
}

var bestOffer flights.FullOffer

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

for _, o := range offers {
_, priceRange, err := session.GetOffers(
o.StartDate,
o.ReturnDate,
[]string{s},
[]string{},
[]string{d},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
}
_, 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 {
log.Fatal(err)
}
if priceRange == nil {
log.Fatal("missing priceRange")
}

if o.Price < priceRange.Low {
fmt.Printf("%s %s\n", o.StartDate, o.ReturnDate)
fmt.Printf("price %d\n", int(o.Price))
url, err := session.SerializeUrl(
o.StartDate,
o.ReturnDate,
[]string{s},
[]string{},
[]string{d},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}
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 {
log.Fatal(err)
}
fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
fmt.Println(url)
}
}
}

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

getCheapOffers(
time.Now().AddDate(0, 0, 60),
time.Now().AddDate(0, 0, 90),
2,
[]string{"Berlin", "Prague"},
[]string{"Athens"},
language.English)
[]string{"Athens", "Rome"},
language.English,
)

fmt.Println(time.Since(t))
}
44 changes: 27 additions & 17 deletions examples/example2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
session := flights.New()
var bestOffer flights.Offer

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

offers, err := session.GetPriceGraph(
rangeStartDate, rangeEndDate,
[]string{srcCity}, []string{}, []string{dstCity}, []string{},
1, currency.PLN, flights.AnyStops, flights.Economy, flights.RoundTrip,
lang, tripLength)
flights.PriceGraphArgs{
RangeStartDate: rangeStartDate,
RangeEndDate: rangeEndDate,
TripLength: tripLength,
SrcCities: []string{srcCity},
DstCities: []string{dstCity},
Args: args,
},
)
if err != nil {
log.Fatal(err)
}
Expand All @@ -32,18 +46,13 @@ func getBestOffer(rangeStartDate, rangeEndDate time.Time, tripLength int, srcCit
fmt.Printf("%s %s\n", bestOffer.StartDate, bestOffer.ReturnDate)
fmt.Printf("price %d\n", int(bestOffer.Price))
url, err := session.SerializeUrl(
bestOffer.StartDate,
bestOffer.ReturnDate,
[]string{srcCity},
[]string{},
[]string{dstCity},
[]string{},
1,
currency.PLN,
flights.AnyStops,
flights.Economy,
flights.RoundTrip,
lang,
flights.UrlArgs{
Date: bestOffer.StartDate,
ReturnDate: bestOffer.ReturnDate,
SrcCities: []string{srcCity},
DstCities: []string{dstCity},
Args: args,
},
)
if err != nil {
log.Fatal(err)
Expand All @@ -58,5 +67,6 @@ func main() {
2,
"Warsaw",
"Athens",
language.English)
language.English,
)
}
Loading

0 comments on commit b056a14

Please sign in to comment.