Initial commit

This commit is contained in:
Arnaud Delcasse 2023-03-29 13:18:40 +02:00
commit 0ce1c108ed
5 changed files with 165 additions and 0 deletions

3
README.md Normal file
View File

@ -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

91
cee.go Normal file
View File

@ -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
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.coopgo.io/coopgo-platform/rpc-gov-registry
go 1.18

42
journeys.go Normal file
View File

@ -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"`
}

26
rpc.go Normal file
View File

@ -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
}