Initial cloud-services repo - gateway service + pkg modules

This commit is contained in:
Chris Rai
2026-01-30 23:14:52 -05:00
commit fbb820d7b3
1037 changed files with 171318 additions and 0 deletions

52
pkg/userconsent/mock.go Normal file
View File

@@ -0,0 +1,52 @@
package userconsent
import (
"net/http"
"net/http/httptest"
"fiskerinc.com/modules/common"
"fiskerinc.com/modules/validator"
"fiskerinc.com/modules/utils"
)
type UserConsentServiceMock struct{}
func (ucsm *UserConsentServiceMock) UserConsentByVehicleNumber(vin string) (*http.Response, error) {
w := httptest.NewRecorder()
userConsentDataArray := []common.UserConsentDataResponse{
{
VehicleIdNum: vin,
ConsentName: validator.NAV_LOCATION_DATA,
ConsentAction: "ACCEPTED",
},
{
VehicleIdNum: vin,
ConsentName: validator.CONNECTED_CAR_FEATURE,
ConsentAction: "ACCEPTED",
},
}
if vin == "9F15K3R45N1234567" {
userConsentDataArray[0].ConsentAction = "DECLINED"
}
utils.RespJSON(w, http.StatusOK, userConsentDataArray)
return w.Result(), nil
}
func (ucsm *UserConsentServiceMock) UserConsent(ucd common.UserConsentDataRequest) (*http.Response, error) {
w := httptest.NewRecorder()
userConsentDataResponse := common.UserConsentDataResponse{
VehicleIdNum: ucd.VehicleIdNum,
ConsentName: ucd.ConsentName,
ConsentAction: ucd.ConsentAction,
}
utils.RespJSON(w, http.StatusCreated, userConsentDataResponse)
return w.Result(), nil
}

View File

@@ -0,0 +1,91 @@
package userconsent
import (
"bytes"
"encoding/json"
"net/http"
"sync"
"fiskerinc.com/modules/common"
"fiskerinc.com/modules/logger"
"fiskerinc.com/modules/utils/envtool"
"github.com/pkg/errors"
)
var (
userConsentService UserConsentServiceInterface
userConsentOnce sync.Once
)
func GetUserConsentService() UserConsentServiceInterface {
userConsentOnce.Do(func() {
if userConsentService != nil {
return
}
userConsentService = NewUserConsentService()
})
return userConsentService
}
func SetUserConsentService(ucs UserConsentServiceInterface) {
userConsentService = ucs
}
func NewUserConsentService() UserConsentServiceInterface {
return &UserConsentService{
userConsentURL: envtool.GetEnv("USER_CONSENT_URL", "REPLACE_ME"),
userConsentXApiKey: envtool.GetEnv("USER_CONSENT_X_API_KEY", "REPLACE_ME"),
}
}
type UserConsentServiceInterface interface {
UserConsent(ucd common.UserConsentDataRequest) (*http.Response, error)
UserConsentByVehicleNumber(vin string) (*http.Response, error)
}
type UserConsentService struct {
userConsentURL string
userConsentXApiKey string
}
func (ucs *UserConsentService) UserConsent(ucd common.UserConsentDataRequest) (*http.Response, error) {
jsonBytes, err := json.Marshal(ucd)
if err != nil {
return nil, err
}
logger.Info().Msg(string(jsonBytes))
request, err := http.NewRequest(http.MethodPost, ucs.userConsentURL+"/user-consent", bytes.NewReader(jsonBytes))
if err != nil {
return nil, errors.WithStack(err)
}
request.Header.Add("accept", "application/json")
request.Header.Add("x-api-key", ucs.userConsentXApiKey)
request.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
return resp, errors.WithStack(err)
}
func (ucs *UserConsentService) UserConsentByVehicleNumber(vin string) (*http.Response, error) {
ucvn := common.UserConsentByVehicleNumberRequest{VehicleNumber: vin}
jsonBytes, err := json.Marshal(ucvn)
if err != nil {
return nil, errors.WithStack(err)
}
request, err := http.NewRequest(http.MethodPost, ucs.userConsentURL+"/user-consent/byVehicleNumber", bytes.NewReader(jsonBytes))
if err != nil {
return nil, errors.WithStack(err)
}
request.Header.Add("accept", "application/json")
request.Header.Add("x-api-key", ucs.userConsentXApiKey)
request.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
return resp, errors.WithStack(err)
}