Skip to content

Commit

Permalink
Adding getpricing config get endpoint as an informational data endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rajagopal28 committed Jul 30, 2021
1 parent 4286a1b commit 1dbd6ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions service/app/generate_pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,19 @@ func (a *App) GeneratePricing(ctx context.Context, request *pricingengine.Genera
log.Println("Leaving GeneratePricing")
return &result, nil
}

// GeneratePricingConfig fetch and cache the configs related to pricing computations
//
func (a *App) GeneratePricingConfig(ctx context.Context) (interface{}, error) {
log.Println("Entering GeneratePricingConfig")
a.Cache.Fetcher = config.ConfigFetcher{Path: "/config/"} // Initialise with actual path
a.Cache.InitialiseWithRefresh(false, 100000) // time to live 100000s
var result map[string]interface{} = make(map[string]interface{})

result["base-rate"] = a.Cache.BaseRateList
result["driver-age-factor"] = a.Cache.DriverAgeFactorList
result["insurance-group-factor"] = a.Cache.InsuranceGroupFactorList
result["licence-validity-factor"] = a.Cache.LicenceValidityFactorList
log.Println("Leaving GeneratePricingConfig")
return result, nil
}
24 changes: 24 additions & 0 deletions service/rpc/generate_pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ func (rpc *RPC) GeneratePricing(w http.ResponseWriter, r *http.Request) {
response(w, res)
}

// GeneratePricingConfig method is a GET method that typically takes care of
// fetching the current pricing config that is configured
// This is an informational call that does not change any existing data but
// to just view the configs based on which the pricing computations are performed
func (rpc *RPC) GeneratePricingConfig(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response(w, err)
return
}

r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))

// query := r.URL.Query()
// println("query=", query.Get("quer"))
res, err := rpc.App.GeneratePricingConfig(r.Context())
if err != nil {
response(w, err)
return
}
response(w, res)
}

// response writes a successful response as JSON to the client
func response(w http.ResponseWriter, res interface{}) {
if err, ok := res.(error); ok {
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ func Start() {
}

r.Post("/generate_pricing", rpc.GeneratePricing)
r.Get("/generate_pricing", rpc.GeneratePricingConfig)
http.ListenAndServe(":3000", r)
}

0 comments on commit 1dbd6ae

Please sign in to comment.