Skip to content

Commit

Permalink
Improve URL serialization (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox authored Jul 15, 2023
1 parent e08b4d6 commit 7b2bff8
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 164 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- '**'

jobs:
integration_test:
unit_test:
name: Run tests
runs-on: ubuntu-22.04
steps:
Expand All @@ -21,6 +21,6 @@ jobs:
- name: Check out repository code
uses: actions/checkout@v3

- name: Run integration test
- name: Run unit tests
run: |
go test ./... -v
102 changes: 0 additions & 102 deletions api/url.go

This file was deleted.

46 changes: 0 additions & 46 deletions api/url_test.go

This file was deleted.

23 changes: 18 additions & 5 deletions api/flight.go → flight.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package main

import (
"bufio"
Expand Down Expand Up @@ -107,17 +107,30 @@ func getPriceSuffix(unit currency.Unit) string {
}

func GetFlights(
departureDate time.Time,
date time.Time,
returnDate time.Time,
departureCity string,
arivalCity string,
srcCity string,
dstCity string,
unit currency.Unit,
) ([]flight, error) {
if !isSupportedCurrency(unit) {
return nil, fmt.Errorf(unit.String() + " is not supproted yet")
}

url, err := CreateFullURL(departureDate, returnDate, departureCity, arivalCity)
url, err := SerializeUrl(
date,
returnDate,
[]string{srcCity},
[]string{},
[]string{dstCity},
[]string{},
1,
unit,
AnyStops,
Economy,
RoundTrip,
)

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/flight_test.go → flight_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package main

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions api/flight_v2.go → flight_v2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package main

import (
"bufio"
Expand Down Expand Up @@ -66,11 +66,11 @@ func (t offer) String() string {
}

func GetRawData(date, returnDate time.Time, originCity, targetCity string) string {
serializedOriginCity, err := GetSerializedCityName(originCity)
serializedOriginCity, err := AbbrCity(originCity)
if err != nil {
log.Fatalf(err.Error())
}
serializedTargetCity, err := GetSerializedCityName(targetCity)
serializedTargetCity, err := AbbrCity(targetCity)
if err != nil {
log.Fatalf(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion api/flight_v2_test.go → flight_v2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package main

import (
"testing"
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"log"
"time"

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

func main() {
date, _ := time.Parse("2006-01-02", "2023-07-04")
returnDate, _ := time.Parse("2006-01-02", "2023-07-08")

offer, err := api.GetOffers(date, returnDate, "Wrocław", "Rzym", currency.PLN)
offer, err := GetOffers(date, returnDate, "Wrocław", "Rzym", currency.PLN)
if err != nil {
log.Fatalf(err.Error())
}
Expand Down
16 changes: 14 additions & 2 deletions api/serialize_city.go → serialize_city.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package main

import (
"bufio"
Expand Down Expand Up @@ -88,7 +88,7 @@ func decodeInnerObject(body *bufio.Reader) ([][][][]interface{}, error) {
return innerObject, nil
}

func GetSerializedCityName(city string) (string, error) {
func AbbrCity(city string) (string, error) {
resp, err := sendRequest(city)
if err != nil {
return "", err
Expand Down Expand Up @@ -117,3 +117,15 @@ func GetSerializedCityName(city string) (string, error) {

return serializedCity, nil
}

func AbbrCities(cities []string) ([]string, error) {
abbrCities := []string{}
for _, c := range cities {
sc, err := AbbrCity(c)
if err != nil {
return nil, err
}
abbrCities = append(abbrCities, sc)
}
return abbrCities, nil
}
Loading

0 comments on commit 7b2bff8

Please sign in to comment.