Repository navigation
Expand file tree
/
Copy pathklm.go
More file actions
145 lines (122 loc) · 3.12 KB
/
Copy pathklm.go
File metadata and controls
145 lines (122 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package klm
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"time"
)
const (
departureDateFormat = "2006-01-02"
)
type FlightDetails struct {
AirportCode string
FlightNumber int32
Date time.Time
}
func (f *FlightDetails) DepartureDateTime() string {
var time string
if f.AirportCode == "BUD" {
time = "T06:30:00"
} else if f.AirportCode == "AMS" {
time = "T20:55:00"
}
return string(f.Date.Format(departureDateFormat) + time)
}
func (f *FlightDetails) DepartureDate() string {
return f.Date.Format(departureDateFormat)
}
type FlightOffering struct {
Origin FlightDetails
Destination FlightDetails
Price float32
Currency string
}
type OfferDetails struct {
UpsellProducts []struct {
Price struct {
DisplayPrice float32 `json:"displayPrice"`
TotalPrice float32 `json:"totalPrice"`
Accuracy float32 `json:"accuracy"`
PricePerPassengerTypes []struct {
PassengerType string `json:"passengerType"`
Fare float32 `json:"fare"`
Taxes float32 `json:"taxes"`
} `json:"pricePerPassengerTypes"`
FlexibilityWaiver bool `json:"flexibilityWaiver"`
Currency string `json:"currency"`
DisplayType string `json:"displayType"`
} `json:"price"`
}
}
func GetOffers(origin, destination FlightDetails) FlightOffering {
d := FlightOffering{
Origin: origin,
Destination: destination,
}
if destination.Date.Sub(origin.Date) < 0 {
return d
}
var has_results bool
d.Price, has_results = DbGetPrice(d)
if has_results == false { // no price in the database
reqJSON := prepareRequestJson(d)
offers, _ := sendAPIRequest(reqJSON)
d.Price, d.Currency = FindCheapest(offers)
// cache result in db
DbInsertOffer(d)
}
if d.Currency == "" { // set currency here
// ASSUMPTION IS THE MOTHER OF ALL FUCKUPS
if d.Price < 10000 {
d.Currency = "EUR"
} else {
d.Currency = "HUF"
}
}
return d
}
func prepareRequestJson(data FlightOffering) bytes.Buffer {
t, err := template.ParseFiles("upsell.json")
if err != nil {
fmt.Println(err)
}
buf := new(bytes.Buffer)
t.Execute(buf, &data)
return *buf
}
func sendAPIRequest(jsonStr bytes.Buffer) (OfferDetails, error) {
url := "https://www.klm.com/ams/search-web/api/upsell-products?country=CH&language=en&localeStringDateTime=en&localeStringNumber=de-CH"
req, err := http.NewRequest("POST", url, &jsonStr)
req.Header.Set("Content-Type", "application/json")
var offerDetails OfferDetails
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Errorf("%s", err)
return offerDetails, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&offerDetails)
if err != nil {
fmt.Errorf("%s", err)
return offerDetails, err
}
return offerDetails, nil
}
func FindCheapest(offers OfferDetails) (float32, string) {
lowest := float32(0)
currency := ""
for _, p := range offers.UpsellProducts {
if lowest == 0 {
lowest = p.Price.DisplayPrice
}
if lowest > p.Price.DisplayPrice {
lowest = p.Price.DisplayPrice
}
currency = p.Price.Currency
}
return lowest, currency
}