From 0ce1c108ed2d0b38ae1b0d81dbd51278ea9cb07c Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Wed, 29 Mar 2023 13:18:40 +0200 Subject: [PATCH] Initial commit --- README.md | 3 ++ cee.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ journeys.go | 42 +++++++++++++++++++++++++ rpc.go | 26 +++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 README.md create mode 100644 cee.go create mode 100644 go.mod create mode 100644 journeys.go create mode 100644 rpc.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c532ad --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# COOPGO RPC Gov Registry + +This library implements the RPC (Registre de Preuve de Covoiturage) API : more informations : https://tech.covoiturage.beta.gouv.fr \ No newline at end of file diff --git a/cee.go b/cee.go new file mode 100644 index 0000000..5001498 --- /dev/null +++ b/cee.go @@ -0,0 +1,91 @@ +package rpc + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "time" + + "github.com/rs/zerolog/log" +) + +type RPCShortDistanceCEERequest struct { + JourneyType string `json:"journey_type"` + DrivingLicence string `json:"driving_licence"` + LastNameTrunc string `json:"last_name_trunc"` + OperatorJourneyId string `json:"operator_journey_id"` + ApplicationTimestamp RPCTime `json:"application_timestamp"` +} + +type RPCCEEResponse struct { + Datetime RPCTimestamp `json:"datetime"` + UUID string `json:"uuid"` + JourneyId string `json:"journey_id"` + Status string `json:"status"` + Token string `json:"token"` +} + +type RPCCEESimulate struct { + JourneyType string `json:"journey_type"` + DrivingLicence string `json:"driving_licence"` + LastNameTrunc string `json:"last_name_trunc"` + PhoneTrunc string `json:"phone_trunc"` +} + +type RPCCEESimulateResponse struct { + Datetime RPCTime `json:"datetime"` + UUID string `json:"uuid"` +} + +type RPCTimestamp time.Time + +func (h RPCHandler) PolicyCEESimulate(req RPCCEESimulate) (success bool, issue *RPCCEESimulateResponse, err error) { + // Test case + if req.DrivingLicence == "0123456789" { + return true, nil, nil + } + + j, err := json.Marshal(req) + if err != nil { + return false, nil, err + } + + request, err := http.NewRequest("POST", h.BaseURL+"/policy/cee/simulate", bytes.NewBuffer(j)) + if err != nil { + return false, nil, err + } + + request.Header.Add("Authorization", "Bearer "+h.Token) + client := &http.Client{} + resp, err := client.Do(request) + + if err != nil { + + log.Error(). + Err(err). + Str("driving_licence", req.DrivingLicence). + Str("last_name_trunc", req.LastNameTrunc). + Str("phone_trunc", req.PhoneTrunc). + Msg("RPC CEE simulate returned an error") + + if resp != nil { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return false, nil, err + } + log.Debug().Str("body", string(body)).Msg("returned body content") + + var result *RPCCEESimulateResponse + err = json.Unmarshal(body, result) + if err != nil { + return false, nil, err + } + return false, result, err + } + + return false, nil, err + } + + return true, nil, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2a36083 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.coopgo.io/coopgo-platform/rpc-gov-registry + +go 1.18 diff --git a/journeys.go b/journeys.go new file mode 100644 index 0000000..dd3e5c5 --- /dev/null +++ b/journeys.go @@ -0,0 +1,42 @@ +package rpc + +type RPCJourney struct { + JourneyId string `json:"journey_id"` + OperatorJourneyId string `json:"operator_journey_id"` + OperatorClass string `json:"operator_class"` +} + +type RPCDriver struct { + Revenue int64 `json:"revenue"` + Incentives []RPCIncentive `json:"incentive"` + Identity RPCIdentity `json:"identity"` + Start RPCLoc `json:"start"` + End RPCLoc `json:"end"` +} + +type RPCPassenger struct { + Contribution int64 `json:"contribution"` + Seats int64 `json:"seats"` + Incentives []RPCIncentive `json:"incentive"` + Identity RPCIdentity `json:"identity"` + Start RPCLoc `json:"start"` + End RPCLoc `json:"end"` +} + +type RPCIncentive struct { + Index int64 `json:"index"` + Amount int64 `json:"amount"` + SIRET string `json:"siret"` +} + +type RPCIdentity struct { + PhoneTrunc string `json:"phone_trunc"` + OperatorUserId string `json:"operator_user_id"` + Over18 bool `json:"over_18"` +} + +type RPCLoc struct { + Datetime RPCTime `json:"datetime"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` +} diff --git a/rpc.go b/rpc.go new file mode 100644 index 0000000..581c6ac --- /dev/null +++ b/rpc.go @@ -0,0 +1,26 @@ +package rpc + +import "time" + +const ( + ProductionBaseURL = "https://api.covoiturage.beta.gouv.fr/v2" + DemoBaseURL = "https://api.demo.covoiturage.beta.gouv.fr/v2" +) + +type RPCHandler struct { + BaseURL string + Token string +} + +func NewRPCHandler(baseurl string, token string) RPCHandler { + return RPCHandler{ + BaseURL: baseurl, + Token: token, + } +} + +type RPCTime time.Time + +func (t RPCTime) MarshalJSON() ([]byte, error) { + return []byte(`"` + time.Time(t).Format("2006-01-02T15:04:05Z") + `"`), nil +}