rpc-gov-registry/cee.go

92 lines
2.1 KiB
Go

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
}