This commit is contained in:
Arnaud Delcasse
2025-10-10 15:11:27 +02:00
parent 63fc3e7c83
commit a2c82a1a5c
6 changed files with 617 additions and 76 deletions

View File

@@ -15,16 +15,18 @@ import (
)
type BBCDailyCarpoolAPI struct {
OperatorId string
APIKey string
BaseURL string
OperatorId string
OperatorName string
APIKey string
BaseURL string
}
func NewBBCDailyCarpoolAPI(operatorId string, api_key string, baseURL string) (*BBCDailyCarpoolAPI, error) {
func NewBBCDailyCarpoolAPI(operatorId string, operatorName string, api_key string, baseURL string) (*BBCDailyCarpoolAPI, error) {
return &BBCDailyCarpoolAPI{
OperatorId: operatorId,
APIKey: api_key,
BaseURL: baseURL,
OperatorId: operatorId,
OperatorName: operatorName,
APIKey: api_key,
BaseURL: baseURL,
}, nil
}
@@ -95,23 +97,33 @@ func (api *BBCDailyCarpoolAPI) GetDriverJourneys(
Currency: &r.Price.Currency,
}
}
var pickupDatetime ocss.OCSSTime
// Try to parse as Unix timestamp first
i, err := strconv.ParseInt(*r.PickupDatetime, 10, 64)
if err != nil {
log.Error().Err(err).Msg("error in string ot int conversion")
// If not a Unix timestamp, try parsing as ISO 8601 datetime
pd, err2 := time.Parse(time.RFC3339, *r.PickupDatetime)
if err2 != nil {
log.Error().Err(err2).Str("datetime", *r.PickupDatetime).Msg("error parsing pickup datetime")
pickupDatetime = ocss.OCSSTime(departureDate)
} else {
pickupDatetime = ocss.OCSSTime(pd)
}
} else {
pd := time.Unix(i, 0)
pickupDatetime = ocss.OCSSTime(pd)
}
pd := time.Unix(i, 0)
pickupDatetime := ocss.OCSSTime(pd)
driverJourney := ocss.DriverJourney{
DriverTrip: ocss.DriverTrip{
Driver: ocss.User{
ID: uuid.NewString(),
Operator: api.OperatorId,
Alias: "Utilisateur BlablacarDaily",
Operator: api.OperatorName,
Alias: "Nom anonymisé",
},
DepartureToPickupWalkingDuration: r.DepartureToPickupWalkingTime,
DropoffToArrivalWalkingDuration: r.DropoffToArrivalWalkingTime,
Trip: ocss.Trip{
Operator: api.OperatorId,
Operator: api.OperatorName,
PassengerPickupLat: nilCheck(r.PickupLatitude),
PassengerPickupLng: nilCheck(r.PickupLongitude),
PassengerDropLat: nilCheck(r.DropoffLatitude),
@@ -171,19 +183,38 @@ func blablacarDailySearch(url string, access_token string, departure_latitude fl
log.Error().Err(err).Msg("error in BBCDaily request")
return nil, err
}
defer resp.Body.Close()
response := []BBCDailyResult{}
log.Debug().
Int("status_code", resp.StatusCode).
Str("status", resp.Status).
Msg("BlaBlaCarDaily API response received")
err = json.NewDecoder(resp.Body).Decode(&response)
body, err := io.ReadAll(resp.Body)
if err != nil {
body, err2 := io.ReadAll(resp.Body)
if err2 != nil {
log.Error().Err(err2).Msg("error reading json string")
}
log.Error().Err(err).Bytes("resp body", body).Any("status", resp.Status).Msg("cannot read json response to blablacardaily API")
log.Error().Err(err).Msg("error reading BBCDaily response body")
return nil, err
}
log.Debug().
Str("response_body", string(body)).
Msg("BlaBlaCarDaily API raw response")
response := []BBCDailyResult{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Error().
Err(err).
Str("resp_body", string(body)).
Any("status", resp.Status).
Msg("cannot parse json response from BlaBlaCarDaily API")
return nil, err
}
log.Debug().
Int("results_count", len(response)).
Msg("BlaBlaCarDaily API response parsed successfully")
return response, nil
}